OpenCV下使用Cmake
最近在依照毛新云的OpenCV3编程入门,在Windows下用CMake编译OpenCV 3.1.0的源代码,由于是第一次接触这些,在学习编译过程中遇到了很多问题,也耗费了大量的时间,现在将其中一个问题记录下来,希望可以帮组别人少走弯路。感觉编译后主要的好处是可以看源代码。网上传言OpenCV官方提供的编译库一般只是标准版本,可能与某些库并不兼容,比如官方提供的编译好的版本与opengl就不兼容,这时就只能自己构建项目后编译。
首先在CMake官网https://cmake.org/下载了cmake-3.6.2-win64-x64,在编译过程中,估计是因为CMake的服务器在外国的原因,跨越不了GREATWALL,一直出现下面错误提示:
Downloading opencv_ffmpeg_64.dll...
CMake Error at cmake/OpenCVUtils.cmake:895 (file):
file DOWNLOAD HASH mismatch
for file: [D:/Program Files (x86)/opencv/sources/3rdparty/ffmpeg/downloads/35fe6ccdda6d7a04e9056b0d73b98e76/opencv_ffmpeg_64.dll]
expected hash: [35fe6ccdda6d7a04e9056b0d73b98e76]
actual hash: [f112aef8bcfad76757e2ebf06c7cf6e5]
status: [28;"Timeout was reached"]
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:15 (ocv_download)
cmake/OpenCVFindLibsVideo.cmake:206 (include)
CMakeLists.txt:536 (include)
CMake Error at cmake/OpenCVUtils.cmake:899 (message):
Failed to download opencv_ffmpeg_64.dll. Status=28;"Timeout was reached"
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:15 (ocv_download)
cmake/OpenCVFindLibsVideo.cmake:206 (include)
CMakeLists.txt:536 (include)
读一下这几行,可以大致了解如下,在编译过程中,程序下载了一个文件,它的hash值与所期待的不一致,所以导致了错误,接下来到CMakeLists.txt文件(在…...\opencv\sources)中去定位,然而看不懂。。。。。。于是去网上搜索opencv_ffmpeg_64.dll,下载至....../opencv/sources/3rdparty/ffmpeg/downloads/35fe6ccdda6d7a04e9056b0d73b98e76/ 下载链接:http://pan.baidu.com/s/1gfE3gHl
还是错误,不过错误提示变了:
Downloading opencv_ffmpeg.dll...
CMake Error at cmake/OpenCVUtils.cmake:895 (file):
file DOWNLOAD HASH mismatch
for file: [D:/Program Files (x86)/opencv/sources/3rdparty/ffmpeg/downloads/89c783eee1c47bfc733f08334ec2e31c/opencv_ffmpeg.dll]
expected hash: [89c783eee1c47bfc733f08334ec2e31c]
actual hash: [3de6314883587d538b4864070e0a749d]
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)
继续下载了一个opencv_ffmpeg.dll,替换至.../opencv/sources/3rdparty/ffmpeg/downloads/89c783eee1c47bfc733f08334ec2e31c/下载链接:http://pan.baidu.com/s/1dEAnopN
然后开始Downloading ippicv_windows_20151201.zip,我知道肯定下载不了,继续自己下载,http://pan.baidu.com/s/1c27hCT6
放到/opencv/sources/3rdparty/ippicv/downloads/windows-04e81ce5d0e329c3fbc606ae32cad44d/继续configure。。。。。。如下图,Configuring done
然后进行第二次Configure(配置),直接点击Configure,几秒钟后,Configuring done
最后,单机Generate,成功生成项目,几秒钟完成,显示Generating done。大功告成也!
在CMake是自己设定build的路径下,可以打开OpenCV.sln工程,按F5启动调试,5分钟编译完成,
但提示如下错误:
原因是OpenCV的源代码工程默认将"ALL_BUILD"这个项目设置为了启动项,编译后就会默认运行它。而"ALL_BUILD"是一个项目生成周边的杂项,不是exe文件,本生不可以运行,所以自然就会弹出这样的错误提示。若想让此报错的对话框不像是,指定一个另外的启动项即可。我们在解决方案资源管理器里,对需要设置为启动项的那个项目右键点击,在弹出的菜单项中单击【设为启动项(J)】进行设定。如下图:
总结解决办法是:自己根据上述地址手动下载该文件,然后将编译时下载的文件替换掉。根据上述代码可知,如果该文件存在,就不会再去下载。