OpenCV是一个优秀的开源视觉处理软件框架,也是计算机视觉学习道路上必须掌握的一套工具,奈何其版本兼容性上实在是一言难尽。本文主要就是在说OpenCV搭建开发环境时遇到的各种奇葩问题,方便帮助后面的新同学排雷。如果你也刚好遇到类似的问题并且靠着本文的处理方式解决掉你的麻烦,不妨搜一搜VX公众号“24K纯学渣”,可能会有更多的惊喜噢。本文将持续更新。。。
PS:如果本文找不到你遇到的问题,可以去官方文档搜,传送门:https://docs.opencv.org/
opencv 3.4.2.17 是最后一版支持免费使用sift等模板匹配算法的版本,之后的版本由于版权问题去掉了一些可能会被收费的模块。
aruco算法从4.7.0开始进入到opencv-python主库
如果只需要主要模块 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。
关于安装包的问题解决完以后,新版本的安装包在原来的代码上会连续报如下错误,对应新版本修改即可。
————————————————
版权声明:本文为CSDN博主「童鸢」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41837701/article/details/129256430
# for Linux-like OS
pip3 install opencv-python==x.x.x.x
# for Windows
pip install opencv-python==x.x.x.x
只安装了opencv-python
import cv2 # opencv-python==4.8.1.78
img = cv2.imread("./in_imgs/4x4_15.png", 0)
cv2.imshow("img", img)
ad = cv2.aruco.ArucoDetector()
corners, ids, rejected = ad.detectMarkers(img)
cv2.waitKey()
cv2.destroyAllWindows()
只安装 opencv-contrib-python==3.4.2.17,环境是CentOS 7.9 server版本
import cv2 # opencv-python==3.4.2.17
img = cv2.imread("./in_imgs/4x4_15.jpg", cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow("img", img) # 由于没有GUI,无法调用imshow函数
arucoDict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_50)
arucoParams = cv2.aruco.DetectorParameters_create()
corners, ids, rejected = cv2.aruco.detectMarkers(img_gray, arucoDict, parameters=arucoParams)
print(corners, ids, rejected)
img = cv2.aruco.drawDetectedMarkers(img, corners, ids, [0,255,0])
cv2.imwrite("./out_imgs/detected_aruco.jpg", img)
(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
————————————————
版权声明:本文为CSDN博主「童鸢」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41837701/article/details/129256430
(3)centos7和ubuntu18.04分别解决cv2缺少共享库( libSM.so.6)的解决方法:
yum install libSM-1.2.2-2.el7.x86_64 --setopt=protected_multilib=false
yum install libXrender-0.9.10-1.el7.x86_64 --setopt=protected_multilib=false
yum install libXext-1.3.3-3.el7.x86_64 --setopt=protected_multilib=false
属性表(Debug):
注意: C:\your\local\env\path\for 换成你自己的安装路径。
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>C:\your\local\env\path\for\opencv\build\include;C:\your\local\env\path\for\opencv\build\include\opencv2;$(IncludePath)IncludePath>
<LibraryPath>C:\your\local\env\path\for\opencv\build\x64\vc16\lib;$(LibraryPath)LibraryPath>
PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>C:\your\local\env\path\for\opencv\build\x64\vc16\lib\opencv_world480d.lib;%(AdditionalDependencies)AdditionalDependencies>
Link>
ItemDefinitionGroup>
<ItemGroup />
Project>
属性表(Release):
注意: C:\your\local\env\path\for 换成你自己的安装路径。
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>C:\your\local\env\path\for\opencv\build\include;C:\your\local\env\path\for\opencv\build\include\opencv2;$(IncludePath)IncludePath>
<LibraryPath>C:\your\local\env\path\for\opencv\build\x64\vc16\lib;$(LibraryPath)LibraryPath>
PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>C:\your\local\env\path\for\opencv\build\x64\vc16\lib\opencv_world480.lib;%(AdditionalDependencies)AdditionalDependencies>
Link>
ItemDefinitionGroup>
<ItemGroup />
Project>
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
//std::cout << "先生成一个aruco marker" << std::endl;
//std::cout << "保存生成的aruco marker" << std::endl;
std::cout << "1. 读取aruco marker" << std::endl;
cv::Mat img_color = cv::imread("./in_imgs/4x4_15.jpg", cv::IMREAD_COLOR);
//cv::Mat img = cv::imread("./in_imgs/4x4_15.jpg", cv::IMREAD_COLOR);
cv::Mat img;
cv::cvtColor(img_color, img, cv::COLOR_BGR2GRAY);
//宽度
std::cout << "宽度: " << img.cols << std::endl;
//高度
std::cout << "高度: " << img.rows << std::endl;
//通道数
std::cout << "通道数: " << img.channels() << std::endl;
cv::imshow("aruco_15", img);
auto dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
std::vector>corners, rejectedImgPoints;
std::vector ids;
auto parameters = cv::aruco::DetectorParameters();
//cv::aruco::detectMarkers(img, dictionary, corners, ids, parameters, rejectedImgPoints);
cv::aruco::ArucoDetector ad = cv::aruco::ArucoDetector();
ad.detectMarkers(img, corners, ids);
cv::aruco::drawDetectedMarkers(img_color, corners, ids, cv::Scalar(0, 255, 0));
if (ids.size() > 0)
{
std::cout << ids[0] << std::endl;
}
cv::imshow("drawed", img_color);
cv::waitKey();
return 0;
}