使用 Python 检测 Aruco 标记的问题

Problem with Aruco markers detection using Python

我尝试检测图像上的 Aruco 代码。我在 Docker 的 Jupyter Notebook 工作,所以我使用的是图像,而不是视频,并且无法访问某些功能(例如 cv2.imshow('QueryImage', QueryImg)

基于代码:https://github.com/kyle-bersani/opencv-examples/blob/master/SimpleMarkerDetection/DetectMarkersAndPrint.py

我准备这个脚本:

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() 

并使用带有标记的简单图像:

但是我的代码看不到这些标记。

代码、图像或标记有问题?你有什么想法吗?

更新: 我使用此代码生成标记:

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)

我得到了这张图片: 接下来,我从该图像复制标记并将其粘贴到图像中(如您在第一张图像中看到的那样)。此外,我更换了字典:

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

但是这段代码仍然无法检测到标记。此外,Google Play 中的任何应用也无法检测到它们。 我用应用程序测试这张图片:

现在一切正常,改进后的代码:

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()