
No module named “cv2.aruco“以及opencv-python 4.7.0以上版本的aruco检测
在利用OpenCV python检测二维码位姿的时候遇到的一些小问题
先贴上参考链接
1. opencv-python、opencv-python-headless、opencv-contrib-python、opencv-contrib-python-headless包的区别
opencv-python
这个不用说,官方发布的标准版
opencv-python-headless
OpenCV-Python-Headless是一种无界面的版本,可用于运行在服务器上的计算机视觉应用程序。它的用法和OpenCV-Python类似,但是在使用时不会显示图像。这对于运行在无图形界面的服务器上的应用程序很方便,因为它可以节省资源。参考链接
opencv-contrib-python
OpenCV Contrib是OpenCV的扩展模块,包含了许多最新的以及可能还没有正式发布有待进一步完善的算法,可以理解为是OpenCV的扩展包,Github网页点击查看。 这有点类似于Matlab中的各个可选安装扩展包。 同时在OpenCV 3.0以后,SIFT、SURF等特征算法也放到了Contrib库中。 所以如果想使用SIFT算子,则必须要安装Contrib库, 下面以Python安装OpenCV Contrib库为例,介绍安装流程。 注意Contrib版本的OpenCV是普通OpenCV的超集,包含了所有OpenCV正常版的功能,可以理解为“OpenCV PLUS”。参考链接
注意:如果你以前安装过OpenCV,那么请先卸载干净,否则等Contrib版的装好以后,可能出现无法识别的情况。因为识别的还是之前的版本。 利用PIP命令即可卸载。
pip uninstall opencv-python
opencv-contrib-python-headless
与opencv-python-headless类似,是opencv-contrib-python的界面版本
以上四种包适分别谁呢?
python中安装OpenCV提供四种依赖包,参考链接
- 如果只需要主要模块 pip install opencv-python
- 如果需要更全的模块 pip install opencv-contrib-python
- 如果资源较少,不需要任何GUI功能 pip install opencv-python-headless
- 如果资源较少,不需要任何GUI功能,包含contrib模块 pip install opencv-contrib-python-headless
因此一般来说都会选择安装opencv-contrib-python不要同时安装opencv-python和opencv-contrib-python。
关于安装包的问题解决完以后,新版本的安装包在原来的代码上会连续报如下错误,对应新版本修改即可。
2.报错与解决
(1)AttributeError: module ‘cv2.aruco’ has no attribute ‘Dictionary_get’
cv2.aruco.Dictionary_get()
函数会加载cv2.aruco.DICT_nXn_250包含250个标记的字典,其中每个标记都是n×n位二进制模式。- 在最新的版本中,这个函数的API改为了
cv2.aruco.getPredefinedDictionary
(2)AttributeError: module ‘cv2.aruco’ has no attribute ‘DetectorParameters_create’
cv2.aruco.DetectorParameters_create
这个函数的作用是使用默认值初始化检测器参数
改为了cv2.aruco.DetectorParameters
(3)AttributeError: module ‘cv2.aruco’ has no attribute ‘drawAxis’
cv2.aruco.drawAxis
这个函数会在二维码上加上坐标轴,改为了cv2.drawFrameAxes
运行结果如下
3.opencv-python 4.7.0以上版本的aruco检测
import cv2
import numpy as np
# Load the image
gray = cv2.imread('.\\image\\aruco.png')
# Convert the image to grayscale
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
parameters = cv2.aruco.DetectorParameters()
# Create the ArUco detector
detector = cv2.aruco.ArucoDetector(aruco_dict, parameters)
# Detect the markers
corners, ids, rejected = detector.detectMarkers(gray)
# Print the detected markers
print("Detected markers:", ids)
if ids is not None:
cv2.aruco.drawDetectedMarkers(gray, corners, ids)
cv2.imshow('Detected Markers', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
参考
更多推荐
所有评论(0)