Digital Image Processing using OpenCV
Updated: Aug 5, 2021

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. It is used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, etc. In this blog, we will discuss about the various methods of image processing using OpenCV, it’s application and it’s benefits.
Before we start, Let's download the OpenCV package
pip install opencv-python
The very first step is Thresholding
It is one of the most common segmentation techniques in computer vision and it allows us to separate the foreground (i.e., the objects that we are interested in) from the background of the image.
Thresholding comes in two forms:
We have simple thresholding where we manually supply parameters to segment the image — this works extremely well in controlled lighting conditions where we can ensure high contrast between the foreground and background of the image.
Next we have adaptive thresholding which, instead of trying to threshold an image globally using a single value, instead breaks the image down into smaller pieces, and thresholds each of these pieces separately and individually.
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# read an image in grayscale
img = cv.imread('C:/images/cycle.jpg',0)
img = cv.resize(img, (320,225))
# apply various thresholds
val, th1 = cv.threshold(img, 110, 255, cv.THRESH_BINARY)
val, th2 = cv.threshold(img, 110, 255, cv.THRESH_BINARY_INV)
val, th3 = cv.threshold(img, 110, 255, cv.THRESH_TRUNC)
val, th4 = cv.threshold(img, 110, 255, cv.THRESH_TOZERO)
val, th5 = cv.threshold(img, 110, 255, cv.THRESH_TOZERO_INV)
# display the images
cv.imshow('Original', img)
cv.imshow('THRESH_BINARY', th1)
cv.imshow('THRESH_BINARY_INV', th2)
cv.imshow('THRESH_TRUNC', th3)
cv.imshow('THRESH_TOZERO', th4)
cv.imshow('THRESH_TOZERO_INV', th5)
cv.waitKey(0)
cv.destroyAllWindows()
Some of the outputs are:





We can also implement adaptive thresholding:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('C:/images/cycle.jpg', 0)
img = cv.resize(img, (320,225))
# apply various adaptive thresholds
th1=cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY, 7, 4)
th2=cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY, 7, 4)


Image smoothing
Image smoothing is an important image processing technique that performs blurring and noise filtering in an image. It finds applications in preprocessing and postprocessing of deep learning models. In general, smoothing is performed by a 2D kernel of a specific size on each channel of the image. The kernel average of neighborhoods yields the resulting image. It is of 4 types:
Simple average blurring
Gaussian blurring
Median filtering
Bilateral filtering
Simple average blurring:- it takes an area of pixels surrounding a central pixel, averages all these pixels together, and replaces the central pixel with the average. By taking the average of the region surrounding a pixel, we are smoothing it and replacing it with the value of its local neighborhood. This allows us to reduce noise and the level of detail, simply by relying on the average.
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('C:/images/cycle.jpg', 1)
img = cv.resize(img, (300,300))
# Apply blur
img1 = cv.blur(img,(3,3))
# display the images
cv.imshow('Original', img)
cv.imshow('Blur', img1)
cv.waitKey(0)
cv.destroyAllWindows()
