Computer Vision Techniques that You Must Implement Today
Free Python course with 25 projects (coupon code: DATAFLAIR_PYTHON) Start Now
Today’s article will be very interesting. We will apply many computer vision techniques like Thresholding, Blurring, Color Filtering and Edge detection that we use in computer vision. One by one we will understand the purpose behind these various techniques and why they are used. So, start your coding editors and play around with your images by implementing these techniques with DataFlair.
Download the source code and images of the article from the zip file.
Computer Vision Techniques
Here are the various computer vision techniques with their implementation:
Thresholding is a basic concept in computer vision. It is widely used in image processing.
In very simple words, thresholding is used to simplify visual data for further analysis. In image processing, we need to pre-process the image data and get the important details. This technique is important to separate background image and foreground image.
In the starting, our image is colorful and it will contain 3 values ranging from 0-255 for every pixel. In thresholding, first, we have to convert the image in gray-scale. So for a gray-scale image, we only need 1 value for every pixel ranging from 0-255. By converting the image, we have reduced the data and we still have the information.
The next step in thresholding is to define a threshold value that will filter out the information which we don’t want. All the pixel values less than the threshold value will become zero and the pixel values greater than the threshold value will become 1. As you can see, now our image data only consist of values 0 and 1.
The function used for the threshold is given below:
cv2.threshold(img , 125, 255, cv2.THRESH_BINARY)
The first parameter is the image data, the second one is the threshold value, the third is the maximum value (generally 255) and the fourth one is the threshold technique. Let’s implement this in our code and observe how different techniques affect the image.
import cv2 #Reading image from computer img = cv2.imread('free.jpg') #Resizing image to make it smaller img = cv2.resize(img, (320,240)) #Converting color image to grayscale gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Applying threshold to the gray image ret, thresh1 = cv2.threshold(gray_img, 125, 255, cv2.THRESH_BINARY) ret, thresh2 = cv2.threshold(gray_img, 125, 255, cv2.THRESH_BINARY_INV) ret, thresh3 = cv2.threshold(gray_img, 125, 255, cv2.THRESH_TRUNC) ret, thresh4 = cv2.threshold(gray_img, 125, 255, cv2.THRESH_TOZERO) ret, thresh5 = cv2.threshold(gray_img, 125, 255, cv2.THRESH_TOZERO_INV) #Displaying resultant images cv2.imshow('Original Image', img) cv2.imshow('Binary Threshold', thresh1) cv2.imshow('Binary Threshold Inverted', thresh2) cv2.imshow('Truncated Threshold', thresh3) cv2.imshow('Set to 0', thresh4) cv2.imshow('Set to 0 Inverted', thresh5) #Wait for user to press key for exit cv2.waitKey(0) cv2.destroyAllWindows()
Revise the important concepts of OpenCV library with OpenCV Python Tutorial
2. Blurring and Smoothing images
Images may contain lots of noise. There are few techniques through which we can reduce the amount of noise by blurring them. The blurring technique is used as a preprocessing step in various other algorithms. With blurring, we can hide the details when necessary. For example – the police use the blurring technique to hide the face of the criminal. Later on, when learning about edge detection in the image, we will see how blurring the image improves our edge detection experience.
There are different computer vision algorithms available to us which will blur the image using a kernel size. There is no right kernel size, according to our need, we have to try the trial and error method to see what works better in our case.
There are four different types of functions available for blurring:
- cv2.blur() – This function takes average of all the pixels surrounding the filter. It is a simple and fast blurring technique.
- cv2.GaussianBlur() – Gaussian function is used as a filter to remove noise and reduce detail. It is used in graphics software and also as a preprocessing step in machine learning and deep learning models.
- cv2.medianBlur() – This function uses median of the neighbouring pixels. Widely used in digital image processing as under certain conditions, it can preserve some edges while removing noise.
- cv2.bilateralFilter() – In this method, sharp edges are preserved while the weak ones are discarded.
import cv2 #Reading image from disk dog = cv2.imread('dog.jpg') #Resizing image to make it smaller dog = cv2.resize(dog, (300,280) ) #Applying different blur functions with 7*7 filter img_0 = cv2.blur(dog, ksize = (7, 7)) img_1 = cv2.GaussianBlur(dog, (7, 7), 0) img_2 = cv2.medianBlur(dog, 7) img_3 = cv2.bilateralFilter(dog, 7, 75, 75) #Displaying resultant images cv2.imshow('Original', dog) cv2.imshow('Blur', img_0) cv2.imshow('Gaussian Blur', img_1) cv2.imshow('Median Blur', img_2) cv2.imshow('Bilateral Filter', img_3) #Waits for a user to press key for exit cv2.waitKey(0) cv2.destroyAllWindows()
Work on the Driver Drowsiness Detection Python Project with OpenCV
3. Color Filtering
When you need information about a specific color, you can take out the color you want.
This process is called color filtering. We can define a range of color which we want to focus at. For example – if you want to track a tennis ball in an image, we can filter out only green color from the image. This computer vision technique works better when the color that needs to be extracted is different from the background. Let’s see how to implement color filtering in your application.
Step 1: Convert the color image into HSV which is Hue Saturation Value
HSV is also another way of representing a color. It is easier to specify a color range in HSV format that is why OpenCV expects us to specify the range in this format. Hue is for color (0- 179), Saturation is for the strength of color (0-255) and Value is for the different lighting conditions from low to high (0-255).
Step 2: Create the mask
OpenCV provides us with cv2.inRange() function. The first parameter contains the HSV image, the second parameter has a numpy array with lower bound of the color we want to extract and the third parameter contains the upper bound of the color.
Note: The upper and lower bound of color should be in HSV value. The mask contains only the 0 and 1 values representing black and white respective.
Step 3: Perform bitwise ‘and’ operation
At last, we perform bitwise ‘and’ operation on the original image and the mask image to get only the color we want.
import cv2 import numpy as np #Reading images from local disk img = cv2.imread('ball.jpeg') #Resizing image to make it smaller(optional) img = cv2.resize(img, (320,240)) #Defining lower and upper range for color we want to extract(hsv format) lower_orange = np.array([8, 100, 50]) upper_orange = np.array([15, 255, 255]) #Converting image from BGR to HSV format hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) #cv2.inRange() function gives us the mask having colors within the specified lower and upper range mask = cv2.inRange(hsv_img, lower_orange, upper_orange) #cv2.bitwise_and() function performs bitwise and operation on pixels of original image and mask res = cv2.bitwise_and(img, img, mask=mask) #Displaying all the output images cv2.imshow('Original',img) cv2.imshow('mask',mask) cv2.imshow('res',res) #Waiting for user to press key for exit cv2.waitKey(0) cv2.destroyAllWindows()
Refer the Color Detection Python Project to learn the implementation of OpenCV library
4. Edge Detection
Edges are a sudden change in the brightness of the image. The significant transitions in the brightness of the image are used to calculate the edges in an image. Edge detection is used for various image processing purposes. One of them is to sharpen the images. Sharpening of images is done to make the images more clear. Edge detection is used to enhance the images and image recognition becomes easier.
The canny edge detection algorithm is mostly used to detect the edges in an image.
OpenCV contains the function cv2.Canny() for edge detection.
cv2.Canny(img, 50, 150)
First parameter is the image data, the second parameter is the lower threshold value and the third parameter is the upper threshold value. You need to try different values for tuning the edge detection algorithm.
import cv2 #load image img = cv2.imread('car.jpeg') #resizing image img = cv2.resize(img, (420,300)) #applying canny edge detection algorithm canny = cv2.Canny(img,50,150) #displaying results cv2.imshow('image',img) cv2.imshow('canny',canny) #destroy all windows cv2.waitKey(0) cv2.destroyAllWindows()
In this article, we discussed some of the various techniques that are widely used in all computer vision tasks. We learned to separate the background from the foreground using Thresholding. We saw how we can smoothen our images by blurring the image. We also saw how we can extract a specific color we want from the image. At last, we saw the edge detection technique and implemented these techniques with Python.
Thinking, next what? Work on Gender and Age Detection Python Project with OpenCV
Any other computer vision technique that you would like to add? Do share with us in the comment section.