Aruco in opencv

So there isn’t great documentation on the python bindings as far as I can find. There are docs on the c++ bindings.  Trying to do this on a mac was a hellish uphill battle, and opencv in the virtual machine has been… hmm actually pretty okay? Well, I did this on my fresh new triple boot ubuntu flash drive.

Invaluable is to go into the python REPL and type

Then you can see what all the available functions are. They’re more or less self explanatory, especially since they are described in the opencv c++ tutorials.

I believe the python bindings are generated programmatically, and they are fairly systematic, but always a touch different from the c++ function calls. A big difference is typically the python calls don’t modify in place.

Anyway, to get you up, I cobbled together some really basic code. It can generate a tag and save it

And this is a basic program to detect the markers

They are sprinkled with the requisite garbage and cruft of me wiggling around with print statements to figure out what everything is.

It sounds like more of what I want is to use Aruco boards. They sound good. I’m looking into using this for maybe robot configuration sensing.


24 thoughts on “Aruco in opencv”

  1. I get the error ‘ImportError: No module named aruco’
    Please give some tips on how to install aruco.
    I use opencv 3.1 with Python 2.7

    1. Hi Raja,

      It’s been a while since I’ve been playing with this and I’ve run all sorts of commands screwing around with my python distro.
      import aruco
      seems to work now and not cv2.aruco. Perhaps this is from a previous attempt to install aruco or perhaps the opencv project organization has changed (I think when I made this post the aruco addition to opencv was pretty fresh and porbably unstable). I believe I did reinstall opencv the other day to play with OpenSfm. I think I might be using aruco for a new project soon, so I’ll make a new post then.

      Edit: Ah. I see. I actually read my own post. I got it working on my ubuntu partition. never mind then.

  2. I have opencv_contrib compiled in ubuntu along with opencv. When I try to run the code to generate a tag, I get the following error –
    Traceback (most recent call last):
    File “”, line 11, in
    aruco_dict = aruco.Dictionary_get(aruco.DICT_5X5_250)
    AttributeError: ‘module’ object has no attribute ‘Dictionary_get’

    How do I resolve this?

  3. I installed opencv3.2 and the issue is fixed. Now, somehow it is not detecting the marker. No errors as such. But marker is not detected.

  4. Hi you all !

    First thank you for your site and the explanations, it is very useful !
    So, I did install successfully opencv and opencv_contrib using ubuntu 14.04 and python3.5.
    I can import the modules cv2 and cv2.aruco succesfully but it seems like the submodue cv2.aruco is empty… I have no function included in the submodule cv2.aruco.
    Is here the output of help(cv2.aruco):

    Help on module cv2.aruco in cv2:


    DICT_4X4_100 = 1
    DICT_4X4_1000 = 3
    DICT_4X4_250 = 2
    DICT_4X4_50 = 0
    DICT_5X5_100 = 5
    DICT_5X5_1000 = 7
    DICT_5X5_250 = 6
    DICT_5X5_50 = 4
    DICT_6X6_100 = 9
    DICT_6X6_1000 = 11
    DICT_6X6_250 = 10
    DICT_6X6_50 = 8
    DICT_7X7_100 = 13
    DICT_7X7_1000 = 15
    DICT_7X7_250 = 14
    DICT_7X7_50 = 12

    Do you have an idea of what’s wrong?
    Thank you so much !


    1. Hi Raphael,

      Your problem really rings a bell. I think that is the state my Mac installation was at before I switched over to Ubuntu. Unfortunately it’s been long enough that my memory is kind of hazy and I’ve since reinstalled my OS on my mac. But if you installed opencv via apt get, I’d suggest installing from source. I think aruco is kind of an off the beaten path submodule.
      You need to use the cmake option
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules
      with that path replaced as appropriate.

      Here are two guides on installing

      I’m not an opencv developer, but you might also want to try posting on stack exchange or an opencv forum or email chain if building from source doesn’t work.

      Also: I’d suggest upgrading to 16.04. It isn’t necessarily a big deal.
      14.04 is getting long in the tooth. I upgraded recently after I spent hours trying to get another installation to work only to find out that the 14.04 version was deprecated and kind of broken

      1. Hi !

        Thank you so much for your answer ! I’ll look at what you sent me to see if I can find something useful !
        I hope to figure it out !!

        Thanks again,


  5. Wow this all worked first try for me! Thanks for writing it up, nice starting point.

    Going to try to render the pose axes on top of this. I’ll post back if/when I get it working.

    FWIW I’m running mac OS 10.13 (17A405), Python 3.6.3 (default, Oct 19 2017, 23:44:15), and cv2 3.3.0-dev and I used the following article to get up/running (kudos to Adrian Roseback for a fantastic write-up!):

    Thanks again,

  6. So here’s quick version that also visualizes pose estimation on the live video:

    import numpy as np
    import cv2
    import cv2.aruco as aruco
    import yaml

    with open(‘calibration.yaml’) as f:
    loadeddict = yaml.load(f)
    camera_matrix = np.asarray(loadeddict.get(‘camera_matrix’))
    dist_coeffs = np.asarray(loadeddict.get(‘dist_coeff’))

    cap = cv2.VideoCapture(0)
    markerLength = 50

    # Capture frame-by-frame
    ret, frame =
    #print(frame.shape) #480×640
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_1000)
    parameters = aruco.DetectorParameters_create()


    ”’ detectMarkers(…)
    detectMarkers(image, dictionary[, corners[, ids[, parameters[, rejectedI
    mgPoints]]]]) -> corners, ids, rejectedImgPoints
    #lists of ids and the corners beloning to each id
    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
    #rvecs, tvecs, objpointsbg = aruco.estimatePoseSingleMarkers(corners,0.05,)
    if ids != None: # if aruco marker detected
    rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners, markerLength, camera_matrix, dist_coeffs)
    gray = aruco.drawAxis(gray, camera_matrix, dist_coeffs, rvec, tvec, 100)

    #It’s working.
    # my problem was that the cellphone put black all around it. The alrogithm
    # depends very much upon finding rectangular black blobs

    gray = aruco.drawDetectedMarkers(gray, corners, ids)

    # Display the resulting frame
    if cv2.waitKey(1) & 0xFF == ord(‘q’):

    # When everything done, release the capture

    Note that it’s a hack currently and only handles a single marker. It crashes if more than one marker is in the scene. Probably tomorrow I’ll see about making it handle multiple markers.

    And here’s what I used for a quick camera calibration:

    Thanks again,

  7. Sir,

    I have a image of 3 markers in it and iam able to identify the corners and rejected ids and ids of the marker but im not able to get the angle of rotation of the marker from the still image.

    and im a beginner sir so please do help me with this sir as im stuck in this part.


  8. I want to know that if i have to detect an image containing multiple aruco marker then what should i have to do in the above code. Also when i am running this by modifying code and passing image in it, i am getting unidentication error .

  9. I have ArUco target detection working. I can derive Marker ID, location in the image, distance to the target. What I am looking for is how to find how many degrees is the target rotated about each axis.

  10. Hey, thank you for this. This, along with a few other tutorials got my aruco detection working and on point. Really appreciate the effort that you put into this.

  11. Hy,
    I am pretty new to python. What does this below mean? I mean, how should i give the inputs for this detectMarkers() function with those square brackets?
    detectMarkers(image, dictionary[, corners[, ids[,parameters[,rejectedImgPoints]]]]) -> corners, ids, rejectedImgPoints

    1. I’m not entirely sure, but I think they are demarking optional arguments. image and dictionary are not options, but everything after is

  12. Hey, great code! Helped me so much in my project. Thanks a lot!

    But there is one more problem i have to solve: how can i save the corner dates as return values to give it to another function in my script?

    Cheers! Johannes

Leave a Reply

Your email address will not be published. Required fields are marked *