主要问题:
1. OpenCV 3应用编程时,无法使用SURF/SIFT等nonfree算法,甚至根本不存在nonfree头文件。
2. 对opencv及opencv_contrib 进行cMake时出现 "project invalid" 、 "CMake Error at cmake/OpenCVModule.cmake:370 (_glob_locations):" 以及 所需组件无法下载(例如 "FFMPEG: Download failed" )等错误。
3. 新opencv编译程序报错LNK1104 cannot open file 'opencv_aruco347.lib'。
4. 简单OpenCV显示图片程序,imshow窗口闪退。
5. opencv 调用cv::xfeatures2d::SURF::create()崩溃。
6. 各种奇怪的崩溃,debug模式下调试报错"Unhandled exception (opencv_core310.dll)"、"(xxx.dll)in xx.exe Access violation reading location"、" Access violation writinglocation"等类似错误。
解决方案:
1. 本文,添加opencv_contrib模块。
2. 1)"project invalid":可能是源码目录选错,更可能是EXTRA_MODULES_PATH目录复制输入的,必须使用文件选择按钮;
2)"FFMPEG: Download failed":反复Configure或者手动下载。
3. 项目属性中链接器(Linker)->通用(General)->附加库目录(Additional Library Directories)设置为$(OPENCV_DIR)\lib。
4. debug里面,链接器->输入->附加依赖项里面只添加带有"-d"的依赖。
5. 一定要勾选"OPENCV_ENABLE_NONFREE"!!
6. debug模式,链接器->输入->附加依赖项里面只添加带有"-d"的依赖。
最近做毕设,在使用OpenCV(我的版本OpenCV 3.4.7)实现特征匹配与检测时,发现无法使用SurfFeatureDetector并且连nonfree头文件都不存在
查询了解到,OpenCV3将很多功能不完善、性能不稳定的模块,都放到了extra_modules(扩展模块)里面。而我所恰好需要的要用到sift/surf算法,都放到了在xfeacture模块中。
本文最主要的参考资料来自于CSDN博文《opencv3.0中contrib模块的添加》(https://blog.csdn.net/streamchuanxi/article/details/51044929)
但由于里面还是有部分错误或者说陷阱误导,我还是要重新详细整理一遍。
本人配置win10_64 + VS2017 + OpenCV 3.4.7 + cmake 3.17.0 rc1
至于博文所述的“全程退出360安全卫士和360杀毒”以及“vs以管理员的方式运行”似乎是不必要的。
关于VS管理员权限问题,也许是因为我的win10是专业版所以不需要管理员方式运行,大家在编译时还是使用管理员权限运行吧,乐于探索的勇士可以都尝试一下。
在OpenCV(https://github.com/opencv/opencv/releases)
OpenCV_contrib(https://github.com/opencv/opencv_contrib/releases)
分别下载对应相同版本的OpenCV、OpenCV_contrib源码(不要下载exe文件,我选择的3.4.7版本Source code(zip))
在cMake(https://cmake.org/download/)下载对应版本的cMake(我选择的Windows win64-x64 Installer可执行文件)
安装cMake
将下载得到的opencv-3.4.7.zip和opencv_contrib-3.4.7.zip解压到同一文件夹下(路径名不要有中文,没试过会不会出错)
打开cMake进行编译,分别选择源码和输出路径,如图
点击“Configure”,选择编译器和平台(我选择的VS2017,x64平台),等待完成:
有可能在这个时候就会出错了(这是由于我第一次时候智障地用opencv_contrib作为源码进行编译)
第一阶段完成会出现一片红,
找到OPENCV_EXTRA_MODULES_PATH,输入opencv_contrib-3.4.7下的modules目录(D:\OpenCV3.4.7\xopencvs\opencv_contrib-3.4.7\modules)
与此同时,一定要勾选"OPENCV_ENABLE_NONFREE",就在"OPENCV_EXTRA_MODULES_PATH"上面一个
否则就会出现nonfree内容无法使用的问题(这也是我遇到的问题,所以不得不又重新编译安装一遍)
就像这样:
代码会在 Ptr
解决方案参考《opencv 调用cv::xfeatures2d::SURF::create()崩溃》(https://blog.csdn.net/st_spring/article/details/103325213?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task)
讲真,配置OpenCV3真的破事太多了,心很累啊!!!!
否则就会出现前文提到过的Project file may be invalid错误(参考http://www.pianshen.com/article/7666286895/)
其日志显示如下:
【在这个地方当时纠结了好久,以为是《opencv3.0中contrib模块的添加》提到的ippicv的问题】
但各种处理都不见结果,最后才发现是目录的输入方式有问题(参考http://www.pianshen.com/article/7666286895/)
更改目录录入方式之后效果,可以对比上图
顺便说一下,下载ippicv可以到https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20180723/ippicv选择下载
在branch处选择不同版本
一直Configure,一直Configure,直到所有都变白色,如下图
或者可以参考博文《CMake opencv时Download: opencv_ffmpeg.dll、ippicv等失败的解决方法》(https://blog.csdn.net/KayChanGEEK/article/details/79919417)进行手动下载并编译安装
在cMake编译输出目录(D:\OpenCV3.4.7\xopencv)找到OpenCV.sln,并用VS(我用的VS2017)打开
打开后,在解决方案中(Solution Explorer)找到CMakeTargets下的INSTALL,分别在Debug和Release下分别右键生成build
漫漫漫漫漫.....长长长长长.....的的的的的.....等等等等等.....待待待待待.....
如果再生成过程中出现错误,可能需要重新cMake一遍.....
DEBUG Build & RELEASE Build
Build完成之后,就能在输出目录下找到 install文件夹,该文件夹就是最终结果:
查看原opencv文件结构
由于我之前就安装并配置过OpenCV,所以我直接将原cpencv\build备份(验证成功之后删除)
并将install文件夹改名build代替旧的
本以为不需要专门进行OpenCV环境配置以及新建项目时的配置,其实不然
环境变量依旧是“D:\OpenCV3.4.7\opencv\build\x64\vc15\bin”
两种方式查看项目属性:
1. Project->XXX Properties
2.解决方案中项目右键->Properties
VC++目录(VC++ Directoies)->包含目录(Include Directories)
D:\OpenCV3.4.7\opencv\build\include
D:\OpenCV3.4.7\opencv\build\include\opencv
D:\OpenCV3.4.7\opencv\build\include\opencv2
VC++目录(VC++ Directoies)->库目录(Library Directoies)
D:\OpenCV3.4.7\opencv\build\x64\vc15\lib
链接器(Linker)->输入(Input)->附加依赖项(Additional Dependencies)
将D:\OpenCV3.4.7\opencv\build\x64\vc15\lib中所有lib文件添加进去
opencv_aruco347.lib
opencv_aruco347d.lib
opencv_bgsegm347.lib
opencv_bgsegm347d.lib
opencv_bioinspired347.lib
opencv_bioinspired347d.lib
opencv_calib3d347.lib
opencv_calib3d347d.lib
opencv_ccalib347.lib
opencv_ccalib347d.lib
opencv_core347.lib
opencv_core347d.lib
opencv_datasets347.lib
opencv_datasets347d.lib
opencv_dnn347.lib
opencv_dnn347d.lib
opencv_dnn_objdetect347.lib
opencv_dnn_objdetect347d.lib
opencv_dpm347.lib
opencv_dpm347d.lib
opencv_face347.lib
opencv_face347d.lib
opencv_features2d347.lib
opencv_features2d347d.lib
opencv_flann347.lib
opencv_flann347d.lib
opencv_fuzzy347.lib
opencv_fuzzy347d.lib
opencv_hfs347.lib
opencv_hfs347d.lib
opencv_highgui347.lib
opencv_highgui347d.lib
opencv_imgcodecs347.lib
opencv_imgcodecs347d.lib
opencv_imgproc347.lib
opencv_imgproc347d.lib
opencv_img_hash347.lib
opencv_img_hash347d.lib
opencv_line_descriptor347.lib
opencv_line_descriptor347d.lib
opencv_ml347.lib
opencv_ml347d.lib
opencv_objdetect347.lib
opencv_objdetect347d.lib
opencv_optflow347.lib
opencv_optflow347d.lib
opencv_phase_unwrapping347.lib
opencv_phase_unwrapping347d.lib
opencv_photo347.lib
opencv_photo347d.lib
opencv_plot347.lib
opencv_plot347d.lib
opencv_reg347.lib
opencv_reg347d.lib
opencv_rgbd347.lib
opencv_rgbd347d.lib
opencv_saliency347.lib
opencv_saliency347d.lib
opencv_shape347.lib
opencv_shape347d.lib
opencv_stereo347.lib
opencv_stereo347d.lib
opencv_stitching347.lib
opencv_stitching347d.lib
opencv_structured_light347.lib
opencv_structured_light347d.lib
opencv_superres347.lib
opencv_superres347d.lib
opencv_surface_matching347.lib
opencv_surface_matching347d.lib
opencv_text347.lib
opencv_text347d.lib
opencv_tracking347.lib
opencv_tracking347d.lib
opencv_video347.lib
opencv_video347d.lib
opencv_videoio347.lib
opencv_videoio347d.lib
opencv_videostab347.lib
opencv_videostab347d.lib
opencv_xfeatures2d347.lib
opencv_xfeatures2d347d.lib
opencv_ximgproc347.lib
opencv_ximgproc347d.lib
opencv_xobjdetect347.lib
opencv_xobjdetect347d.lib
opencv_xphoto347.lib
opencv_xphoto347d.lib
测试代码及结果
#include "pch.h"
#include
#include
using namespace cv;
int main()
{
Mat srcImg = imread("G:\\gatepic.jpg");
imshow("imshow", srcImg);
waitKey(0);
return 0;
}
imshow窗口一闪而过.............................................
我使用我在开头所说的“SIFT/SURF算法和nonfree头文件”问题,再次进行验证
已经可以使用nonfree.hpp了。不过SURF使用语句有改变,即从
int minHessian = 400;
cv::SurfFeatureDetector detector(minHessian);
变为
Ptr detector = xfeatures2d::SURF::create();
而且现在程序已经完全无法运行了。。。。。。。。。。。。。。。。。。。
报错 " Error LNK1104 cannot open file 'opencv_aruco347.lib' "
天哪,已经搞了九个小时了,,,,,I wanna say the F-word....
我失败了 (2020-02-22 21:10)
以为是编译过程出现了问题,于是又完整的重复了一遍上面的操作
结果依然。。。。。
最后参考《win10+vs2017+opencv3.4.3+cmake配置opencv contrib过程和注意事项》(https://blog.csdn.net/weixin_42012977/article/details/82992962)
其中提到【将*\opencv\new_build\install\x64\vc14\bin下的dll文件复制至 C:\Windows\System32 和 C:\Windows\SysWOW64 目录下】,最开始不想这么做,因为以前的OpenCV一直也没有这样操作,况且加了环境变量的,为什么还需要复制到sys文件夹???
但后来不得已,只得这样尝试。
哈哈哈哈啊哈,,,,没有用!!!!!
(2020-02-23 1:28)
就在我准备放弃并且重启电脑的时候,我继续查看了下百度关于【LNK1104 cannot open file 'opencv_aruco347.lib'】的搜索结果,找到了一条overStack的提问【OpenCV error: “LINK : fatal error LNK1104: cannot open file 'opencv_core300d.lib' ”】(https://www.baidu.com/link?url=uZFfbkzhpvy5vzELyS9atbbmT0h7tkMt5YOlKht-D6WqXGpHYzjrPFectg6kF2ZFK1doFErbTaCMnsUA1CncVYO6rKFIuDXTus1lJtaTHh3EKj-Gz7Xw5sKRORUWceSshaXBVVr12V1SKd60NpO81jgFnIdw20zgvlOLGk8LJ__-AbtGtNarQTxqesf5fZxJ&wd=&eqid=8b03faed00067d7b000000055e515b41)
打勾的答案如下:
意思就是说要在项目属性中链接器(Linker)->通用(General)->附加库目录(Additional Library Directories)设置为$(OPENCV_DIR)\staticlib;(即答主举例的E:\opencv\build\x86\vc12\staticlib)
当然,现存的文件结构中是不存在 staticlib 的,所以我就尝试性的使用D:\OpenCV3.4.7\opencv\build\x64\vc15\lib;
编译运行成功了!!!!!!!!!!!!!!!!!!(2020-02-23 02:00)
另外,我偶然发现,真正导致【LNK1104 cannot open file 'opencv_aruco347.lib'】的不是【链接器(Linker)->通用(General)->附加库目录(Additional Library Directories)】的问题
而是由于我在填写【VC++目录(VC++ Directoies)->库目录(Library Directoies)】时候填到下一行了
【??但是我编译安装了三次,三次都出现这个问题,难道是我三次都填错了吗??】
那么现在再来看一下前面那个简单OpenCV图像显示程序,imshow窗口闪退的问题。
仅Debug模式下闪退,Release模式下不会闪退
这其实也是一个跟奇快的问题没因为在使用之前的OpenCV3.4.7时候并没有这种情况。
#include "pch.h"
#include
#include
using namespace cv;
int main()
{
Mat srcImg = imread("G:\\gatepic.jpg");
imshow("imshow", srcImg);
waitKey(0);
return 0;
}
查找过程中找到了一篇问答《VS2010配置opencv2.2.0,运行时总是闪退》(https://ask.csdn.net/questions/246151)
其中回答【在debug里面,链接器->输入->附加依赖项里面添加,opencv_world341d.lib,注意,有一个d在里面,而release里面是没有D的,这两个不能混用,否则会闪退】
于是我将该项目中的依赖换成只有-d的
opencv_aruco347d.lib
opencv_bgsegm347d.lib
opencv_bioinspired347d.lib
opencv_calib3d347d.lib
opencv_ccalib347d.lib
opencv_core347d.lib
opencv_datasets347d.lib
opencv_dnn347d.lib
opencv_dnn_objdetect347d.lib
opencv_dpm347d.lib
opencv_face347d.lib
opencv_features2d347d.lib
opencv_flann347d.lib
opencv_fuzzy347d.lib
opencv_hfs347d.lib
opencv_highgui347d.lib
opencv_imgcodecs347d.lib
opencv_imgproc347d.lib
opencv_img_hash347d.lib
opencv_line_descriptor347d.lib
opencv_ml347d.lib
opencv_objdetect347d.lib
opencv_optflow347d.lib
opencv_phase_unwrapping347d.lib
opencv_photo347d.lib
opencv_plot347d.lib
opencv_reg347d.lib
opencv_rgbd347d.lib
opencv_saliency347d.lib
opencv_shape347d.lib
opencv_stereo347d.lib
opencv_stitching347d.lib
opencv_structured_light347d.lib
opencv_superres347d.lib
opencv_surface_matching347d.lib
opencv_text347d.lib
opencv_tracking347d.lib
opencv_video347d.lib
opencv_videoio347d.lib
opencv_videostab347d.lib
opencv_xfeatures2d347d.lib
opencv_ximgproc347d.lib
opencv_xobjdetect347d.lib
opencv_xphoto347d.lib
果然,窗口保持住了
还有一个解决方案:在代码
imshow("imshow", srcImg);
前,添加一句
namedWindow("imshow");
但这样就很不爽,不想采用
但我不禁怀疑,【难道我添加contribute模块导致OpenCV退化成 2 版本了???】
还是说我之前使用OpenCV3也遇到过这种情况,调整之后忘了??可是我也有最近新建项目,都没这种情况
从02-22 中午12点到现在(02-23 02:40)15个小时了,太可怕了,又是一事无成的一天
代码在 Ptr
int hessianThreshold = 400;
Ptr detector = xfeatures2d::SURF::create(hessianThreshold);
std::vector keypoints;
detector->detect(srcImg, keypoints);
Mat featuredImg;
drawKeypoints(srcImg, keypoints, featuredImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
正如前文所述,一定要勾选"OPENCV_ENABLE_NONFREE",就在"OPENCV_EXTRA_MODULES_PATH"上面一个
参考《opencv 调用cv::xfeatures2d::SURF::create()崩溃》(https://blog.csdn.net/st_spring/article/details/103325213?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task)
另外,前方已经处理过这个问题了,但今天在重新做的时候,附加库好像自己改变了
于是造成了错误,使我迷糊
查阅资料,终于找到了根本原因,参考《Face Recognition in Videos with OpenCV3 gives Unhandled exception (opencv_core310.dll)》(https://stackoverflow.com/questions/41302737/face-recognition-in-videos-with-opencv3-gives-unhandled-exception-opencv-core31)
赶紧查看自己的附加库设置,果然如此
最后更改后,完整运行SURF特征检测程序,成功实现了特征检测:
再一次吐槽OpenCV3