Object Tracking
This scripts allow to track a specific object on real time, we draw a circul on thata object and start to move following this object.
Enter into python virtual environment:
# workon cvNow the first thing we need to detect and determine is the object that we want to be tracked. So for that we need to execute:
# python objectdata.py --filter HSV --webcamThis is going to bring us a 3 different windows. The firs windows show our camera, the second a black & white camera and the third one is a controller that we could adjust so we could leave the object that we want in white and the rest of the scenario on black.
Get Object Data script
Now we are going to explain the code.
We need to import the libraries that we are going to use.
import cv2
import argparse
from operator import xorThen we define the controllers so we can edit and modificate the object that we want to track
def setup_trackbars(range_filter):
cv2.namedWindow("Object detector data", 0)
for i in ["MIN", "MAX"]:
v = 0 if i == "MIN" else 255
for j in range_filter:
cv2.createTrackbar("%s_%s" % (j, i), "Object detector data", v, 255, callback)On get_arguments function we define the arguments that we want that the user need to introduces, such as what filter want to use (RGB or HSV)
Then at the main functions we :
Read the arguments
Get the filter that we are going to apply
Open the camera
Set up the sliders
Start a loop so the filters can be apply at the image of the webcam, converting our image to a new one with the filters apply.
We need to get that our object get in white and the rest on black.
Object Tracking script
As we mention this script allow that an image could be tracked.
Enter into python virtual enviroment:
To execute this code we need to run the next command:
Explaining the code:
We need to import the libraries that we are going to use.
We define the parameters that we want to user introduces at args
Define the RGB upper and lower data that we previously get from the first program.
Initialize the list of tracked points, the frame counter and the coordinates deltas
Initialice the camera and start getting the frames
Resize the frame and convert it to HSV, then we constuct a mask for the object, a series of dilatations and erosions to remove any blobs left
Then we find the contours in the mask that we create.
Validate if at least one contours exist, then get the larges contour in the mask and then use it to get minium enclosing circle.
Loop over the set of tracked points, if either of the tracked points are none, ignore them, otherwise compute the thickness of the line and draw the connecting lines.
Last updated
Was this helpful?