[转]VS2015+OpenCV3.3 GPU模块和opencv_contrib模块的编译以及采用CMake编译opencv_contrib时提示“No extra modules found in folder”问题的解决方案
据官方说法,目前还不是太稳定的算法模块都在opencv_contrib里边,由于不稳定,所以不能在release版本里发行,只有在稳定以后才会放进release里边。但是这里边有很多我们经常要用的算法,比如SIFT,SURF等(在xfeatures2d 模块里边)。官网提供了说明,可以把opencv_contrib扩展模块添加编译到已安装的opencv3里边。
同时我们还需要编译opencv的GPU模块,以便在GPU上加速执行这些算法。
1.点[Brouse Source…],选择OpenCV源码那个sources文件夹的路径。
点[Brouse Build…],选择要生成的工程的路径。如下图:
2.点击 [Configure],出现对话框说文件夹不存在要不要新建文件夹,点yes,然后出现对话框选择生成的工程版本,如下图:
3.点[Finished],一段读条后会生成工程。完成后可以检查一下窗口下部的框,如果正确安装、配置CUDA,应该会有如下字样 CUDA detected+版本号:
4.检查一下WITH_CUDA选项,如果Cmake检测到你安装了CUDA,应该是自动勾上的。如果没自动勾上那就把它勾上。如下图:
5.下载opencv_contrib模块,链接:https://github.com/opencv/opencv_contrib
解压后,我把它放到了opencv3.3的目录下
6.在cmake界面找到OPENCV_EXTRA_MODULES_PATH,修改其值为:D:/opencv3.3/opencv_contrib-master/modules,就是第5步中modules的路径
7.确认好选项之后再按[Configure]
8.按[Generate]生成工程,如果配置和生成工程完全ok底下应该有Configuring done和Generating done两行。如下图:
9.关掉cmake,关掉杀毒软件,在build_opencv3.3_contrib找到OpenCV.sln用VS2015打开,选择生成-重新生成解决方案。
等待两个多小时……
10.编译好后,找到解决方案目录里的[CMakeTargets]项展开的[INSTALL]项,右键->[Project Only(仅项目)]->[Build Only INSTALL(仅生成INSTALL)]。这时在D:\build_opencv3.3_contrib\install\x64\vc14生成了编译好的库(默认生成debug的库,修改为release编译生成release的库)。
给windows操作系统添加“系统环境变量”path环境变量:D:\build_opencv3.3_contrib\install\x64\vc14\bin
11.测试gpu模块与opencv_contrib模块。
新建VS2015控制台应用程序,
工程属性--配置属性--VC++目录--包含目录 中添加:
D:\build_opencv3.3_contrib\install\include\opencv;
D:\build_opencv3.3_contrib\install\include\opencv2;
D:\build_opencv3.3_contrib\install\include;
工程属性--配置属性--VC++目录--库目录 中添加:
D:\build_opencv3.3_contrib\install\x64\vc14\lib
工程属性--配置属性--链接器--输入--附加依赖项 中添加:
opencv_aruco331d.lib
opencv_bgsegm331d.lib
opencv_bioinspired331d.lib
opencv_calib3d331d.lib
opencv_ccalib331d.lib
opencv_core331d.lib
opencv_cudaarithm331d.lib
opencv_cudabgsegm331d.lib
opencv_cudacodec331d.lib
opencv_cudafeatures2d331d.lib
opencv_cudafilters331d.lib
opencv_cudaimgproc331d.lib
opencv_cudalegacy331d.lib
opencv_cudaobjdetect331d.lib
opencv_cudaoptflow331d.lib
opencv_cudastereo331d.lib
opencv_cudawarping331d.lib
opencv_cudev331d.lib
opencv_datasets331d.lib
opencv_dnn331d.lib
opencv_dpm331d.lib
opencv_face331d.lib
opencv_features2d331d.lib
opencv_flann331d.lib
opencv_fuzzy331d.lib
opencv_highgui331d.lib
opencv_img_hash331d.lib
opencv_imgcodecs331d.lib
opencv_imgproc331d.lib
opencv_line_descriptor331d.lib
opencv_ml331d.lib
opencv_objdetect331d.lib
opencv_optflow331d.lib
opencv_phase_unwrapping331d.lib
opencv_photo331d.lib
opencv_plot331d.lib
opencv_reg331d.lib
opencv_rgbd331d.lib
opencv_saliency331d.lib
opencv_shape331d.lib
opencv_stereo331d.lib
opencv_stitching331d.lib
opencv_structured_light331d.lib
opencv_superres331d.lib
opencv_surface_matching331d.lib
opencv_text331d.lib
opencv_tracking331d.lib
opencv_video331d.lib
opencv_videoio331d.lib
opencv_videostab331d.lib
opencv_xfeatures2d331d.lib
opencv_ximgproc331d.lib
opencv_xobjdetect331d.lib
opencv_xphoto331d.lib
可以在D:\opencv3.3\sources\samples\gpu\surf_keypoint_matcher.cpp找到一个测试例程,对其代码做简单修改如下:
#include <iostream> #include "opencv2/opencv_modules.hpp" #include "opencv2/core.hpp" #include "opencv2/features2d.hpp" #include "opencv2/highgui.hpp" #include "opencv2/cudafeatures2d.hpp" #include "opencv2/xfeatures2d/cuda.hpp" using namespace std; using namespace cv; using namespace cv::cuda; int main() { GpuMat img1, img2; img1.upload(imread("1.bmp", IMREAD_GRAYSCALE)); img2.upload(imread("2.bmp", IMREAD_GRAYSCALE)); cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice()); SURF_CUDA surf; // detecting keypoints & computing descriptors GpuMat keypoints1GPU, keypoints2GPU; GpuMat descriptors1GPU, descriptors2GPU; surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU); surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU); cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl; cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl; // matching descriptors Ptr<cv::cuda::DescriptorMatcher> matcher = cv::cuda::DescriptorMatcher::createBFMatcher(surf.defaultNorm()); vector<DMatch> matches; matcher->match(descriptors1GPU, descriptors2GPU, matches); // downloading results vector<KeyPoint> keypoints1, keypoints2; vector<float> descriptors1, descriptors2; surf.downloadKeypoints(keypoints1GPU, keypoints1); surf.downloadKeypoints(keypoints2GPU, keypoints2); surf.downloadDescriptors(descriptors1GPU, descriptors1); surf.downloadDescriptors(descriptors2GPU, descriptors2); // drawing the results Mat img_matches; drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches); namedWindow("matches", 0); imshow("matches", img_matches); waitKey(0); return 0; }
使用CMake编译OpenCV3.0+OpenCV_contrib时所遇问题的解决方案
在windows下使用CMake编译opencv+opencv_contrib的步骤,网上已有很多中文转载,介绍得都很详细。如果对“转载”不放心,可以查看opencv_contrib目录下的README.md,它才是中文翻译的原版。在这里主要解决编译中可能遇到的问题。
-
如果使用VS2010作为IDE, 点击configure之后,可能会出现:error in configuration process project files may be invalid。
解决方案:这是vs2010的问题,不是CMake和opencv的问题,出现这个错误说明电脑中vs版本不兼容,查找以下两个路径:C:\Program Files(x86)\Microsoft Visual Studio 10.0\vc\bin\cvtres.exe和C:\Windows\Microsoft.NET\Framework\v4.0.30319\cvtres.exe。右键属性|详细信息, 查看两者版本号,删除/重命名较旧的版本,或者重新设置Path变量。 -
如果在设置完扩展包之后(如下):
仍然出现 error in configuration process project files may be invalid。
解决方案:可能是opencv与opencv_contrib的版本不匹配,如果是直接打开链接下载的opencv_contrib,则得到的是master版本,它一般是最新的稳定版,而你现在编译的opencv是3.0的版本,所以也需要opencv_contrib为3.0版。
把它作为扩展包的引用,就没有问题了。同时也解决了后续类似 “Unknown CMake command ‘ocv_download’ “的问题。
参考链接:
1、【opencv3.3】VS2015+opencv3.3 GPU模块编译(包含opencv_contrib模块)
3、Windows7+VS2012下64位OpenCV3.0+CUDA7.5的编译和部署
4、【OpenCV入门教程之七】 玩转OpenCV源代码:生成OpenCV工程解决方案与OpenCV源码编译 - 浅墨_毛星云的CSDN博客
5、使用CMake编译OpenCV3.0+OpenCV_contrib时所遇问题的解决方案
6、vs2015下Cmake3.10编译Opencv3.4及opencv_contrib3.4在Windows10
7、opencv3.4+vs2015+win10安装过程问题解决
8、opencv3.4.0的CMake需联外网下载的.cache文件夹内容(ippicv等)
9、OpenCV3.4.0+contrib在VS2017下编译_win10 64位
Visual Studio下载链接: