Ant tracking — given reasonable lighting conditions, it’s a trivial task for humans. You can easily locate a small dark shape and follow its movements with your eyes, and I bet you don’t think too much about how.
On the other hand, unless you want to spend hours watching ants, automation is necessary. Thus, this semester, my lab partner and I worked on developing an ant tracker with OpenCV. Given a video of an ant on a tree structure, could we correctly track the junctions that the ant crosses and what turns it takes? In this post, I cover an algorithm for the first step in tracking: detection with OpenCV. The detector takes in a video frame as input and outputs the center coordinates of detected objects. (You can view the code implementation here!)
We begin by converting the frame to grayscale: as RGB information is irrelevant for tracking purposes, we won’t carry around 3 channels (which is more computationally expensive) when we only need 1.
Next, we perform background subtraction to remove all but the moving ant (the foreground) from the frame. We use OpenCV’s BackgroundSubtractorMOG2, which computes an initial model of the background, and updates the model according to pixel differences between the model and the following frames. Basically, the longer a pixel stays constant, the more likely it is to be part of the background.
- Gaussian filtering to smooth images and remove noise;
- Sobel filtering to compute edge gradients and directions;
- non-maximum suppression to remove non-edge pixels and leave “thin edges”; and
- hysteresis thresholding to determine which edges are true edges.
We perform an additional thresholding step to retain only edges with a pixel intensity below a specified threshold. Pixels within the threshold are set to 255 (the maximum pixel intensity); the rest are set to 0.
Edge detection is followed by contour detection. Contours – essentially, the borders of the objects in the frame – are detected with OpenCV’s findContours, which looks for significant differences in pixel intensity. As we binarized our image with the previous edge detection, this step is fairly straightforward.
Finally, we use contours to determine the position of the ant. We iterate over all contours detected in the frame. For each contour, we compute its minimum enclosing circle. If the circle is too big or too small, we disregard it as noise. Otherwise, we assume this contour represents an ant! The centroid of the circle is taken as the current (x,y) position of the ant.
Ta-da! We have now located an ant. And if we repeat this process for all frames of a video, we can build a rudimentary tracker that simply keeps track of all (x,y) positions detected.
Of course, things get more complicated when multiple ants enter the frame and begin to overlap. How should the computer distinguish between two ants? What if one ant is too close to another? Check out Jingnan’s post on the next step, tracking itself.
Further reading:
Ayush Gupta. “Getting Started With Object Tracking Using OpenCV.” AnalyticsVidhya, 4 Aug. 2021, https://www.analyticsvidhya.com/blog/2021/08/getting-started-with-object-tracking-using-opencv/.
Media credits:
All images by the author.


.png)
.png)

No comments:
Post a Comment