python使用cv2.aruco模块对图像中的二维码位姿估计代码参考
时间: 2023-06-10 16:04:14 浏览: 134
检测图像中二维码的位置.zip
import cv2
import numpy as np
#读取图像
img = cv2.imread('test.jpg')
#设置二维码字典类型
aruco_dict = cv2.aruco.Dictionary_create(5, 5)
#生成Marker
markerImage = np.zeros((200, 200), dtype=np.uint8)
markerImage = cv2.aruco.drawMarker(aruco_dict, 23, 200, markerImage, 1)
#拼接图像
height, width = img.shape[:2]
smaller_img = cv2.resize(markerImage, (0,0), fx=0.2, fy=0.2)
x_offset = width - 200
y_offset = height - 200
img[y_offset:y_offset+smaller_img.shape[0], x_offset:x_offset+smaller_img.shape[1]] = smaller_img
#设置二维码参数
parameters = cv2.aruco.DetectorParameters_create()
#检测二维码
markerCorners, markerIds, rejectedCandidates = cv2.aruco.detectMarkers(img, aruco_dict, parameters=parameters)
#计算位姿
if markerIds is not None and len(markerIds) > 0:
rvecs, tvecs, _ = cv2.aruco.estimatePoseSingleMarkers(markerCorners, 0.05, cameraMatrix, distCoeffs)
#绘制结果
for i in range(len(markerIds)):
cv2.aruco.drawAxis(img, cameraMatrix, distCoeffs, rvecs[i], tvecs[i], 0.1)
cv2.aruco.drawDetectedMarkers(img, markerCorners)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
阅读全文