Understanding img.find_apriltags() for Custom Contour Detection

Hello OpenMV Community,

I’m working on an image processing project using OpenMV and have been experimenting with contour detection. Initially, I used img.find_blobs() for detecting objects, but the contours aren’t as precise as I’d like. While exploring the example scripts, I came across img.find_apriltags(), which produces remarkably accurate contours for AprilTags. This got me curious about how it achieves such robust detection.

Could someone share insights into the methodology behind img.find_apriltags()? Specifically, I’m interested in understanding the techniques it uses for contour detection. If the source code for this function is available, I’d greatly appreciate a pointer to it so I could study and adapt it for my project. If that’s not possible, could you suggest similar approaches or OpenMV functions to achieve high-precision contour detection for custom shapes?

Thank you for your time and help!

Here’s the code: openmv/src/omv/imlib/apriltag.c at master · openmv/openmv · GitHub

It works by doing the following:

  1. The image is thresholded into 3 states. White, black, and grey.
  2. The edge of white and black transitions are found. Edges that are next to each other are added to the same list.
  3. For each point list, the points are sorted by their position radially around their combined centroid.
  4. Quad detection is then done by looking to see if the angle produced by walking the points only increases nominally. The 4 strongest changes in angles over points are remembered as the corners.
  5. The corners are then used to project the quad into a 2D picture which can then be scanned to detect the code, scanning is done against all 4 possible rotations.
  6. The binarized value is then compared against a list of codes… checking the hamming distance, which is the number of bits that are different. The first code with the lowest hamming distance under some threshold is picked.

Regarding what you want. Their method depends on the sharp lines produced by white-black transitions.