Image Pre-processing using openCV (Part-1)

Image pre-processing using opencv Part 1
Read Time: 5 min

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 use case or application type like objection detection, object segmentation, text extraction, facial recognition etc. Here in following post “Image pre-processing using openCV (Part-1)”, I will discuss basic operations on image using openCV and some techniques that are very general and can be used for many applications.

Table of content:

  • Basic terminology used in image processing
  • 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

In the first place, let’s check out basic terms we often use to deal with images:

  1. 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)
  2. Hue: To what degree, a color is different from Green, Blue or Red, is called hue.
  3. Saturation: If no white light is present in the color, image is called saturated.
  4. Aspect Ratio: Ratio of width to height of an object in an image is called aspect ratio.
  5. Contrast: Difference between lightest and darkest region of the image, is called contrast.
  6. 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.
  7. Tone: Tone describes lightness or darkness of the image.
  8. Morphological Operation: In image processing morphological operation deals with changes in structure of the object present in the image.
  9. Noise: In image processing, noise means any undesirable change in pixel value(pixel density)
  10. Pixel: The smallest square unit of an image that gives visual information of that part is called pixel.
  11. Sharpening: Sharpening is an enhancement technique that focus on details of the image like edge highlight.

Let’s check basic image handling techniques:

Installation of openCV

Run the following command in jupyter notebook to install openCV

!pip install opencv-python

Read an image using OpenCV

img = cv2.imread('Zebra.jpg') 
Output:

Display an image

Using openCV:

To display an image on temporary basis, cv2.imshow() function is used. Through this function, image is displayed in 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 argument of waitkey(), it means it will wait indefinitely times unless any key is pressed.

DestroyAllWindows() simply destroys all windows that have been created to display the image.

cv2.imshow('zebra',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

Display the image using matplotlib:

plt.figure(figsize = (30,6))
plt.imshow(img)

Write an image using OpenCV

cv2.imwrite is used to save the image in local system. In this case, only name of the image is mentioned as it is saved in same directory otherwise whole path should be given.

cv2.imwrite('zebra.png',img) 

Change colored image to grayscale

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Output:

Additionally, it’s reverse can also be done i.e. grayscale image can be converted into colored one.

To change the gray-scale image into colored one, instead of cv2.COLOR_BGR2GRAY, cv2.COLOR_GRAY2RGB can be used.

Histogram Plotting using openCV

Histogram shows frequency of the pixels in the image.

It is a plot between pixel values (x axis) and number of pixels (y axis). Histogram shows how color density varies from black pixel (0) to white pixel (255).

Using openCV, Histogram is plotted using following function:

cv.calcHist(input_image, channels, mask, histSize, ranges[])

Parameters:

Channel: It represents color i.e. red, blue or green. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively.

Mask: It is used if histogram of any specific region is to be calculated. If histogram is calculated for whole image then ‘None’ is passed as the argument.

Range: It represents pixel values.

img = cv.imread('zebra.jpg',0)
hist = cv.calcHist([img],[0],'None',[256],[0,256])
plt.hist(img.ravel(),256,[0,256])

plt.show()
Output:

Where to use histogram:

Histogram helps in analyzing the image. It can help in setting the threshold, adjusting the brightness and contrast. If histogram of input image and output image is known, it can be find out which filter has been applied on image. Histogram is used for image equalization as well.

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 Canny edge detection algorithm, we need to provide 3 arguments in the cv2.Canny() function i.e. input image array, minimum value of pixel, maximum value of pixel needed.

edges = cv2.Canny(img,100,200)
plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

plt.show()
Output:

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 hyper parameter which can be adjusted according to the need.

In our case, if I change upper threshold value from 200 to 500, only zebra lines will be detected.

edges = cv2.Canny(img,100,500)
plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

plt.show()
Output:

Where to use edge detection:

As shown in image, edges are representation on 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, sharpens the image.

Summary:

Thus through this post we have learnt how to read and write image using openCV, histogram plotting and edge detection using Canny algorithm.

Here I have attached github link for code.

https://github.com/Appiiee/Image-Pre-processing-using-OpenCV

In my next posts, I have explained other pre-processing techniques as well. You can check those posts from here: