Now Reading
Guide To OpenCV Functions For Image Processing

Guide To OpenCV Functions For Image Processing

Prudhvi varma
W3Schools

OpenCV is used as an image processing library in many computer vision real-time applications. There are thousands of functions available in OpenCV. These simple techniques are used to shape our images in our required format. As we know an image is a combination of pixels, for a color image we have three channels with pixels ranging from 0 to 225, and for black & white-colored images has only one change ranging from 0 to 1. 

In this article, we will demonstrate some important image processing steps and we will do modifications to an image by manipulating its pixels using OpenCV functions. 

Topics that will be covered this article

  • Edge detection using Canny filter
  • Converting a color image to Grayscale & vice versa.
  • Smoothing techniques
  • Morphology techniques

Converting a Color Image to a Grayscale Image

This is the basic technique that is used to convert the color image into grey shades. A color image consists of 3 channel depth while using grayscaling it reduces the depth of the image to 1 channel. It reduces model complexity. For many applications like edge detection, photo sketch, cartooning image we use grayscale converted images.



In the below code snippet we are converting a color image to a grayscale image.

import cv2

image = cv2.imread('/content/Screenshot (14).png')

grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2_imshow(grayImage)

In the above output, we converted the colored image to grayscale.

Edge detection 

In edge detection, it gives an outline of the image by extracting edges from the image. We are using a canny filter to perform this task. Based on the threshold values, a canny filter detects the edges. The higher thresholds give cleaner images compared to lower thresholds gives a clumsy output.

import cv2

import numpy as np

img = cv2.imread('/content/Screenshot (14).png',0)

edge_det = cv2.Canny(img,100,200)

cv2_imshow(edge_det)

Here we had given a threshold value 100,200 and you can try with different threshold values. This edge detection is used for segmentation, restoration, picture enhancement, pattern recognition.

Smoothing Techniques

The image smoothing technique is performed using a filter. By convolving the image, it reduces the noise in the image by adding a blurring effect on the edges. There are different types of smoothing techniques we perform depending on the input image.

Average using cv2.blur()

In this technique, depending on the size of the filter convolution is performed but it averages the pixels under the filter and assigns the value to the centre pixel under the filter.

import cv2

import numpy as np

from google.colab.patches import cv2_imshow

img = cv2.imread('/content/download (14).jpg')

blur = cv2.blur(img,(5,5))

cv2_imshow(blur)

cv2_imshow(img)

normal image

Median Blur using cv2.medianBlur()

In this technique, it calculates the median of the pixels under the filter and it replaces the center value under the filter with the median value, positive odd integer to be assigned as filter size to perform the median blur technique.

img = cv2.imread('/content/download (14).jpg')

median_blur = cv2.medianBlur(img,5)

cv2_imshow(median_blur)

cv2_imshow(img)

normal image
after applying median blur

Comparing smoothing techniques averaging and median blur gives similar results because one calculates on averaging pixels and another on calculating the medians under the filter.

Gaussian Blur using cv2.GaussianBlur()

In this technique, there is another parameter called sigma x and sigma y. It performs a blurring of an image by using parameters sigma x & sigma y. If it is set to zero then it calculates based on the size of its kernel.

img = cv2.imread('/content/download (14).jpg')

Gaussian_blur = cv2.GaussianBlur(img,(5,5),0)

cv2_imshow(median_blur)

cv2_imshow(img)

normal image
after applying Gaussian blur

As we can see in the output, by blurring the image we tried to reduce the effect of rain

See Also
pgmagick image processing

Morphological Techniques

These are the techniques used to manipulate the image sizes, these techniques are used only after converting the image into grayscale. These techniques are used for removing noise, correcting the imperfections in the data, and to make clear images. Erosion and Dilation are mostly used in morphological techniques.

Erosion

This technique removes the boundary pixels from the input by just passing the filter on the image. Depending on the size of the kernel, it removes the boundary pixels from the input image. It performs similar to soil erosion so they named this technique as erosion.

img = cv2.imread('/content/Screenshot (14).png') 

kernel = np.ones((5,5),np.uint8)

img_erosion = cv2.erode(img, kernel, iterations=1)

img_dilation = cv2.dilate(img, kernel, iterations=1)

cv2_imshow(img)

cv2_imshow(img_erosion)

normal image
image erosion

In the above output by using erosion technique  we tried to make spiderman thinner.

Dilation

The above technique removes the boundary pixels but in Dilation. It adds the additional pixels to the input. It is used when the pixels are missing in the image. In general practices, we apply erosion to shrink the image to remove noises, and then by applying the dilation, there will be no loss of pixels.

img = cv2.imread('/content/Screenshot (14).png') 

kernel = np.ones((5,5),np.uint8)

img_dilation = cv2.dilate(img, kernel, iterations=1)

cv2_imshow(img)

cv2_imshow(img_dilation)

normal image
image after dilation

In the above output using the dilation technique, we tried to make spiderman a little fatter.

Conclusion

In this article, we have illustrated different types of filters which play a key role in image processing while working on computer vision applications. Using OpenCV and support of inbuilt functions in OpenCV, we performed these implementations by just writing a few lines of codes.

What Do You Think?

If you loved this story, do join our Telegram Community.


Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.
What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top