Image pre-processing through OpenCV (Part 4)

Read Time: 3 min

In last post, we discussed some image processing techniques using opencv like denoising, edge detection, histogram etc. Here I have explained color filtering, contour creation and drawing of geometric shapes on image.

You can access previous posts from following links:

Table of Content:

  • Color Filtering
  • Contour Creation and finding it’s co-ordinates
  • Drawing geometric shapes

Image pre-processing using Color Filtering

It means identify and separate out a region or object in the image based on color. Color filtering is kind of segmentation technique using threshold function. Threshold function is based on color intensity.

Use of color filtering in image processing

To understand color filtering, we need to first understand color space.

Color space defines the representation that image is made up of which type of combinations of colors.

Two mainly used color spaces are as follows:

  1. RGB/BGR: This color space follows that an image is comprised using three colors i.e. Red, Green, Blue
  2. HSV: This color space follows the convention that an image is comprised using Hue, Saturation and value.

Using color filtering process we can extract object or region of particular color.

Let’s check how:

img = cv2.imread('zebra.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) 
      
# Threshold of blue in HSV space 
lower_white = np.array([0, 0, 230])
upper_white = np.array([180, 25, 255])

  
mask = cv2.inRange(hsv, lower_white, upper_white) 
result = cv2.bitwise_and(img, img,mask = mask) 

plt.imshow(result) 

Contour creation

Coutour is basically an outline that defines shape of the object. By contour creation usually object is differentiated from rest of the image.

It is used in image segmentation, creating bounding boxes for object detection.

Let’s understand how to create contour using openCV:

image = cv2.imread('zebra.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
edge = cv2.Canny(gray, 50, 400) 

contours, hierarchy = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  
print("Number of Contours found = " + str(len(contours))) 

contour = cv2.drawContours(image, contours, -1, (0, 255, 0), 1) 

plt.figure(figsize=(20,10))
plt.subplot(121),plt.imshow(edge),plt.title('Output1')
plt.subplot(122),plt.imshow(contour),plt.title('Output2')
plt.show() 

Geometric shapes

  • Drawing a line
  • Drawing an arrow line
  • Drawing a circle
  • Drawing an ellipse
  • Drawing an rectangle

Line drawing

It can be used to create an stroke on image.

img1 = cv2.imread('zebra.jpg')
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
start_point = (150, 100) 
end_point = (250, 200) 
color = (16, 180, 25) #any value can be given according to requirement
thickness = 5
image_line = cv2.line(img1, start_point, end_point, color, thickness) 
plt.imshow(image_line)

Drawing an arrow line

Drawing an ellipse

Drawing an circle

image = cv2.imread('zebra.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
center_coordinates = (120, 50) 
radius = 50
color = (255, 0, 0) 
thickness = 2
image_circle = cv2.circle(image, center_coordinates, radius, color, thickness) 
plt.imshow(image_circle)

Drawing an rectangle

image = cv2.imread('zebra.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
start_point = (75, 10) 
end_point = (275, 200) 
color = (100, 100, 40) 
thickness = 2
image_rect = cv2.rectangle(image, start_point, end_point, color, thickness) 
plt.imshow(image_rect)

You can get code from my GitHub Link:

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