opencv 无法使用python检测aruco

yvfmudvl  于 2023-04-21  发布在  Python
关注(0)|答案(1)|浏览(178)

我正在使用Python中的Aruco,并且能够使用aruco.drawMarker函数生成单个aruco标记,但当我试图检测相同时,我无法做到这一点。markerCorners列表为空,我得到了两个被拒绝的候选人。请参阅下面的代码和图片以了解详细信息。

标记生成代码

import numpy as np
import cv2 as cv
import cv2.aruco as aruco
dictionary = cv.aruco.Dictionary_get(cv.aruco.DICT_6X6_250)

markerImage = np.zeros((200, 200), dtype=np.uint8)

markerImage = cv.aruco.drawMarker(dictionary, 34, 200, markerImage, 1)

cv.imwrite("marker34.png", markerImage

标记检测码

import cv2 as cv
import cv2.aruco as aruco
import numpy as np
dictionary = aruco.Dictionary_get(aruco.DICT_6X6_250)
print(dictionary)
parameters = cv.aruco.DetectorParameters_create()
print(parameters.adaptiveThreshWinSizeMax)
frame = cv.imread("marker34.png")

markerCorners, markerIds, rejectedCandidates = cv.aruco.detectMarkers(
    frame, dictionary, parameters=parameters)

print(rejectedCandidates)
print(markerCorners)
# draw circle in opencv
for rect in rejectedCandidates:
    for points in rect:
        for point in points:
            print(point)
            cv.circle(frame, (int(point[0]), int(
                point[1])), 5, (0, 0, 255), -1)

cv.ims("frame", frame)
cv.waitKey(0)

以上代码的输出

<aruco_Dictionary 0x7fd7b35caa50>
23
[array([[[ 99., 150.],
        [124., 149.],
        [125., 174.],
        [100., 175.]]], dtype=float32), array([[[ 74.,  25.],
        [ 99.,  24.],
        [100.,  49.],
        [ 75.,  50.]]], dtype=float32)]
[]
[ 99. 150.]
[124. 149.]
[125. 174.]
[100. 175.]
[74. 25.]
[99. 24.]
[100.  49.]
[75. 50.]

当我绘制被拒绝的点时,我得到这个imagex 1c 1d 1x
有没有人可以帮我找出我的错误?提前感谢!

goqiplq2

goqiplq21#

如前所述,您需要在标记周围添加白色填充,以便可以检测到它。
为了完整起见,这可以很容易地通过np.pad并选择适当的宽度来完成:

import numpy as np
import cv2

image_filename = "marker34.png"
frame = cv2.imread(image_filename, cv2.IMREAD_GRAYSCALE)
frame = np.pad(frame, pad_width=100, constant_values=255)

相关问题