OpenCV aruco的生成
import cv2 as cv
import numpy as np
if __name__ == '__main__':
# Load the predefined dictionary
dictionary = cv.aruco.Dictionary_get(cv.aruco.DICT_6X6_250)
# Generate the marker
imghdr=[]
for i in range(4):
markerImage = np.zeros((200, 200), dtype=np.uint8)
markerImage = cv.aruco.drawMarker(dictionary, i, 200, markerImage, 1);
imghdr.append(markerImage)
cv.imwrite(str(i)+".png", markerImage);
有可能出现模块不存在的情况
安装OpenCV contrib包
pip install opencv-contrib-python
如果还是出现包不存在的问题
原因最新的OpenCV好像更改了使用方法
下载4.6.0.66版本实测可用,不必和opencv版本对应
标题 aruco的检测
import imutils
import cv2
import sys
# 构造参数解析器并解析参数
# 定义 OpenCV 支持的每个可能的 ArUco 标签的名称
ARUCO_DICT = {"DICT_4X4_50": cv2.aruco.DICT_4X4_50, "DICT_4X4_100": cv2.aruco.DICT_4X4_100,
"DICT_4X4_250": cv2.aruco.DICT_4X4_250, "DICT_4X4_1000": cv2.aruco.DICT_4X4_1000,
"DICT_5X5_50": cv2.aruco.DICT_5X5_50, "DICT_5X5_100": cv2.aruco.DICT_5X5_100,
"DICT_5X5_250": cv2.aruco.DICT_5X5_250, "DICT_5X5_1000": cv2.aruco.DICT_5X5_1000,
"DICT_6X6_50": cv2.aruco.DICT_6X6_50, "DICT_6X6_100": cv2.aruco.DICT_6X6_100,
"DICT_6X6_250": cv2.aruco.DICT_6X6_250, "DICT_6X6_1000": cv2.aruco.DICT_6X6_1000,
"DICT_7X7_50": cv2.aruco.DICT_7X7_50, "DICT_7X7_100": cv2.aruco.DICT_7X7_100,
"DICT_7X7_250": cv2.aruco.DICT_7X7_250, "DICT_7X7_1000": cv2.aruco.DICT_7X7_1000,
"DICT_ARUCO_ORIGINAL": cv2.aruco.DICT_ARUCO_ORIGINAL,
"DICT_APRILTAG_16h5": cv2.aruco.DICT_APRILTAG_16h5,
"DICT_APRILTAG_25h9": cv2.aruco.DICT_APRILTAG_25h9,
"DICT_APRILTAG_36h10": cv2.aruco.DICT_APRILTAG_36h10,
"DICT_APRILTAG_36h11": cv2.aruco.DICT_APRILTAG_36h11}
image = cv2.imread('19.jpg')
image = imutils.resize(image, width=600)
corners=[]
ids=[]
rejected=[]
for key,value in ARUCO_DICT.items():
arucoDict = cv2.aruco.Dictionary_get(value)
arucoParams = cv2.aruco.DetectorParameters_create()
(corners, ids, rejected) = cv2.aruco.detectMarkers(image, arucoDict, parameters=arucoParams)
if len(corners) > 0:
break
# 验证至少一个 ArUCo 标记被检测到
if len(corners) > 0:
# 展平 ArUCo ID 列表
ids = ids.flatten()
# 循环检测到的 ArUCo 标记
for (markerCorner, markerID) in zip(corners, ids):
# 提取始终按以下顺序返回的标记:
# TOP-LEFT, TOP-RIGHT, BOTTOM-RIGHT, BOTTOM-LEFT
corners = markerCorner.reshape((4, 2))
(topLeft, topRight, bottomRight, bottomLeft) = corners
# 将每个 (x, y) 坐标对转换为整数
topRight = (int(topRight[0]), int(topRight[1]))
bottomRight = (int(bottomRight[0]), int(bottomRight[1]))
bottomLeft = (int(bottomLeft[0]), int(bottomLeft[1]))
topLeft = (int(topLeft[0]), int(topLeft[1]))
# 绘制ArUCo检测的边界框
cv2.line(image, topLeft, topRight, (0, 255, 0), 2)
cv2.line(image, topRight, bottomRight, (0, 255, 0), 2)
cv2.line(image, bottomRight, bottomLeft, (0, 255, 0), 2)
cv2.line(image, bottomLeft, topLeft, (0, 255, 0), 2)
# 计算并绘制 ArUCo 标记的中心 (x, y) 坐标
cX = int((topLeft[0] + bottomRight[0]) / 2.0)
cY = int((topLeft[1] + bottomRight[1]) / 2.0)
cv2.circle(image, (cX, cY), 4, (0, 0, 255), -1)
# 在图像上绘制 ArUco 标记 ID
cv2.putText(image, str(markerID), (topLeft[0], topLeft[1] - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
print("[INFO] ArUco marker ID: {}".format(markerID))
# 显示输出图像
cv2.namedWindow("Image1", 0);
cv2.resizeWindow("Image1", 640, 480);
cv2.imshow("Image1", image)
cv2.waitKey(0)
检测不成功,字典里可能没有相关字典具体看opencv文档