OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋

Table of Contents

前言

Windows 10, OpenCV4.0.1, Qt5.9.3, CMake3.12.1, MinGW5.3.0

Windows 10, OpenCV4.0.0, Qt5.9.3, CMake3.12.1, MinGW5.3.0

OpenCV3.4.2 + Contrib3.4.2 + Qt5.9 + CMake3.12.1编译

资料准备

开启CMake GUI

CMake之Qt相关设置

CMake必要的设置

检查CMake设置

Generate

控制台编译

Contrib配置

踩坑笔记

Issue#0:ffmpeg下载问题:Couldn't connect to server/Timeout

Issue#1:勾选ENABLE_CXX11

Issue#3:取消勾选"WITH_MSMF"

Issue#2:在cap_dshow.cpp的文件中加宏定义“#define NO_DSHOW_STRSAFE”

Issue#3:取消勾选"WITH_MSMF"

Issue#4:取消勾选BUILD_opencv_hdf

Issue#5:取消勾选BUILD_opencv_world

Issue#6:取消勾选"ENABLE_PRECOMPILED_HEADERS"

Issue#7:Python3版编译失败

Issue#8.取消勾选BUILD_opencv_gapi【OpenCV4.0.0有此问题,OpenCV4.0.1已经修复】

Issue#9 'FILTER_SCHARR' was not declared in this scope

Qt+OpenCV配置

新建Qt项目

Qt + OpenCV配置

1. 添加环境变量【建议重启电脑】

2. 在TestOpenCV.pro文件中添加包含文件

3.在TestOpenCV.pro文件中添加library

代码验证及效果图

代码1 - 简单验证普通版OpenCV

代码1 - 效果图

代码2 - 简单验证Contrib版OpenCV(使用SIFT特征比对)

代码2 - 效果图

编译所需的.cache文件  - Qt5.9+VS2013/VS2015

OpenCV3.4.2:Qt5.9+VS2013用同一份

OpenCV4.0.1:VS2015

OpenCV4.0.1:Qt5.9

编译好的OpenCV版本链接 - Qt5.9+VS2013

opencv342Qt5.9

opencv342Qt5.9_world

opencv342Qt5.9Contrib

opencv342Vs2013_world

opencv342Vs2013Contrib

opencv342Vs2013Contrib_world

opencv401Qt5.9Contrib

OpenCV + Python版

参考文章


前言

这两天重新编译OpenCV+Visual Studio,顺带着把OpenCV+Qt5.9一起编译了。

详细请参考OpenCV3.4.2 + Contrib3.4.2 + Qt5.9 + CMake3.12.1编译过程、配置及验证。

OpenCV4.0的编译新手建议使用OpenCV4.0.1(非4.0.0),懒人直接去文章末尾下载我编译好的OpenCV版本。

想要体验编译乐趣的高级玩家请务必先看完本文,再动手,以后就可以自行编译需求版本。

教程用的是OpenCV3.4.2。

若有需补充的地方,欢迎大家留言。

Windows 10, OpenCV4.0.1, Qt5.9.3, CMake3.12.1, MinGW5.3.0

汇总CMake及其他配置

  1. 勾选WITH_QT,WITH_OPENGL ,不能勾选WITH_IPP(默认不勾选)
  2. 取消选中"WITH_MSMF"
  3. 修改“D:\software\OpenCV\SourceCode\opencv-4.0.1\modules\videoio\src\cap_dshow.cpp”的文件,在#include "cap_dshow.hpp"前加宏定义“#define NO_DSHOW_STRSAFE”
  4. 取消选中"Build_opencv_python3"和"BUILD_opencv_python_bindings_generator"【我Python2编译成功,但是Python3编译失败,具体还没仔细研究,暂时用不到,就先取消勾选;电脑上不要同时装有python2和python3。如果需要可以去pip3 install,不用自己编译;需要python3版的OpenCV就选中;不要python2的也可以取消选中;电脑上不要同时装有python2和python3,最后只有Python2编译成功,Python3失败。具体参考后面“踩坑笔记”】
  5. 取消勾选BUILD_opencv_world:
  6. Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo【我选的Release,生成release版;需要debug版的选debug;RelWithDebInfo:Release版并带有Debug信息】
  7. .cache问题:除非各位的环境和版本与我的一样,用同样的.cache文件,否则会出现这个问题mismatch_md5。解决办法参考后面“踩坑笔记”
  8. 在这个选项填入Contrib的位置【精确到modules文件夹】

OPENCV_EXTRA_MODULES_PATH:

我的路径为:D:/software/OpenCV/SourceCode/opencv_contrib-3.4.2/modules

    9. 取消选中BUILD_opencv_hdf【H5Type深度学习中的一种文件格式】

   10. 勾选OPENCV_ENABLE_NON_FREE选项。【专利算法模块,包含特征检测和GPU相关的内容】

【OpenCV4.0已经没有这个选项了,它默认支持C++11】

  1. 勾选ENABLE_CXX11

【OpenCV4.0.1已经修复的问题如下,就保持默认就好,大家可以看看。感谢开源代码的Contributors!!!】

  1. 在opencv_contrib-4.0.1\modules\cvv\src\qtutil\filter\sobelfilterwidget.cpp文件中添加using namespace cv;
  2. BUILD_opencv_gapi(默认勾选)【OpenCV4.0添加了全新的opencv_gapi模块。 它是非常有效的图像处理的引擎,基于处理图的lazy评估和动态构造(因此名称为Graph API或G-API)。 有关详细信息,请参见Graph API(G-API)】

以上详细请参考后面“踩坑笔记”。

Windows 10, OpenCV4.0.0, Qt5.9.3, CMake3.12.1, MinGW5.3.0

汇总CMake及其他配置

  1. 勾选WITH_QT,WITH_OPENGL ,不能勾选WITH_IPP(默认不勾选)
  2. 取消选中"WITH_MSMF"
  3. 修改“D:\software\OpenCV\SourceCode\opencv-3.4.2\modules\videoio\src\cap_dshow.cpp”的文件,在#include "cap_dshow.hpp"前加宏定义“#define NO_DSHOW_STRSAFE”
  4. 取消选中"Build_opencv_python3"和"BUILD_opencv_python_bindings_generator"【我Python2编译成功,但是Python3编译失败,具体还没仔细研究,暂时用不到,就先取消勾选;电脑上不要同时装有python2和python3。如果需要可以去pip3 install,不用自己编译;需要python3版的OpenCV就选中;不要python2的也可以取消选中;电脑上不要同时装有python2和python3,最后只有Python2编译成功,Python3失败。具体参考后面“踩坑笔记”】
  5. 取消勾选BUILD_opencv_world:若勾选了在生成的文件夹install\x86\mingw\lib中就只有一个libopencv_world342.dll.a;否则有很多个libxxx.dll.a,我生成的里面有17个【注意:Contrib版不能勾选这个,普通版可以勾选这个】
  6. Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo【我选的Release,生成release版;需要debug版的选debug】
  7. .cache问题:除非各位的环境和版本与我的一样,用同样的.cache文件,否则会出现这个问题mismatch_md5。解决办法参考后面“踩坑笔记”
  8. 在D:\software\OpenCV\SourceCode\opencv-4.0.0\modules\gapi\cmake\init.cmake的开头添加

OCV_OPTION(WITH_ADE "Enable ADE framework (required for Graph API module)" ON)

 

if(NOT WITH_ADE)

  return()

endif()

然后保存,关闭档案

详细请参考后面踩坑笔记【OpenCV4.0.1有修复此问题,可以改用OpenCV4.0.1编译】

9. 取消勾选BUILD_opencv_gapi【OpenCV4.0添加了全新的opencv_gapi模块。 它是非常有效的图像处理的引擎,基于处理图的lazy评估和动态构造(因此名称为Graph API或G-API)。 有关详细信息,请参见Graph API(G-API)】【OpenCV4.0.1已结修复此问题】

10. 在这个选项填入Contrib的位置【精确到modules文件夹】

OPENCV_EXTRA_MODULES_PATH:

我的路径为:D:/software/OpenCV/SourceCode/opencv_contrib-3.4.2/modules

11. 取消选中BUILD_opencv_hdf【H5Type为深度学习中的一种文件格式】

12. 勾选OPENCV_ENABLE_NON_FREE选项。【专利算法模块,包含特征检测和GPU相关的内容】

13. 在opencv_contrib-4.0.0\modules\cvv\src\qtutil\filter\sobelfilterwidget.cpp文件中添加

using namespace cv;

我的文件路径:D:\software\OpenCV\SourceCode\opencv_contrib-4.0.0\modules\cvv\src\qtutil\filter\sobelfilterwidget.cpp【OpenCV4.0.1已结修复此问题】

【OpenCV4.0已经没有这个选项了,它默认支持C++11】

  1. 勾选ENABLE_CXX11

以上详细请参考后面“踩坑笔记”。

OpenCV3.4.2 + Contrib3.4.2 + Qt5.9 + CMake3.12.1编译

资料准备

opencv:版本3.4.2

https://github.com/opencv/opencv/releases【建议下载release版】

opencv_contrib: 版本3.4.2

https://github.com/opencv/opencv_contrib/releases【建议下载release版】

Cmake3.12.1

https://github.com/Kitware/CMake/releases?after=v3.13.1

Qt 5.9.3【理论上其他5.9版本也可以】

http://download.qt.io/archive/qt/5.9/5.9.3/

开启CMake GUI

诀窍:configure#1 -> configure#2 ->  configure#3 [->  configure#4 ->  configure#5] -> Generate -> 控制台编译

需要编译Contrib3.4.2的要点五次configure,不需要的只用三次即可

Where is the source code: D:/software/OpenCV/SourceCode/opencv-3.4.2

Where to build the binaries: D:/software/OpenCV/opencv342_Qt5.9

第一次点击Configure

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第1张图片

CMake之Qt相关设置

Specify the generator for this project: MinGW Makefiles

Specify native compilers, next

Compilers C: D:\Qt\Qt5.9.3\Tools\mingw530_32\bin\gcc.exe

Compilers C++: D:\Qt\Qt5.9.3\Tools\mingw530_32\bin\g++.exe

Finish

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第2张图片

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第3张图片

CMake必要的设置

 

出现这个画面(有很多红色背景)

  1. 勾选WITH_QT,WITH_OPENGL ,不能勾选WITH_IPP(默认不勾选)
  2. 取消选中"WITH_MSMF"
  3. 勾选ENABLE_CXX11
  4. 修改“D:\software\OpenCV\SourceCode\opencv-3.4.2\modules\videoio\src\cap_dshow.cpp”的文件,在#include "cap_dshow.hpp"前加宏定义“#define NO_DSHOW_STRSAFE”
  5. BUILD_opencv_world:若勾选了在生成的文件夹install\x86\mingw\lib中就只有一个libopencv_world342.dll.a;否则有很多个libxxx.dll.a,我生成的里面有17个【注意:Contrib版不能勾选这个,普通版可以勾选这个】
  6. 取消选中"Build_opencv_python3"和"BUILD_opencv_python_bindings_generator"我Python2编译成功,但是Python3编译失败,具体还没仔细研究,暂时用不到,就先取消勾选;电脑上不要同时装有python2和python3。

2-5详细请参考后面“踩坑笔记”

第二次点击Configure

一定要保持网络畅通,会需要从(国外的)网站下载资料,你也可以提前下载好。主要是这些data, ffmpeg, ippicv, xfeatures2d等文件,下载的东西存在.cache文件夹中。

【福利】我将其共享在百度云盘,地址如下:

大家可以去下载,解压后将其放在文件夹”Where is the source code”中即可。(我的是D:\software\OpenCV\SourceCode\opencv-3.4.2)

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第4张图片

检查CMake设置

Set QT_MAKE_EXECUTABLE to D:\Qt\Qt5.9.3\5.9.3\mingw53_32\bin\qmake.exe【这个我没有碰到,Qt官网上用的CMake3.7.2应该有用】

 Set Qt5Concurrent_DIR to D:/Qt/Qt5.9.3/5.9.3/mingw53_32/lib/cmake/Qt5Concurrent

 Set Qt5Core_DIR to D:/Qt/Qt5.9.3/5.9.3/mingw53_32/lib/cmake/Qt5Core

 Set Qt5Gui_DIR to D:/Qt/Qt5.9.3/5.9.3/mingw53_32/lib/cmake/Qt5Gui

 Set Qt5Test_DIR to D:/Qt/Qt5.9.3/5.9.3/mingw53_32/lib/cmake/Qt5Test

 Set Qt5Widgets_DIR to D:/Qt/Qt5.9.3/5.9.3/mingw53_32/lib/cmake/Qt5Widgets

 Set Qt5OpenGL_DIR to D:/Qt/Qt5.9.3/5.9.3/mingw53_32/lib/cmake/Qt5OpenGL

【以上几项CMake自动帮忙配置好了,可以稍微检查下】

Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo【我选的Release,生成release版;需要debug版的选debug】

第三次点击Configure

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第5张图片

Generate

确保没有红色背景了再点击Generate,出现“Generating done”,如下图

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第6张图片

控制台编译

切换到控制台,输入以下指令

C:\Users\xxx>d:【切换到d盘,有冒号】

D:\>cd D:/software/OpenCV/opencv342_Qt5.9【Where to build the binaries】

D:\software\OpenCV\opencv342_Qt5.9>mingw32-make -j 8【加速编译】

Qt官网说明:

To speed up the compile, the -j flag can be used to run multiple compile jobs simultaneously. On an 8 core CPU, you can set it to 8 or higher, so all cores are used. On a core [email protected] with 8GB ram, the compile takes about 6 minutes.

笔者说明:

-j是指jobs,即同时编译8份jobs,意思是开8个线程同时编译,你也可以设置为4,也可以不带这个参数。

我的电脑编译普通版OpenCV只用了不到七分钟,Contrib版是半小时左右。

如果有错误,请参考“踩坑笔记”去解决,解决完再重新下这个指令。

 

D:\software\OpenCV\opencv342_Qt5.9>mingw32-make install【最后一步,生成install文件夹】

生成的内容在install文件夹中。各个细项的解释请参考

https://blog.csdn.net/yl_best/article/details/89152860#3.%20%E7%94%9F%E6%88%90%E7%9A%84%E7%BB%93%E6%9E%9C

 

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第7张图片

Contrib配置

在这个选项填入Contrib的位置【精确到modules文件夹】

OPENCV_EXTRA_MODULES_PATH:D:/software/OpenCV/SourceCode/opencv_contrib-3.4.2/modules

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第8张图片

第四次点击Configure

会出现很多红色背景的选项,

取消选中BUILD_opencv_hdf【深度学习中的一种文件格式】

勾选OPENCV_ENABLE_NON_FREE选项。【专利算法模块,包含特征检测和GPU相关的内容】

其他的不用配置了,

第五次点击Configure,出现全白色的画面,如下

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第9张图片

再点击Generate

剩下步骤同“控制台编译”

踩坑笔记

Issue#0:ffmpeg下载问题:Couldn't connect to server/Timeout

错误信息如下:

FFMPEG: Download: opencv_ffmpeg.dll

=======================================================================

Couldn't connect to server from the Internet.

Perhaps direct connections are not allowed in the current network.

To use proxy please check/specify these environment variables:

- http_proxy/https_proxy

- and/or HTTP_PROXY/HTTPS_PROXY

=======================================================================

 

CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
FFMPEG: Download failed: 7;"Couldn't connect to server"

For details please refer to the download log file:

D:/software/OpenCV/opencv400_Qt5.9/CMakeDownloadLog.txt

Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:20 (ocv_download)
cmake/OpenCVFindLibsVideo.cmake:200 (download_win_ffmpeg)
CMakeLists.txt:636 (include)

解决办法:除非各位的环境和版本与我的一样,用同样的.cache文件,否则会出现这个问题mismatch_md5;这时应该去D:/software/OpenCV/opencv400_Qt5.9/CMakeDownloadLog.txt文件中查找对应的下载地址,提前下载好所需要的.dll

例如我编译OpenCV4.0时的地址是这个

https://raw.githubusercontent.com/opencv/opencv_3rdparty/759a23e24ab787a0979f8a93103dcc3105ec10c1/ffmpeg/opencv_ffmpeg.dll

opencv_ffmpeg.dll下载完成之后,将其更改名字为849286ccc527c99e5a218b67f13c6e8c-opencv_ffmpeg.dll。

参考这句

#cmake_download "D:/software/OpenCV/SourceCode/opencv-4.0.0/.cache/ffmpeg/849286ccc527c99e5a218b67f13c6e8c-opencv_ffmpeg.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/759a23e24ab787a0979f8a93103dcc3105ec10c1/ffmpeg/opencv_ffmpeg.dll"

这些log文件中都有,大家耐心点看就可以发现。

opencv_ffmpeg_64.dll, ffmpeg_version.cmake及其他下载问题都是同样的解决办法。

 

Issue#1:勾选ENABLE_CXX11

error: 'nullptr' was not declared in this scope

   return s == nullptr || *s == 0;

解决办法:在CMake GUI上选中ENABLE_CXX11,重新Configure->Generate

Issue#3:取消勾选"WITH_MSMF"

D:\software\OpenCV\SourceCode\opencv-3.4.2\modules\videoio\src\cap_msmf.cpp:148:29: error: using invalid field '{anonymous}::ComPtr::p'

解决办法:取消选中"WITH_MSMF",如下图所示: 重新点击Configure->Generate

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第10张图片

Issue#2:在cap_dshow.cpp的文件中加宏定义“#define NO_DSHOW_STRSAFE”

D:\software\OpenCV\SourceCode\opencv-3.4.2\modules\videoio\src\cap_dshow.cpp: In member function 'void videoInput::getMediaSubtypeAsString(GUID, char*)':

D:\software\OpenCV\SourceCode\opencv-3.4.2\modules\videoio\src\cap_dshow.cpp:2184:41: error: 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope

     if( type == MEDIASUBTYPE_RGB24)     sprintf(tmpStr, "RGB24");

解决办法:修改“D:\software\OpenCV\SourceCode\opencv-3.4.2\modules\videoio\src\cap_dshow.cpp”的文件,在#include "cap_dshow.hpp"前加宏定义“#define NO_DSHOW_STRSAFE”,如下图所示:

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第11张图片

Issue#3:取消勾选"WITH_MSMF"

D:\software\OpenCV\SourceCode\opencv-3.4.2\modules\videoio\src\cap_msmf.cpp:148:29: error: using invalid field '{anonymous}::ComPtr::p'

解决办法:取消选中"WITH_MSMF",如下图所示: 重新点击Configure->Generate

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第12张图片

 

Issue#4:取消勾选BUILD_opencv_hdf

【OpenCV3.4.2错误信息】

In file included from C:/anaconda3/Library/include/hdf5.h:22:0,

                 from D:\software\OpenCV\SourceCode\opencv_contrib-3.4.2\modules\hdf\src\hdf5.cpp:37:

C:/anaconda3/Library/include/H5public.h:156:19: error: conflicting declaration 'typedef long long int ssize_t'

 typedef long long ssize_t;

modules\world\CMakeFiles\opencv_world.dir\build.make:1394: recipe for target 'modules/world/CMakeFiles/opencv_world.dir/D_/software/OpenCV/SourceCode/opencv_contrib-3.4.2/modules/hdf/src/hdf5.cpp.obj' failed

mingw32-make[2]: *** [modules/world/CMakeFiles/opencv_world.dir/D_/software/OpenCV/SourceCode/opencv_contrib-3.4.2/modules/hdf/src/hdf5.cpp.obj] Error 1

mingw32-make[2]: *** Waiting for unfinished jobs....

 

【OpenCV4.0.1错误信息】

C:/anaconda3/Library/include/H5public.h:156:19: error: conflicting declaration 'typedef long long int ssize_t'

 typedef long long ssize_t;

CV_Error_(Error::StsInternal, ("Unknown H5Type: %lld.", (long long)h5Type));

modules\hdf\CMakeFiles\opencv_hdf.dir\build.make:62: recipe for target 'modules/hdf/CMakeFiles/opencv_hdf.dir/src/hdf5.cpp.obj' failed

mingw32-make[2]: *** [modules/hdf/CMakeFiles/opencv_hdf.dir/src/hdf5.cpp.obj] Error 1

CMakeFiles\Makefile2:3205: recipe for target 'modules/hdf/CMakeFiles/opencv_hdf.dir/all' failed

mingw32-make[1]: *** [modules/hdf/CMakeFiles/opencv_hdf.dir/all] Error 2

mingw32-make[1]: *** Waiting for unfinished jobs....

解决办法:H5Type深度学习中的一种文件格式,取消勾选BUILD_opencv_hdf

 

Issue#5:取消勾选BUILD_opencv_world

错误信息:

collect2.exe: error: ld returned 1 exit status

modules\world\CMakeFiles\opencv_world.dir\build.make:11665: recipe for target 'bin/libopencv_world342.dll' failed

解决办法:取消勾选BUILD_opencv_world。编译带Contrib版的OpenCV3.4.2遇到的,不能勾选;普通版OpenCV3.4.2是没有这个问题的,可以勾选这个选项,成功build;OpenCV4.0都没有成功,取消勾选

 

编译带Contrib版的OpenCV3.4.2 world错误信息如下:

CMakeFiles\opencv_world.dir/objects.a(translationsmatchview.cpp.obj):translationsmatchview.cpp:(.text$_ZN3cvv4view20TranslationMatchViewC2ESt6vectorIN2cv8KeyPointESaIS4_EES6_S2_INS3_6DMatchESaIS7_EENS3_3MatESA_bP7QWidget+0x1492): undefined reference to `vtable for cvv::view::MatchView'
collect2.exe: error: ld returned 1 exit status
modules\world\CMakeFiles\opencv_world.dir\build.make:11665: recipe for target 'bin/libopencv_world342.dll' failed
mingw32-make[2]: *** [bin/libopencv_world342.dll] Error 1
CMakeFiles\Makefile2:4583: recipe for target 'modules/world/CMakeFiles/opencv_world.dir/all' failed

各位朋友如果有编译成功的,还请留言告知,谢谢。

在这个网站上找到的答案说下载最新版OpenCV和Contrib(提问人用的是opencv 3.1)

http://answers.opencv.org/question/94347/error-in-building-cvv-module-of-contrib/

目前还是无解,只能先取消勾选BUILD_opencv_world,不过Visual Studio下有编译成功。

大家有编译成功的还请告知,谢谢。

Issue#6:取消勾选"ENABLE_PRECOMPILED_HEADERS"

error : ... windres.exe: unknown option -- W ...【我用CMake3.12.1版没有碰到,不过Qt官网有提(用的是3.7.2),估计是旧版CMake会有碰到,我先放上来了】

解决办法:取消选中"ENABLE_PRECOMPILED_HEADERS",如下图所示: 重新点击Configure->Generate

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第13张图片

Issue#7:Python3版编译失败

第一种情况:Python2版成功,Python3版失败

[100%] Linking CXX shared module ..\..\lib\python3\cv2.cp36-win_amd64.pyd

C:/anaconda3/python36.dll: file not recognized: File format not recognized

collect2.exe: error: ld returned 1 exit status

modules\python3\CMakeFiles\opencv_python3.dir\build.make:103: recipe for target 'lib/python3/cv2.cp36-win_amd64.pyd' failed

mingw32-make[2]: *** [lib/python3/cv2.cp36-win_amd64.pyd] Error 1

CMakeFiles\Makefile2:7663: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed

解决办法:

我暂时用不到,就先取消选中 Build_opencv_python3,后面用到再重新configure。

猜测是不是安装了两个版本的Python: Python2.7.5 & Python3.6.7,有待验证。

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第14张图片

如下为Configuring Done时信息:

Python 2:

Interpreter: C:/Python27/python.exe (ver 2.7.5)

Libraries: optimized C:/Python27/libs/libpython27.a debug C:/Python27/libs/python27_d.lib (ver 2.7.5)

numpy: C:/Python27/lib/site-packages/numpy/core/include (ver 1.15.2)

packages path: C:/Python27/Lib/site-packages

 

Python 3:

Interpreter: C:/Python/Python36/python.exe (ver 3.6.7)

Libraries: C:/Python/Python36/python36.dll (ver 3.6.7)

numpy: C:/Python/Python36/lib/site-packages/numpy/core/include (ver 1.15.4)

packages path: C:/Python/Python36/Lib/site-packages

Python (for build): C:/Python27/python.exe

【更新】有两个版本的Python,但是CMake只选择了Python27版的python.exe,所以python3生成不了,

Python27我已经不需要使用,所以卸载Python27之后就变成如下了

第二种情况:只有Python3,仍然失败

Python 3:

Interpreter: C:/Python/Python36/python.exe (ver 3.6.7)

Libraries: C:/Python/Python36/python36.dll (ver 3.6.7)

numpy: C:/Python/Python36/lib/site-packages/numpy/core/include (ver 1.15.4)

packages path: C:/Python/Python36/Lib/site-packages

Python (for build): C:/Python/Python36/python.exe【有找到正确版本】

失败信息如下:

[100%] Linking CXX shared module ..\..\lib\python3\cv2.cp36-win_amd64.pyd

C:/Python/Python36/python36.dll: file not recognized: File format not recognized

collect2.exe: error: ld returned 1 exit status

modules\python3\CMakeFiles\opencv_python3.dir\build.make:103: recipe for target 'lib/python3/cv2.cp36-win_amd64.pyd' failed

mingw32-make[2]: *** [lib/python3/cv2.cp36-win_amd64.pyd] Error 1

CMakeFiles\Makefile2:7663: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed

mingw32-make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2

mingw32-make[1]: *** Waiting for unfinished jobs....

目前还是无解,只能先取消勾选,大家如有知道的麻烦留言备注,谢谢。

Issue#8.取消勾选BUILD_opencv_gapi【OpenCV4.0.0有此问题,OpenCV4.0.1已经修复】

OpenCV4.0可以configure成功,但是Generate失败,错误信息如下:

CMake Error at modules/gapi/cmake/DownloadADE.cmake:28 (add_library):
No SOURCES given to target: ade
Call Stack (most recent call first):
modules/gapi/cmake/init.cmake:10 (include)
cmake/OpenCVModule.cmake:312 (include)
cmake/OpenCVModule.cmake:375 (_add_modules_1)
modules/CMakeLists.txt:7 (ocv_glob_modules)

解决办法:在D:\software\OpenCV\SourceCode\opencv-4.0.0\modules\gapi\cmake\init.cmake的开头添加

OCV_OPTION(WITH_ADE "Enable ADE framework (required for Graph API module)" ON)

 

if(NOT WITH_ADE)

  return()

endif()

如下图:灰色背景为新增加的

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第15张图片

然后保存,关闭档案,重启CMakeGUI重头开始【如果你提前按照这个步骤改好了就不用重启了】

参考链接

https://github.com/opencv/opencv/commit/de8696aa43826510d5fd2ba1563604a29ab18a9d

路径OpenCV4.0.1 opencv/modules/gapi/cmake/

扩展说明:

OpenCV4.0添加了全新的opencv_gapi模块(OpenCV3.4.2没有gapi这个module)。 它是非常有效的图像处理的引擎,基于处理图的lazy评估和动态构造(因此名称为Graph API或G-API)。 有关详细信息,请参见Graph API(G-API)

Issue#9 'FILTER_SCHARR' was not declared in this scope

错误信息:

D:\software\OpenCV\SourceCode\opencv_contrib-4.0.0\modules\cvv\src\qtutil\filter\sobelfilterwidget.cpp:136:11: error: 'FILTER_SCHARR' was not declared in this scope

解决方法:在对应的opencv_contrib-4.0.0\modules\cvv\src\qtutil\filter\sobelfilterwidget.cpp文件中添加

using namespace cv;

参考https://github.com/opencv/opencv_contrib/issues/1914

【OpenCV4.0.1已经修复此问题】

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第16张图片

Qt+OpenCV配置

新建Qt项目

新建一个Qt项目:

打开QtCreator-> File -> New File or project -> Application | Qt Widgets Application -> Choose,如下图

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第17张图片

项目名:TestOpenCV

路径:F:\Qt_Ex

点击Next,如下图

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第18张图片

Kit Selection: 保持不变 -> 点击Next,如下图

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第19张图片

Class Information: 保持不变 -> 点击Next,如下图OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第20张图片

 

Qt + OpenCV配置

1. 添加环境变量【建议重启电脑】

我把Where to build the binaries(我的是D:/software/OpenCV/opencv342_Qt5.9)中生成的Install文件夹copy至另外一个文件夹中D:\OpenCV\opencv342Qt5.9_world,方便使用。

D:\OpenCV\opencv342Qt5.9_world\install\x86\mingw\bin

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第21张图片

2. 在TestOpenCV.pro文件中添加包含文件

INCLUDEPATH += D:\OpenCV\opencv342Qt5.9Contrib\install\include

INCLUDEPATH += D:\OpenCV\opencv342Qt5.9Contrib\install\include\opencv

INCLUDEPATH += D:\OpenCV\opencv342Qt5.9Contrib\install\include\opencv2

【注意】所有路径不要全部copy我的,除非和我的一模一样;以你编译出来的文件为准

3.在TestOpenCV.pro文件中添加library

方法一:添加所有library【编译起来比方法二稍微慢点】

LIBS += -L D:\OpenCV\opencv342Qt5.9Contrib\install\x86\mingw\lib\libopencv_*.dll.a

 

方法二:只添加需要的library

LIBS += D:\OpenCV\opencv342Qt5.9Contrib\install\x86\mingw\lib\libopencv_core342.dll.a

LIBS += D:\OpenCV\opencv342Qt5.9Contrib\install\x86\mingw\lib\libopencv_highgui342.dll.a

LIBS += D:\OpenCV\opencv342Qt5.9Contrib\install\x86\mingw\lib\libopencv_imgcodecs342.dll.a

LIBS += D:\OpenCV\opencv342Qt5.9Contrib\install\x86\mingw\lib\libopencv_imgproc342.dll.a

LIBS += D:\OpenCV\opencv342Qt5.9Contrib\install\x86\mingw\lib\libopencv_features2d342.dll.a

LIBS += D:\OpenCV\opencv342Qt5.9Contrib\install\x86\mingw\lib\libopencv_calib3d342.dll.a

 

方法三:更加正确的做法【个人推荐,比较简洁】

1.新增环境变量【建议重启电脑】

OPENCV_SDK_DIR=D:\OpenCV\opencv342Qt5.9Contrib\install

如果有多个版本的可以这样写

OPENCV342_SDK_DIR=D:\OpenCV\opencv342Qt5.9Contrib\install

OPENCV401_SDK_DIR=D:\OpenCV\opencv401Qt5.9Contrib\install

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第22张图片

2.在*.pro文件中添加包含文件

INCLUDEPATH += $$(OPENCV_SDK_DIR)/include

INCLUDEPATH += $$(OPENCV_SDK_DIR)/include/opencv

INCLUDEPATH += $$(OPENCV_SDK_DIR)/include/opencv2

3. 在*.pro文件中添加library

【注意】以下两种只要选择一种即可

# 只添加需要的library

LIBS += -L$$(OPENCV_SDK_DIR)/x86/mingw/lib \

        -lopencv_core342        \

        -lopencv_highgui342     \

        -lopencv_imgcodecs342   \

        -lopencv_imgproc342     \

        -lopencv_features2d342  \

        -lopencv_calib3d342

# 添加所有library

LIBS += -L $$(OPENCV_SDK_DIR)/x86/mingw/lib/libopencv_*.a    #import all libs

汇总如下代码,下次再有新项目直接copy就好

【注意】这个用的是方法三,按照步骤来就可以成功运行

#-------------------------------------------------
#
# Project created by QtCreator 2019-04-10T18:01:43
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TestOpenCV
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

INCLUDEPATH += $$(OPENCV_SDK_DIR)/include
INCLUDEPATH += $$(OPENCV_SDK_DIR)/include/opencv
INCLUDEPATH += $$(OPENCV_SDK_DIR)/include/opencv2

# import necessary libs
#LIBS += -L$$(OPENCV_SDK_DIR)/x86/mingw/lib \
#        -lopencv_core342        \
#        -lopencv_highgui342     \
#        -lopencv_imgcodecs342   \
#        -lopencv_imgproc342     \
#        -lopencv_features2d342  \
#        -lopencv_calib3d342

#import all libs
LIBS += -L $$(OPENCV_SDK_DIR)/x86/mingw/lib/libopencv_*.a    #import all libs

代码验证及效果图

代码1 - 简单验证普通版OpenCV

在mainwindow.cpp中书写如下代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // read an image
    cv::Mat image = cv::imread("F:/images/lena.jpg", CV_WINDOW_AUTOSIZE);
    // create image window named "My Image"
    cv::namedWindow("My Image");
    // show the image on window
    cv::imshow("My Image", image);
}

MainWindow::~MainWindow()
{
    delete ui;
}

代码1 - 效果图

【注意】有两个窗口

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第23张图片OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第24张图片

代码2 - 简单验证Contrib版OpenCV(使用SIFT特征比对)

在mainwindow.cpp中书写如下代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//命名空间
using namespace cv;
using namespace std;
using namespace xfeatures2d;

/*
class SIFT : public Feature2D {
public:
static Ptr create (
int    nfeatures         = 0,    // 最大特征数(0,则算法将返回可以找到的所有特征)
int    nOctaveLayers     = 3,    // 每个Octave(图像金字塔中的图像)应该计算多少层(不同尺度的高斯卷积)
double contrastThreshold = 0.04, // 精细筛选的阈值(to filter out weak features)
double edgeThreshold     = 10,   // 精细筛选的阈值(to filter out "edge" features)
double sigma             = 1.6   // 图像的预光滑处理
);
int descriptorSize() const;        // descriptor size, always 128
int descriptorType() const;        // descriptor type, always CV_32F
...
};
*/

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   Mat srcImage, gray, draw;
    //读入一张图片,载入图像
    srcImage = imread("F:/images/lena.jpg");     //在程序中打开一张图片
    if (!srcImage.data)
    {
        printf("could not load image...\n");
    }
    char input_title[] = "input image";
    char output_title[] = "sift image";
    namedWindow(input_title, WINDOW_AUTOSIZE);
    namedWindow(output_title, WINDOW_AUTOSIZE);

    // 显示载入的图片
    imshow(input_title, srcImage);

    //转为灰度图
    cvtColor(srcImage, gray, COLOR_BGR2GRAY);

    Mat descriptors;
    std::vector keypoints;
    //生产sift结构
    Ptr siftFD = SiftFeatureDetector::create();
    siftFD->detectAndCompute(gray, Mat(), keypoints, draw);
    drawKeypoints(gray, keypoints, gray, Scalar(0, 0, 255), DrawMatchesFlags::DEFAULT);

    //显示效果图
    imshow(output_title, gray);
}

MainWindow::~MainWindow()
{
    delete ui;
}

代码2 - 效果图

有三个窗口

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第25张图片

OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第26张图片OpenCV4.0.1/4.0.0/3.4.2 + Contrib + Qt5.9 + CMake3.12.1编译及踩坑笔记、Qt5+OpenCV配置、代码验证、效果图、福利彩蛋_第27张图片

编译所需的.cache文件  - Qt5.9+VS2013/VS2015

啰嗦下,各位的环境与软件版本除非和我的一样可以直接下载使用,否则建议提前下载,因为会出现这个问题mismatch_md5。

OpenCV3.4.2:Qt5.9+VS2013用同一份

链接:https://pan.baidu.com/s/1JxjGO-rNcUNsp6qKtGmJ8g 
提取码:rzwd 

 

OpenCV4.0.1:VS2015

链接:https://pan.baidu.com/s/16taJ6zJ_-icGYbkZJVUyOw 
提取码:9sfs 

 

OpenCV4.0.1:Qt5.9

链接:https://pan.baidu.com/s/1KoilXgJHXawAF4YFJN6dXQ 
提取码:r4ha

编译好的OpenCV版本链接 - Qt5.9+VS2013

说明:不带Contrib字样的就是普通版OpenCV,

带world字样的是编译成opencv_world.dll版本的。

下载下来的压缩包里有ReadMe.txt,上面有详细说明。

opencv342Qt5.9

链接:https://pan.baidu.com/s/1AYyPjIgKElXwcCL0_Nmr6w 
提取码:hhh1 


opencv342Qt5.9_world

链接:https://pan.baidu.com/s/1zIg-8V_7FoVYDExh_IiAKw 
提取码:xdi9 


opencv342Qt5.9Contrib

链接:https://pan.baidu.com/s/1RfEPUWP7xAFC662F1DzK3w 
提取码:7age 


opencv342Vs2013_world

链接:https://pan.baidu.com/s/1qmiP2_0Wo3k08ZSZfisPog 
提取码:ox3z 


opencv342Vs2013Contrib

链接:https://pan.baidu.com/s/15tZtVy3_wJyBks17T2WwhQ 
提取码:6n76 


opencv342Vs2013Contrib_world

链接:https://pan.baidu.com/s/1CJdgYEipTrO0ICXbUDm5Og 
提取码:65wq 


opencv401Qt5.9Contrib

链接:https://pan.baidu.com/s/1cHuPgk4Zak8mPUHCPAaBkQ 
提取码:ml82

 

OpenCV + Python版

https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv

笔者说明:以下面这个为例,4.1.0是OpenCV_Python 4.1.0版,contrib是指带Contrib模块,

cp36指Python3.6版,win_amd64是指要安装的电脑64位Windows或AMD平台

win32就是指Windows 32位。

大家按需下载。

opencv_python‑4.1.0+contrib‑cp36‑cp36m‑win_amd64.whl

 

参考文章

https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows

你可能感兴趣的:(OpenCV,Qt,OpenCV,Qt,Visual,Studio)