python使用opencv4.7中的cv2.aruco模块对图像中的二维码位姿估计代码参考
时间: 2023-06-10 08:04:13 浏览: 179
opencv-python-4.10.0.84.tar.gz
5星 · 资源好评率100%
import cv2
import numpy as np
# 创建aruco字典
aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_6X6_250)
# 创建aruco探测器
aruco_params = cv2.aruco.DetectorParameters_create()
# 读取图像
img = cv2.imread('test.png')
# 转换成灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测二维码
corners, ids, rejected = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=aruco_params)
# 如果检测到二维码
if ids is not None:
# 绘制二维码边框
cv2.aruco.drawDetectedMarkers(img, corners, ids)
# 估计位姿
rvec, tvec, _ = cv2.aruco.estimatePoseSingleMarkers(corners, 0.05, mtx, dist)
# 绘制位姿
for i in range(len(ids)):
cv2.aruco.drawAxis(img, mtx, dist, rvec[i], tvec[i], 0.1)
# 显示结果
cv2.imshow('result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
阅读全文