In our robotics lab at UT Austin, we often use augmented reality (AR) tags to determine the position and orientation of an object. One good ROS package we use for tracking markers is `ar_track_alvar`, which can track ArUco-style markers (shown below) and calculate their 6D pose. The package makes detecting AR tags as easy as running a `roslaunch` file (with some slight configuration tweaks).

But as we started placing AR tags on every item in our mock nuclear storage vault, we asked: just how many possible AR tag permutations are possible? If we want to put a unique tag on 10,000 different items, how large does the tag have to be? Is 4×4 enough? 5×5? And so began my mini math-quest for the answer.

## Problem statement

We want to find the number of unique, identifiable AR tags. AR tags are a square grid of cells which can be either black or white. The entire tag has a black border, which we will ignore when calculating tag size. The picture below shows 18 unique 6×6 tags. A tag may not be rotationally symmetric with itself, nor can it be mirror-symmetric with itself. This requirement ensures that the algorithm can always calculate an unambiguous 6DOF pose for the marker, and it’s also what makes it tricky to come up with a formula for the number of possible tags for a given size. Also, note that for odd-numbered sizes (like 5×5), there is a single middle square. In even-numbered sizes, there is no single center square. This means that the formulas for the odd-numbered and even-numbered sizes will be slightly different.
Solution

We want a formula for the number of unique orientable AR tags that can be represented by an n x n grid of black-and-white squares (each square can store one bit). The easiest way to derive a formula is to look at the odd- and even-numbered sizes separately.

## Even

First, calculate the number of squares in the tag’s grid. This is easy, it’s n2. Now, the number of possible combinations of black and white bits in the tag is 2n2. Simple so far, right?

Now we have to subtract off the mirror-symmetric tags. For each possible half of the tag (let’s call it A), there is a full tag that has a top half A and a bottom half that is the mirror image of A. So we need to subtract one for each possible upper half of the tag. The number of possible upper halves is 2(n2)/2. I’m going to switch to equations to save your eyes. The new formula is

` `

Finally we have to take out rotational symmetry. For each possible non-mirror symmetric correct AR tag, there are 3 more (90, 180, and 270 degree rotations) which look exactly the same, but rotated. A robot can’t tell the difference between a tag that’s rotated 90 degrees and a tag that looks like another tag rotated 90 degrees, so all the rotations will have to go. Just divide by 4. Equation 2: The number of unique nxn AR tags where n is even.

We’re done now, right? Right?

## Odd

Nope, we forgot about odd-numbered sizes, like 5×5! Thankfully, this one is almost exactly the same. We just have to think about the middle square when looking at the “half-tags.” Equation 2: The number of unique nxn AR tags where n is odd.

Finally, we can do some fancy math using (-1)n to combine the odd and even formulas into a single formula! My fellow researcher Ben Ebersole already did this for you, and also cleaned up some of the fractions.

## Result

For an n x n AR tag (disregarding the black border), the number of unique AR tags suitable for 6DOF pose estimation is given by

`permutations(n) = 2^(n^2-2)-2^((2*n^2-(-1)^n-7)/4)`

Or if you just want to use a lookup table, here are the permutations for common sizes:
nxn    Permutations
2×2    3
3×3    120
4×4    16,320
5×5    8,386,560

Special thanks to Dr. Scott Niekum and Ben Ebersole.