Custom ArUco Markers in Python

It is very probable if you work on computer vision projects to cross paths with OpenCV. It is “a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel…”

For different projects I have used its ArUco module for object tracking, pose estimation and perspective transformation. While the online documentation is very rich, its Python bindings can be found much less covered. To get started with basic ArUco tracking using OpenCV and Python, I recommend checking this clear and to-the-point example by Dr. Philip Zucker.

For whatever reason, you might want to use specific markers, instead of the randomly-generated combinations offered by the module. While the C++ example in the documentation is quite straightforward, I wasn’t able to find a Python equivalent so I thought I should figure it out and share my result.

As in the example above, after importing necessary modules, we start by creating our dictionary. In this case it is a custom empty dictionary:

# define an empty custom dictionary for markers of size 4
aruco_dict = aruco.custom_dictionary(0, 4, 1)

Then, we need to create a three-dimensional array in the bytesList object inside the aruco dictionary:

# add empty bytesList array to fill with 3 markers later
aruco_dict.bytesList = np.empty(shape = (3, 2, 4), dtype = np.uint8)

Now, we can create our markers as arrays with the bits that represent black (0) or white (1) parts of the marker:

# add new marker(s)
mybits = np.array([[1,1,1,1],[1,0,0,0],[1,1,1,0],[1,0,0,0]], dtype = np.uint8)

Finally, we can add the marker’s bytes to the bytesList object:

aruco_dict.bytesList[0] = aruco.Dictionary_getByteListFromBits(mybits)

From here on, our program can go on just like in the example and it can detect our new markers just like other ArUco markers. You can find the full script for this program on GitHub, which adds all three markers to the dictionary and you can test It directly using a webcam.

If you have any question or would like to use markers in your project, feel free to drop me a line using this form or on social media links below.