Color based object detection

Intoduction

Object recognition is one of the frequently needed and challenging task in the field of robotics and computer vision. It involves finding instances of real worled objects such as cars, pedestrains, traffic signals etc. In the current era for making such real time applications it requires very robust, efficient image processing algorithms and computing resources.

Object recognition is based on multiple objects features like color, texture, shape, gradients etc. Color plays a significant role in defining an object and hence its detection.

In this tutorial we will use color information to detect ball in the below image and draw a bounding rectangle around it.

Demo Image
Before we proceed further lets have a quick look at most used color channels used to read and write images in digital formats. RGB and HSV color spaces are most used for digital image data storages and manipulation.

Images in digtal formats consists of pixels arrange in 2/3 dimensional matrix depending on the type of color space chose.

In RGB color spaces each pixel consists of three components that is RED, GREEN and BLUE channels which together can form all human visible colors.

Similar to RGB color space, in HSV color space each pixel is also consists of three components that is Hue, Saturation and Value. Hue represents the color value, Saturation represents the amount to which that respective color is mixed with white and Value(Intensity) represents the amount to which that respective color is mixed with black.

As we can see here that unlike RGB, HSV separates color information of image from intensity, hence HSV color space gets more usefull for object detection in our scenario.

First we will import the opencv module and load the image in RGB format and convert it to HSV format.

      
        import cv2
        import numpy as np
        image = cv2.imread("/Images/temp.jpg")
        #rgb to HSV color spave conversion
        hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      
    

Now we will define the Hue range for the object color we want to filter

      
        HSV_lower = np.array([22, 50, 50], np.uint8) #Lower HSV value
        HSV_upper = np.array([30, 250, 250], np.uint8) #Upper HSV value
        frame_threshed = cv2.inRange(hsv_img, HSV_lower, HSV_upper)
      
    

Next we will simply find all the contour of masked image above and will remove the contour around object of interest leaving the contour around noises.

      
        # find connected components
        _, contours, hierarchy, = cv2.findContours(frame_threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

        # Draw contours around filtered objects
        thresh = 100 # Noise removal threshold
        OutputImg = img.copy()

        for cnt in contours:
          # remove noise objects having contour length threshold value
          if len(cnt) > thresh:
            cv2.drawContours(OutputImg, [cnt], 0, (0,0,255), 2)

        cv2.imshow('Object detection', OutputImg)
        cv2.waitKey(0)
      
    

Below is our detected object in the given image.

Output Image

Here is our final implementation what we have done so far-

      
        import cv2
        import numpy as np

        image = cv2.imread("/Images/temp.jpg")
        #rgb to HSV color spave conversion
        hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 

        HSV_lower = np.array([22, 50, 50], np.uint8) #Lower HSV value
        HSV_upper = np.array([30, 250, 250], np.uint8) #Upper HSV value

        frame_threshed = cv2.inRange(hsv_img, HSV_lower, HSV_upper)

        # find connected components
        _, contours, hierarchy, = cv2.findContours(frame_threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

        # Draw contours around filtered objects
        thresh = 100 # Noise removal threshold
        OutputImg = img.copy()

        for cnt in contours:
          # remove noise objects having contour length threshold value
          if len(cnt) > thresh:
            cv2.drawContours(OutputImg, [cnt], 0, (0,0,255), 2)

        cv2.imshow('Object detection', OutputImg)
        cv2.waitKey(0)
      
    

Recently Added Tutorial