前言:
由于OpenCV功能越来越臃肿,OpenCV3改变了项目架构,使用内核+插件的架构形式。 因此,OpenCV从2.x到3.x是一个很大的转变,对于很多功能不完善、性能不稳定的模块,都被放到了extra_modules(扩展模块)里面了。在Github中,除了存放正式版OpenCV的主仓库和新增加的OpenCV_extra仓库外,还添加了一个OpenCV_contrib的全新仓库,功能有:脸部识别、文本识别、边缘检测、追踪算法等,里面存放的是功能未稳定的代码,如果需要使用这些功能,就需要重新进行编译。
由于任务要求,需要把OpenCV的扩展模块中的xfeatures给包含进来,因此自己手动编译OpenCV3.1.0与其扩展模块实现。这个过程其实并不难,只要如下三步即可实现。在正式开始之前,有些准备工作需要做,就是下载OpenCV3.1.0还有其扩展模块,以及CMake GUI工具。在这里,我先贴出下载地址,大家如果有需要可以去上面下载。
OpenCV3.1.0下载地址:
https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.1.0/opencv-3.1.0.exe/download
OpenCV3.1.0安装教程:
https://blog.csdn.net/qq_17550379/article/details/78201442
OpenCVcontrib下载地址:
https://github.com/opencv/opencv_contrib
cmake-gui下载地址(3.11.0): https://cmake.org/download/
一、下载OpenCV contrib
这里我事先已经下载并配置好了VS2015和OpenCV3.1.0,所以我们直接下载OpenCVcontrib,进入上面给出的网址,点击releases,如下:
进入releases以后:
由于我的OpenCV版本是3.1.0,因此我们下载对应的zip压缩文件到指定文件夹下,如下:
下载好以后我们对其进行解压缩:
进入解压后的文件夹下:
二、下载cmake-gui
我们进入网址,选择win64位版本对应的压缩包,如下所示:
点击下载:
下载好以后,我们对其进行解压缩:
然后我们在OpenCV的安装目录下新建一个newbuild文件夹,用来存放编译扩展模块后的生成文件:
打开其bin目录下的cmake-gui.exe工具:
运行该程序,输入如下目录:
然后点击Generate进行编译:
第一次导出时需要设置后面用来编译此OpenCV.sln工程的VS的版本(这里特别要注意的是,自己机器上是否装有相应的VS版本,如果没有装,还是要编译就会出错,可能是找不到对应的工具原因,以及选择X86和X64),这里用的是VS 2015。
稍等片刻:
(1)真是天有不测风云,人有悲欢离合,结果报错:
Downloading opencv_ffmpeg.dll...
CMake Error at cmake/OpenCVUtils.cmake:895 (file):
file DOWNLOAD HASH mismatch
for file: [D:/OpenCV3.1/opencv/sources/3rdparty/ffmpeg/downloads/89c783eee1c47bfc733f08334ec2e31c/opencv_ffmpeg.dll]
expected hash: [89c783eee1c47bfc733f08334ec2e31c]
actual hash: [3f05c9b2e6866360eedb35ff3dddff65]
status: [28;"Timeout was reached"]
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:10 (ocv_download)
cmake/OpenCVFindLibsVideo.cmake:206 (include)
CMakeLists.txt:536 (include)
CMake Error at cmake/OpenCVUtils.cmake:899 (message):
Failed to download opencv_ffmpeg.dll. Status=28;"Timeout was reached"
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:10 (ocv_download)
cmake/OpenCVFindLibsVideo.cmake:206 (include)
CMakeLists.txt:536 (include)
仔细分析,其实是sources\3rdparty\ffmpeg\downloads目录下的opencv_ffmpeg.dll,由于下载不成功导致以上错误 。
于是我们自行下载这两个文件:
下载网址如下
https://raw.githubusercontent.com/Itseez/opencv_3rdparty/8aeefc4efe3215de89d8c7e114ae6f7a6091b8eb/ffmpeg/opencv_ffmpeg.dll
https://raw.githubusercontent.com/Itseez/opencv_3rdparty/8aeefc4efe3215de89d8c7e114ae6f7a6091b8eb/ffmpeg/opencv_ffmpeg_64.dll
然后再将opencv_ffmpeg.dll放在目录下:
sources\3rdparty\ffmpeg\downloads\89c783eee1c47bfc733f08334ec2e31c
将opencv_ffmpeg_64.dll放在目录
sources\3rdparty\ffmpeg\downloads\35fe6ccdda6d7a04e9056b0d73b98e76
(2)重新编译,结果又发生错误:
CMake Error at 3rdparty/ippicv/downloader.cmake:73 (file):
file DOWNLOAD HASH mismatch
for file:[D:/OpenCV3.1/opencv/sources/3rdparty/ippicv/downloads/windows-04e81ce5d0e329c3fbc606ae32cad44d/ippicv_windows_20151201.zip]
expected hash: [04e81ce5d0e329c3fbc606ae32cad44d]
actual hash: [f407b66b95729ddd35da423d75ba651a]
status: [28;"Timeout was reached"]
Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:237 (include)
cmake/OpenCVFindLibsPerf.cmake:12 (include)
CMakeLists.txt:537 (include)
CMake Error at 3rdparty/ippicv/downloader.cmake:77 (message):
ICV: Failed to download ICV package: ippicv_windows_20151201.zip.
Status=28;"Timeout was reached"
Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:237 (include)
cmake/OpenCVFindLibsPerf.cmake:12 (include)
CMakeLists.txt:537 (include)
Configuring incomplete, errors occurred!
See also "D:/OpenCV3.1/opencv/newbuild/CMakeFiles/CMakeOutput.log".
See also "D:/OpenCV3.1/opencv/newbuild/CMakeFiles/CMakeError.log".
仔细分析,原理是ippicv无法下载,我们自己手动下载:
下载地址如下
https://raw.githubusercontent.com/Itseez/opencv_3rdparty/81a676001ca8075ada498583e4166079e5744668/ippicv/ippicv_windows_20151201.zip
windows平台,将ippicv_windows_20151201.zip放在
sources\3rdparty\ippicv\downloads\windows-04e81ce5d0e329c3fbc606ae32cad44d
将所有需要下载的文件都放到相应的目录,在开始CMake,配置完成:
第一次编译完成之后,我们需要将额外的opencv_contrib加到工程中进行第二次编译,在配置表中找到“OPENCV_EXTRA_MODULES_PATH”,设置其参数值为open_contrib源码包中的modles目录。一次之后如果还有红色的块,继续configure,直到全部变白为止。 然后点击Generate,回提示Generate Done。恭喜,完成了一半!!!
、、
编译完成后,我们在资源管理器下查看上面设置的文件输出目录,发现目录下多出了很多文件,其实就是OpenCV.sln工程文件:
三、配置VS生成install
用VS 2015打开OpenCV.sln工程,在解决方案中可以查看工程目录:
编译生成debug版本的库,记得在此之前要选择编译的平台信息,这就是编译生成debug版本和release版本的区别,也可以选择release,因为自己的工程可能要用到相应的动态链接库。
我们先对项目进行重新生成解决方案:
等待一段时间,直至生成完毕:
找到CMakeTargets->INSTALL, 右键选择生成install即可。这样就会在D:\OpenCV3.1\opencv\newbuild下面多出一个install的文件夹,到如果能成功生成,编译就结束了。
等待几分钟:
然后,在工程目录newbuild下的install目录中,可以看到生成了一堆文件,这堆文件代表着编译完成:
四、配置新的库文件
重新配置OpenCV,新建一个项目打开,然后配置opencv工程属性:
打开属性管理器:
双击图中Microsoft.Cpp.Win32.use前面的扳手图标,进行配置:
设置可执行文件目录:
设置包含目录:
包含以下文件目录:
D:\OpenCV3.1\opencv\newbuild\include
D:\OpenCV3.1\opencv\newbuild\include\opencv
D:\OpenCV3.1\opencv\newbuild\include\opencv2
设置库文件目录:
设置库目录:
然后是附加依赖项:
在里面粘贴如下库文件:
opencv_aruco310d.lib
opencv_bgsegm310d.lib
opencv_bioinspired310d.lib
opencv_calib3d310d.lib
opencv_ccalib310d.lib
opencv_core310d.lib
opencv_datasets310d.lib
opencv_dnn310d.lib
opencv_dpm310d.lib
opencv_face310d.lib
opencv_features2d310d.lib
opencv_flann310d.lib
opencv_fuzzy310d.lib
opencv_highgui310d.lib
opencv_imgcodecs310d.lib
opencv_imgproc310d.lib
opencv_line_descriptor310d.lib
opencv_ml310d.lib
opencv_objdetect310d.lib
opencv_optflow310d.lib
opencv_photo310d.lib
opencv_plot310d.lib
opencv_reg310d.lib
opencv_rgbd310d.lib
opencv_saliency310d.lib
opencv_shape310d.lib
opencv_stereo310d.lib
opencv_stitching310d.lib
opencv_structured_light310d.lib
opencv_superres310d.lib
opencv_surface_matching310d.lib
opencv_text310d.lib
opencv_tracking310d.lib
opencv_ts310d.lib
opencv_video310d.lib
opencv_videoio310d.lib
opencv_videostab310d.lib
opencv_xfeatures2d310d.lib
opencv_ximgproc310d.lib
opencv_xobjdetect310d.lib
opencv_xphoto310d.lib
最后设置环境变量, 我的编译生成的OpenCV v14/bin的目录如下:
D:\OpenCV3.1\opencv\newbuild\install\x86\vc14\bin
五、代码测试
为了测试扩展模块是否配置成功,我们编写测试代码:
#include
#include
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;
int main( )
{
Mat img = imread("test3.png", IMREAD_GRAYSCALE);
if (img.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("原图:", CV_WINDOW_AUTOSIZE);
imshow("原图:", img);
int minHessian = 400;
Ptr detector = SURF::create(minHessian);
vector keypoints;
detector->detect(img, keypoints);
Mat img_keypoints;
drawKeypoints(img, keypoints, img_keypoints, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
namedWindow("key points", CV_WINDOW_AUTOSIZE);
imshow("key points", img_keypoints);
waitKey(0);
return 0;
}
运行代码,结果如下:
大功告成!!!打完收工!!!