Frame differencing gives us some useful information, but we cannot use it to build anything meaningful. In order to build a good object tracker, we need to understand what characteristics can be used to make our tracking robust and accurate. So, let's take a step in that direction and see how we can use colorspaces to come up with a good tracker. As we have discussed in previous chapters, HSVcolorspace is very informative when it comes to human perception. We can convert an image to the HSV space, and then use colorspacethresholding
to track a given object.
Consider the following frame in the video:
If you run it through the colorspace filter and track the object, you will see something like this:
As we can see here, our tracker recognizes a particular object in the video, based on the color characteristics. In order to use this tracker, we need to know the color distribution of our target object. Following is the code:
import cv2 import numpy as np # Capture the input frame from webcam def get_frame(cap, scaling_factor): # Capture the frame from video capture object ret, frame = cap.read() # Resize the input frame frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) return frame if __name__=='__main__': cap = cv2.VideoCapture(0) scaling_factor = 0.5 # Iterate until the user presses ESC key while True: frame = get_frame(cap, scaling_factor) # Convert the HSV colorspace hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # Define 'blue' range in HSV colorspace lower = np.array([60,100,100]) upper = np.array([180,255,255]) # Threshold the HSV image to get only blue color mask = cv2.inRange(hsv, lower, upper) # Bitwise-AND mask and original image res = cv2.bitwise_and(frame, frame, mask=mask) res = cv2.medianBlur(res, 5) cv2.imshow('Original image', frame) cv2.imshow('Color Detector', res) # Check if the user pressed ESC key c = cv2.waitKey(5) if c == 27: break cv2.destroyAllWindows()