Examples of OpenCV with Python
- Load an Image in Memory using cv2.imread(image_path, format) function. Format take value of IMREADMODES. Avilable Modes to read an image are: cv2.IMREAD_COLOR, cv2.IMREAD_UNCHANGED, cv2.IMREAD_GRAYSCALE, cv2.IMREAD_ANYCOLOR etc.
- Display the Image Loaded using cv2.imshow(Message, image) function.
- After displaying the image wait for user to press a key to terminate. cv2.waitKey(0)
- cv2.imread()
- cv2.namedWindows()
- cv2.imshow()
- cv2.waitKey()
- Modify the Image from BGR Channel to GrayScale Channel using cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).
- Write the new Image to Disk using cv2.write(name, image)
- cv2.cvtColor()
- cv2.COLOR_BGR2GRAY
- cv2.resize()
- cv2.imwrite()
- Enhance the Image by Manipulating each pixel using the formula
pixel_value = 5 * IMG(j, i) - [IMG(j - 1, i) + IMG(j + 1, i) + IMG(j, i - 1) + IMG(j, i + 1). - Another way to enhance the image is correlating the Image with a Kernel using cv2.filter2D(img, ddpeth, kernel).
- Manual Manipulation of Pixels using NumPy
- cv2.filter2D()
- cv2.cvtColor()
- cv2.Sobel()
- cv2.convertScaleAbs()
- Blend two Images together using Linear Blend i.e
- cv2.addWeigthed(array_1, alpha, array_2, beta, gamma) is used to blend the two images together (gamma = 0)
python (or python3) --image_1 "my_images/TG.jpg" --image_2 "my_images/TokyoGhoul.jpg"
- cv2.addWeighted()
- Image Processing is to pre-process a image and convert it into a suitable image or in other words take a image (or multiple) image as n input and perform some operation to give output image (or images).
Example: Color Correction, Hue Adjustment, Contrast and Brightness Control. - Point Operators (or Point Processes) is one of the Image Processing method where each pixel is manipulated independently from it's neighboring pixels. Formula:
where alpha and beta are used to control contrast and brightness respectively.
- cv2.convertScaleAbs()
- NumPy clip() for manual manipulation
- Gamma Correction is a non-linear tranformation which is used to remove the non-linear mapping b/w input radiance and quantized pixel values. Reference: Computer Vision Algorithms and Applications by Richard Szeliski
- NumPy Array
- cv2.LUT()
- cv2.convertScaleAbs()
- cv2.cvtColor()
- np.uint8()
- cv2.inRange()
- cv2.GaussianBlur()
- cv2.bitwise_and()
Smoothing or Bluring of an Image is a common task in Image Processing which is performed to reduce the noise from the image using filters.
The most common type of filter is Linear Filter where output pixel's value is calculated using the weighted sum of the input pixel value.
where, is a kernel which is nothing but the filter coefficients.
-
Here, each output pixel is the mean of the kernel neighbours. One such filter is Box Blur or Box Linear Filter.
-
Gaussian Filter
The Gaussian Filter works by convolving each pixel in the input image with a Gaussian Function and then summing them up together.Gaussian Function:
and
-
Median Filter<br? It works by traversing through each pixel in the input image and replacing the pixel with the median of its neighbouting pixels.
-
Bilateral Filter
It is an non-linear, edge-preserving and noise reducing filter i.e apart from reducing the noise from the image it also preserves the edges of the image i.e doesnot smooth out the edges rather replaces the intensity of each pixel with a weighted average of intensity value from the nearby pixels.
- cv2.blur()
- cv2.GaussianBlur()
- cv2.medianBlur()
- cv2.bilateralFilter()
Morphological Image Processing is a collection of non-linear operations related to the shape of morphological features in the image. Morphological Operations usually works on Binary Images as it only rely on the relative ordering of pixel values and not on thier numerical values but they can also be applied to GreyScale Images.
Morphological Operatations uses an Structuring Element which is nothing but a small binary image (matrix) of pixels each with a value of 0 or 1. This Structuing Element (s) is applied to an input image (f) to get a output (g).
The two most common Morphological Operations are:
-
Dilation The Operation consist of colvolving an image (f) with some structuring element (s). The Structuring Element (s) has a defined anchor point (or origin) which is usually at the center. As, (s) moves over the image (f), the maximum pixel overlapped by (s) is computed and the image pixel value is replaced by this maximum value. This operation grows the bright region in the image.
-
Erosion Erosion is in a way opposite of Dilation as it calculates the minimum over the area of the given structuring element (s). It shrinks the bright region in the image.
- cv2.getStructuringElement()
- cv2.erode()
- cv2.dilate()
- cv2.putText()
Structuring Element (s):
- Shape: cv2.MORPH_RECT
- Size: (3, 3)
Structuring Element (s):
- Shape: cv2.MORPH_CROSS
- Size: (3, 3)
Structuring Element (s):
- Shape: cv2.MORPH_ELLIPSE
- Size: (3, 3)