据官方说法,目前还不是太稳定的算法模块都在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的库)。
添加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
#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 matcher = cv::cuda::DescriptorMatcher::createBFMatcher(surf.defaultNorm());
vector matches;
matcher->match(descriptors1GPU, descriptors2GPU, matches);
// downloading results
vector keypoints1, keypoints2;
vector 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;
}
测试图片:
运行结果: