Image pre-processing is an integral part of computer vision for analyzing the image for almost every application. Usually, Image pre-processing steps are very specific to the use case or application type like objection detection, object segmentation, text extraction, facial recognition, etc. Here in the following post “Image pre-processing using OpenCV (Part-1)”, I will discuss the basic operations on images using OpenCV and some general used for many applications.
Table of content:
- Basic terminology used in Computer Vision
- Reading of an image
- Writing of an image
- Display of an image
- Conversion of a colored image to gray-scale
- Histogram of an image
- Edge Detection
Let’s understand first the basic terminology used in Computer Vision
- Color Space: It is a way to represent color channels in the image. Basically three types of color spaces are there: 1. RGB/BGR 2. HSV 3. CMYK (we will discuss in detail later)
- Hue: A hue is, to what degree, a color is different from Green, Blue or Red.
- Saturation: Image is saturated, if no white light is present in the color of the image.
- Aspect Ratio: Aspect ratio of an image is the ratio of width to height of an object in an image.
- Contrast: A contrast is the difference between lightest and darkest region of the image.
- Edge: Edges are boundaries of objects present in an image. If sudden change occurred in pixel density over a small number of pixels, edge is detected at that place.
- Tone: Tone describes lightness or darkness of the image.
- Morphological Operation: In image processing morphological operation deals with changes in structure of the object present in the image.
- Noise: In image processing, noise means any undesirable change in pixel value(pixel density)
- Pixel: The smallest square unit of an image that gives visual information of that part is called pixel.
- Sharpening: Sharpening is an enhancement technique that focus on details of the image like edge highlight.
As we have understood now the basic terminology used in image processing, let’s check the basic image handling techniques used in python:
Before we proceed further, let’s install OpenCV first if not done yet.
How to install OpenCV in python?
You just need to run the following command in the Jupyter notebook for OpenCV installation.
1. Read an image using OpenCV
2. Display the image you just read
To display an image on a temporary basis, we need to use the cv2.imshow() function. This function helps to display the image in a separate window.
cv2.waitkey() is keyboard binding function. It waits for the user for any key is to be pressed for particular milliseconds. In case ‘0’ is passed as an argument of waitkey(), it means it will wait indefinitely times unless any key is pressed.
DestroyAllWindows() simply destroys all windows if anything is created already.
3. Write an image using OpenCV
We used cv2.imwrite to save the image in the local system. In this case, we need to mention only the name of the image if the image is present in the same directory where we are working, otherwise, we have to specify the whole path.
4. Change colored image to grayscale
Additionally, you can do its reverse operation too i.e. to convert a grayscale image into a colored one.
To change the gray-scale image into a colored one, instead of cv2.COLOR_BGR2GRAY, use cv2.COLOR_GRAY2RGB.
5. Histogram Plotting using openCV
The histogram shows the frequency of the pixels in the image.
It is a plot between pixel values (x-axis) and the number of pixels (y-axis). A histogram shows how color density varies from black pixel (0) to white pixel (255).
Using OpenCV, Histogram is plotted using the following function:
cv.calcHist(input_image, channels, mask, histSize, ranges)
Channel: It represents color i.e. red, blue, or green. For example, if the input is a grayscale image, its value is . For a color image, you can pass , , or  to calculate the histogram of the blue, green, or red channel respectively.
Mask: We use masking if the histogram of any specific region has to be calculated. If the histogram is calculated for the whole image then ‘None’ is passed as the argument.
Range: It represents pixel values.
Where to use histogram?
The histogram helps in analyzing the image. It can help in setting the threshold, adjusting the brightness and contrast. If the histogram of the input image and the output image is known, it’s easy to find out which filter has been applied to the image. A histogram is used for image equalization as well.
6. Edge detection using Canny Algorithm
Canny edge detection is based on:
- gradient of the image i.e. difference between two adjacent pixels
- hysteresis filtering: It selects the lines using those pixels which are different from adjacent ones.
For the Canny edge detection algorithm, we need to provide 3 arguments in the cv2.Canny() function i.e. input image array, the minimum value of a pixel, the maximum value of pixel.
How to set minimum and maximum value i.e. min and max threshold in canny edge detection?
For this usually ‘Otsu thresholding’ is used. But threshold setting can be considered as a hyperparameter that can be adjusted according to the need.
In our case, if I change the upper threshold value from 200 to 500, only zebra lines will be detected.
Do you want to know where to use edge detection?
As shown in the image, edges are representations of the shape of the object present in the image. Once edges are detected, we can use enhancement techniques to a particular region to extract more features.
Extraction of the edges and overlapping it with the original image, sharpen the image.
Thus through this post, we have learned how to read and write images using OpenCV, histogram plotting, and edge detection using the Canny algorithm. Above mentioned steps are considered image pre-processing using OpenCV and are known as basic fundamentals to learn computer vision.
Here I have attached a Github link for code for your reference.
If interested in learning Computer Vision with the guided project, join our upcoming Bootcamp. Here are details: