Search code examples
pythoncomputer-visionaruco

drawMarker() in cv2.aruco not found


I'm trying to use the function drawMarker() from OpenCV documentation in ArucoMarkers and for some reason it keeps giving me an error:

AttributeError: module 'cv2.aruco' has no attribute 'drawMarker'

If it helps, I am using VS code and coding in Python. I'm not sure why I don't have "drawMarker()" in my library.

I'm trying to print aruco markers in a matplotlib plot. This is my code for that.

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Create a predefined dictionary object for ArUco markers
dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)

# Generate an ArUco marker image
marker_id = 23
marker_size = 200
# No need to pass an image to fill
marker_image = cv2.aruco.drawMarker(dictionary, marker_id, marker_size)

# Display the marker using matplotlib
plt.imshow(marker_image, cmap='gray')
plt.axis('off')
plt.show()

This is my version (which I tried to make it as up to date as possible):

/Users/[OMITTED]/Desktop/test/aruco_markers.py 4.8.1

When I ran print(dir(cv2.aruco)):

rtcbhiv01a02:python_gui [OMITTED]$ /usr/local/bin/python3 /Users/[OMITTED]/Desktop/test/aruco_markers.py
['ARUCO_CCW_CENTER', 'ARUCO_CW_TOP_LEFT_CORNER', 'ArucoDetector', 'Board', 'CORNER_REFINE_APRILTAG', 'CORNER_REFINE_CONTOUR', 'CORNER_REFINE_NONE', 'CORNER_REFINE_SUBPIX', 'CharucoBoard', 'CharucoDetector', 'CharucoParameters', 'DICT_4X4_100', 'DICT_4X4_1000', 'DICT_4X4_250', 'DICT_4X4_50', 'DICT_5X5_100', 'DICT_5X5_1000', 'DICT_5X5_250', 'DICT_5X5_50', 'DICT_6X6_100', 'DICT_6X6_1000', 'DICT_6X6_250', 'DICT_6X6_50', 'DICT_7X7_100', 'DICT_7X7_1000', 'DICT_7X7_250', 'DICT_7X7_50', 'DICT_APRILTAG_16H5', 'DICT_APRILTAG_16h5', 'DICT_APRILTAG_25H9', 'DICT_APRILTAG_25h9', 'DICT_APRILTAG_36H10', 'DICT_APRILTAG_36H11', 'DICT_APRILTAG_36h10', 'DICT_APRILTAG_36h11', 'DICT_ARUCO_MIP_36H12', 'DICT_ARUCO_MIP_36h12', 'DICT_ARUCO_ORIGINAL', 'DetectorParameters', 'Dictionary', 'Dictionary_getBitsFromByteList', 'Dictionary_getByteListFromBits', 'EstimateParameters', 'GridBoard', 'RefineParameters', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_native', 'calibrateCameraAruco', 'calibrateCameraArucoExtended', 'calibrateCameraCharuco', 'calibrateCameraCharucoExtended', 'detectCharucoDiamond', 'detectMarkers', 'drawCharucoDiamond', 'drawDetectedCornersCharuco', 'drawDetectedDiamonds', 'drawDetectedMarkers', 'drawPlanarBoard', 'estimatePoseBoard', 'estimatePoseCharucoBoard', 'estimatePoseSingleMarkers', 'extendDictionary', 'generateImageMarker', 'getBoardObjectAndImagePoints', 'getPredefinedDictionary', 'interpolateCornersCharuco', 'refineDetectedMarkers', 'testCharucoCornersCollinear']

Solution

  • OpenCV 4.7.0 brought few changes in API. generateImageMarker() should be used instead of drawMarker().

    Original c++ tutorial was updated with:

    cv::Mat markerImage;
    cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
    cv::aruco::generateImageMarker(dictionary, 23, 200, markerImage, 1);
    cv::imwrite("marker23.png", markerImage);
    

    Corresponding working solution in python:

    import cv2
    
    dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
    marker_id = 23
    marker_size = 200
    marker_image = cv2.aruco.generateImageMarker(dictionary, marker_id, marker_size)
    

    Printing will bring you the desired marker:

    import matplotlib.pyplot as plt
    
    plt.imshow(marker_image, cmap='gray')
    plt.axis('off')
    plt.show()
    

    AcUro #23