Tracking in real time based on color
Let's study a real-life application of this concept. In the HSV format, it's much easier to recognize the color range. If we need to track a specific color object, we will need to define a color range in HSV and then convert the captured image in the HSV format and check whether the part of that image falls within the HSV color range of our interest. We can use the cv2.inRange()
function to achieve this. This function takes an image, the upper and lower bounds of the colors, and then it checks the range criteria for each pixel. If the pixel value falls in the given color range, then the corresponding pixel in the output image is 0; otherwise, it is 255, thus creating a binary mask. We can use bitwise_and()
to extract the color range we're interested in using this binary mask thereafter. Take a look at the following code to understand this concept:
import numpy as np import cv2 cam = cv2.VideoCapture(0) while (True): ret, frame = cam.read() hsv ...