Search code examples
pythonopencvaruco

Problem with Aruco markers detection using Python


I try detect Aruco codes on images. I'm working at Jupyter Notebook in Docker, so I working with images, not videos and do not have access to some functions (for example cv2.imshow('QueryImage', QueryImg) )

Based on code: https://github.com/kyle-bersani/opencv-examples/blob/master/SimpleMarkerDetection/DetectMarkersAndPrint.py

I prepare this script:

import numpy
import cv2
import cv2.aruco as aruco
from matplotlib import pyplot as plt

im = cv2.imread('ar3.jpg')

ARUCO_PARAMETERS = aruco.DetectorParameters_create()
# OLD:
# ARUCO_DICT = aruco.Dictionary_get(aruco.DICT_6X6_1000)
# NEW
ARUCO_DICT = aruco.Dictionary_get(aruco.DICT_5X5_1000)

plt.figure(figsize = (20,20))
imgplot = plt.imshow(im, interpolation='nearest')
plt.show() 

im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
corners, ids, rejectedImgPoints = aruco.detectMarkers(im, ARUCO_DICT, parameters=ARUCO_PARAMETERS)

if ids is not None and len(ids) == 5:
    for i, corner in zip(ids, corners):
            print('ID: {}; Corners: {}'.format(i, corner))

    im = aruco.drawDetectedMarkers(im, corners, borderColor=(0, 0, 255))
else:
    print("NONE")
plt.figure(figsize = (20,20))
imgplot = plt.imshow(im, interpolation='nearest')
plt.show() 

And use simple image with markers:

enter image description here

But my code do not see these markers.

Problem is with code, image or markers? Do You have any idea?

Update: I generate markers using this code:

import cv2
import cv2.aruco as aruco

# Create gridboard, which is a set of Aruco markers
# the following call gets a board of markers 5 wide X 7 tall
gridboard = aruco.GridBoard_create(
        markersX=5, 
        markersY=7, 
        markerLength=0.04, 
        markerSeparation=0.01, 
        dictionary=aruco.Dictionary_get(aruco.DICT_5X5_1000))

# Create an image from the gridboard
img = gridboard.draw(outSize=(988, 1400))
cv2.imwrite("test_gridboard.jpg", img)

And I get this image: enter image description here Next I copy markers from this image and paste it into image (as You see at 1st image). Moreover, I replaced Dictionaries:

# OLD:
# ARUCO_DICT = aruco.Dictionary_get(aruco.DICT_6X6_1000)
# NEW
ARUCO_DICT = aruco.Dictionary_get(aruco.DICT_5X5_1000)

But this code still can't detect markers. Moreover any app from Google Play can't detect them too. I test this image with apps:


Solution

  • Now everything working, code after improvements:

    import numpy
    import cv2
    import cv2.aruco as aruco
    from matplotlib import pyplot as plt
    
    im = cv2.imread('2-03.png')
    # im = cv2.imread('1-07.jpg')
    
    ARUCO_PARAMETERS = aruco.DetectorParameters_create()
    ARUCO_DICT = aruco.Dictionary_get(aruco.DICT_5X5_1000)
    
    plt.figure(figsize = (20,20))
    imgplot = plt.imshow(im, interpolation='nearest')
    plt.show() 
    
    im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    corners, ids, rejectedImgPoints = aruco.detectMarkers(im, ARUCO_DICT, parameters=ARUCO_PARAMETERS)
    
    if ids is not None:
        print('detected: {}'.format(len(ids)))
        # for i, corner in zip(ids, corners):
                # print('ID: {}; Corners: {}'.format(i, corner))
    
        im = aruco.drawDetectedMarkers(im, corners, borderColor=(255, 0, 0))
    else:
        print("NONE")
    plt.figure(figsize = (20,20))
    imgplot = plt.imshow(im, interpolation='nearest')
    plt.show()