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.

http://docs.opencv.org/3.1.0/d9/d6d/tutorial_table_of_content_aruco.html

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.

 

13 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 “gen.py”, 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:

    NAME
    cv2.aruco

    DATA
    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
    DICT_ARUCO_ORIGINAL = 16
    :
    **********

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

    Raphael

    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
      http://docs.opencv.org/trunk/d7/d9f/tutorial_linux_install.html
      http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

      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,

        Raphael

  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!):

    https://www.pyimagesearch.com/2016/12/05/macos-install-opencv-3-and-python-3-5/

    Thanks again,
    Brandon

  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

    while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    #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()

    #print(parameters)

    ”’ 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)
    print(corners)
    #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)

    #print(rejectedImgPoints)
    # Display the resulting frame
    cv2.imshow(‘frame’,gray)
    if cv2.waitKey(1) & 0xFF == ord(‘q’):
    break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()


    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:
    https://github.com/LongerVision/OpenCV_Examples/blob/master/01_internal_camera_calibration/chessboard.py

    Thanks again,
    Brandon

Leave a Reply

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