As part of this project, we are manipulating the connections between several turtle ant nests within an arena, to observe how this influences which nests the colony chooses. To monitor the ants' behavior, we currently use Raspberry Pis set up to record footage of the nests. Trying to track all of the ants by having people watch the footage would be very tedious because of the large quantity of videos produced. Every day, our experiments produce 16 hours of video footage. I don't know about you, but I'd prefer to not spend 16 hours per day in front of a computer, looking at videos of ants.
Because we're interested how the ants choose nests, we wish to measure how ants move along paths to each nest, as well as at key decision points. Therefore, we isolate these locations (Regions of Interest) within the video frame and have the computer pipeline process only those regions of the video. However, with the large amounts of video the experiment is making, marking the Regions of Interest (ROIs) manually in every video would be very tedious and take a long time. So I set about trying to automate turning the raw footage of the colony, which looks like this:
![]() |
| Figure 1: A frame of video from one experiment manipulating the connections between ant nests (red tubes in blue bases). |
![]() |
| Figure 2: The regions of interest, manually labeled |
The first thing I had to do was to teach the computer what red is. This task was much harder than I anticipated it being, because the actual values of the pixels in the red regions can vary quite a bit based on the lighting, and the classic RGB color model, which is how videos are recorded and computers process footage, does not reflect how people view colors.
My first task was to represent the image in a way which made it easier for the computer to identify different colors. After doing a lot of research (there are a lot of different color models to choose from), I ended up settling on the HSV Color Model:
![]() |
| Figure 3: The Hue-Saturation-Value color wheel |
The main advantages that this model offered was that it has a simple representation, and each of the three components corresponds to an aspect of the color which people can intuitively grasp: the hue encodes what color it is, the value encodes how bright the color is, and the saturation encodes how pure the color is.
However, even with this improved color model, selecting by just pixel values results in an image like this:
![]() |
| Figure 4: The pixels from the video frame which are red |
and this has several problems which must be overcome before it can be used. The first problem is that the red polygons we want to draw contain some holes which would prevent the code from recognizing that these objects are continuous polygons. In order to prevent this, I turned to morphological operations. Arya Massarat wrote a post explaining them, a few months ago, which explains both what they are and their use in tracking ants. By using morphological operations to fill in those random holes in the polygons, we could get to this image:
![]() |
| Figure 5: The result of closing holes with morphological operations |
![]() |
| Figure 6: The contours detected from the above mask |
![]() |
| Figure 7: The skeleton of the mask from above |
Lastly, the approximated polygons get converted into a convex hull to reduce inaccuracies in the above algorithm, which sometimes introduces unwanted indents into the polygons. OpenCV has a function which is supposed to convert polygons to their convex hulls. However, the algorithm they implement was proven to not work for all polygons, so I had to search the literature for an algorithm which works, and then I had to write my own code to implement it.
And here's the final result of the algorithm:
![]() |
| Figure 8: The final detected polygons |
which, when overlaid onto the original image, gets us this result:
![]() |
| Figure 9: The final image, with regions of interest automatically labeled |
Further Reading:
Joblove and Greenberg, 1978. "Color spaces for computer graphics." https://doi.org/10.1145%2F965139.807362Lee, 1983. "On finding the convex hull of a simple polygon." https://doi.org/10.1007/BF00993195
Ramer, 1972. "An iterative procedure for the polygonal approximation of plane curves." https://doi.org/10.1016/S0146-664X(72)80017-0
Saha et al., 2016. "A survey on skeletonization algorithms and their applications." https://sci-hub.tw/10.1016/j.patrec.2015.04.006
Suzuki and Abe, 1983. "Topological structural analysis of digitized binary images by border following." https://doi.org/10.1016/0734-189X(85)90016-7
Toussaint and Gindy, 1983. "A counterexample to an algorithm for computing monotone hulls of simple polygons." https://doi.org/10.1016/0167-8655(83)90028-4
Image Credits:
[1]: This image is a frame taken from a video recording of an experimental setup in the lab.[2,4,5,6,7,8,9]: These images were made by using pieces of the pipeline to process figure 1.
[3]: Image from Wikipedia user SharkD (source, CC BY-SA 3.0 license)









No comments:
Post a Comment