Qt5.13.2中配置opencv4.5.0踩坑记录

 

目录

cmake编译opencv时速度过慢或超时

 mingw32-make时报错

错误一 [modules\core\CMakeFiles\opencv_core.dir\build.make:1494: modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1

错误二 [modules\core\CMakeFiles\opencv_core.dir\build.make:1708: bin/libopencv_core450.dll] Error 1

​错误三error: 'std::_hypot' has not been declared

 在Qt中使用opencv


首先详细阅读参考链接:

OpenCV4.1.1+QT5.13+QtCreater4.9.1+MinGW730 在windows10下的环境搭建教程

官方指南

补充说明

 本机使用的各个版本:

  • qt 5.13.2
  • cmake 3.19.2
  • opencv 4.5.0
  • win10 64位

 上面三篇链接详细地介绍了安装步骤,下面是我的补充说明和踩坑记录:

cmake编译opencv时速度过慢或超时

速度过慢或者超时往往是因为opencv_videoio_ffmpeg.dll 和 opencv_videoio_ffmpeg_64.dll下载失败导致

参考这篇文章:CMake opencv时Download: opencv_ffmpeg.dll、ippicv等失败的解决方法

解决办法:将opencv_videoio_ffmpeg.dll 、 opencv_videoio_ffmpeg_64.dll和face_landmark_model.dat单独下载下来,下载地址。保存在编译后的相应的build文件中:

比如我的opencv的三个文件夹都放在D盘中,名称为:

 所以将下载好的dll文件放入D:\opencv_4.5.0-build\3rdparty\ffmpeg中即可

 Qt5.13.2中配置opencv4.5.0踩坑记录_第1张图片

将dat文件放入D:\opencv_4.5.0-build\testdata/cv/face中(需要自己创建该文件夹)

Qt5.13.2中配置opencv4.5.0踩坑记录_第2张图片

 将这三个文件提前下载好放入相应的build文件夹中,可极大地提高编译速度。

cmake编译日志:

General configuration for OpenCV 4.5.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            D:/opencv_contrib-4.5.0-source/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2020-12-19T04:08:17Z
    Host:                        Windows 10.0.19042 AMD64
    CMake:                       3.19.2
    CMake generator:             MinGW Makefiles
    CMake build tool:            D:/Mingw-w64/mingw32/bin/mingw32-make.exe
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (17 files):         + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                D:/myQT/Tools/mingw730_64/bin/g++.exe  (ver 7.3.0)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  D:/myQT/Tools/mingw730_64/bin/gcc.exe
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections  
    Linker flags (Debug):        -Wl,--gc-sections  
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 alphamat cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev freetype hdf java js julia matlab ovis python2 sfm viz
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    QT:                          YES (ver 5.13.2)
      QT OpenGL support:         NO
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.0.5-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 2.3.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      NO
      avcodec:                   NO
      avformat:                  NO
      avutil:                    NO
      swscale:                   NO
      avresample:                NO
    GStreamer:                   NO
    DirectShow:                  YES

  Parallel framework:            none

  Trace:                         YES (built-in)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                D:/opencv-4.5.0-source/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

 cuowu Python 3:
    Interpreter:                 D:/Python36/python.exe (ver 3.6.8)
    Libraries:                   D:/Python36/libs/libpython36.a (ver 3.6.8)
    numpy:                       D:/Python36/lib/site-packages/numpy/core/include (ver 1.14.5)
    install path:                D:/Python36/Lib/site-packages/cv2/python-3.6

  Python (for build):            D:/Python36/python.exe

  Java:                          
    ant:                         NO
    JNI:                         C:/Program Files (x86)/Java/jdk1.8.0_73/include C:/Program Files (x86)/Java/jdk1.8.0_73/include/win32 C:/Program Files (x86)/Java/jdk1.8.0_73/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    D:/opencv_4.5.0-build/install
-----------------------------------------------------------------

Configuring done
Generating done

 mingw32-make时报错

错误一 [modules\core\CMakeFiles\opencv_core.dir\build.make:1494: modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1

gcc: error: long: No such file or directory
D:\Mingw-w64\mingw32\bin\windres.exe: preprocessing failed.
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1494: modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:4097: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
mingw32-make: *** [Makefile:181: all] Error 2

解决办法:

将CMake中的 OPENCV/OPENCV_ENABLE_ALLOCATOR_STATS 不打勾

Qt5.13.2中配置opencv4.5.0踩坑记录_第3张图片

错误二 [modules\core\CMakeFiles\opencv_core.dir\build.make:1708: bin/libopencv_core450.dll] Error 1

D:/myQT/Tools/mingw730_64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 architecture of input file `CMakeFiles\opencv_core.dir/objects.a(vs_version.rc.obj)' is incompatible with i386:x86-64 output
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1708: bin/libopencv_core450.dll] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:4097: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
mingw32-make: *** [Makefile:181: all] Error 2

 设置OPENCV_VS_VERSIONINFO_SKIP=1,也就是打勾,若无法找到变量OPENCV_VS_VERSIONINFO_SKIP,则可以自己添加一个:

Qt5.13.2中配置opencv4.5.0踩坑记录_第4张图片错误三error: 'std::_hypot' has not been declared

In file included from D:/Python36/include/Python.h:8:0,
                 from D:\opencv-4.5.0-source\modules\python\src2\cv2.cpp:17:
D:/myQT/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/math.h:91:12: error: 'std::_hypot' has not been declared
 using std::hypot;
            ^
mingw32-make[2]: *** [modules\python3\CMakeFiles\opencv_python3.dir\build.make:82: modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:8358: modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
mingw32-make: *** [Makefile:181: all] Error 2

参考文章:Python写C/C++扩展时,编译报错:error: '::hypot' has not been declared

具体步骤:找到自己报错的目标头文件,我这里是: D:/Python36/include/Python.h,打开并修改Python.h头文件,在文件开头添加#include

最后的编译日志

D:\opencv_4.5.0-build>mingw32-make -j1
[  1%] Built target zlib
[  4%] Built target libjpeg-turbo
[  6%] Built target libtiff
[ 11%] Built target libwebp
[ 11%] Built target libopenjp2
[ 12%] Built target libpng
[ 16%] Built target IlmImf
[ 20%] Built target libprotobuf
[ 20%] Built target quirc
[ 21%] Built target ade
[ 21%] Built target opencv_videoio_plugins
[ 26%] Built target opencv_core
[ 30%] Built target opencv_imgproc
[ 31%] Built target opencv_imgcodecs
[ 32%] Built target opencv_videoio
[ 33%] Built target opencv_highgui
[ 34%] Built target opencv_ts
[ 36%] Built target opencv_test_core
[ 37%] Built target opencv_perf_core
[ 37%] Built target opencv_flann
[ 37%] Built target opencv_test_flann
[ 40%] Built target opencv_test_imgproc
[ 41%] Built target opencv_perf_imgproc
[ 41%] Built target opencv_intensity_transform
[ 41%] Built target opencv_test_intensity_transform
[ 42%] Built target opencv_ml
[ 43%] Built target opencv_test_ml
[ 43%] Built target opencv_phase_unwrapping
[ 43%] Built target opencv_test_phase_unwrapping
[ 44%] Built target opencv_photo
[ 44%] Built target opencv_test_photo
[ 44%] Built target opencv_perf_photo
[ 44%] Built target opencv_plot
[ 45%] Built target opencv_quality
[ 45%] Built target opencv_test_quality
[ 45%] Built target opencv_reg
[ 45%] Built target opencv_test_reg
[ 45%] Built target opencv_perf_reg
[ 45%] Built target opencv_surface_matching
[ 46%] Built target opencv_xphoto
[ 47%] Built target opencv_test_xphoto
[ 47%] Built target opencv_perf_xphoto
[ 52%] Built target opencv_dnn
[ 52%] Built target opencv_test_dnn
[ 52%] Built target opencv_perf_dnn
[ 52%] Built target opencv_dnn_superres
[ 53%] Built target opencv_test_dnn_superres
[ 53%] Built target opencv_perf_dnn_superres
[ 54%] Built target opencv_features2d
[ 55%] Built target opencv_test_features2d
[ 56%] Built target opencv_perf_features2d
[ 57%] Built target opencv_fuzzy
[ 57%] Built target opencv_test_fuzzy
[ 57%] Built target opencv_hfs
[ 57%] Built target opencv_img_hash
[ 57%] Built target opencv_test_img_hash
[ 57%] Built target opencv_test_imgcodecs
[ 58%] Built target opencv_perf_imgcodecs
[ 59%] Built target opencv_line_descriptor
[ 59%] Built target opencv_test_line_descriptor
[ 59%] Built target opencv_perf_line_descriptor
[ 60%] Built target opencv_saliency
[ 60%] Built target opencv_test_saliency
[ 60%] Built target opencv_text
[ 60%] Built target opencv_test_text
[ 61%] Built target opencv_test_videoio
[ 61%] Built target opencv_perf_videoio
[ 63%] Built target opencv_calib3d
[ 64%] Built target opencv_test_calib3d
[ 65%] Built target opencv_perf_calib3d
[ 65%] Automatic MOC for target opencv_cvv
[ 65%] Built target opencv_cvv_autogen
[ 68%] Built target opencv_cvv
[ 68%] Automatic MOC for target opencv_test_cvv
[ 68%] Built target opencv_test_cvv_autogen
[ 69%] Built target opencv_test_cvv
[ 71%] Built target opencv_datasets
[ 71%] Built target opencv_test_highgui
[ 71%] Built target opencv_mcc
[ 71%] Built target opencv_test_mcc
[ 71%] Built target opencv_objdetect
[ 71%] Built target opencv_test_objdetect
[ 72%] Built target opencv_perf_objdetect
[ 72%] Built target opencv_rapid
[ 73%] Built target opencv_test_rapid
[ 74%] Built target opencv_rgbd
[ 74%] Built target opencv_test_rgbd
[ 74%] Built target opencv_perf_rgbd
[ 74%] Built target opencv_shape
[ 74%] Built target opencv_test_shape
[ 75%] Built target opencv_structured_light
[ 75%] Built target opencv_test_structured_light
[ 75%] Built target opencv_video
[ 75%] Built target opencv_test_video
[ 76%] Built target opencv_perf_video
[ 77%] Built target opencv_videostab
[ 77%] Built target opencv_test_videostab
[ 78%] Built target opencv_xfeatures2d
[ 78%] Built target opencv_test_xfeatures2d
[ 78%] Built target opencv_perf_xfeatures2d
[ 80%] Built target opencv_ximgproc
[ 81%] Built target opencv_test_ximgproc
[ 82%] Built target opencv_perf_ximgproc
[ 82%] Built target opencv_xobjdetect
[ 82%] Built target opencv_waldboost_detector
[ 82%] Built target opencv_aruco
[ 83%] Built target opencv_test_aruco
[ 83%] Built target opencv_bgsegm
[ 83%] Built target opencv_test_bgsegm
[ 84%] Built target opencv_bioinspired
[ 84%] Built target opencv_test_bioinspired
[ 84%] Built target opencv_perf_bioinspired
[ 84%] Built target opencv_ccalib
[ 84%] Built target opencv_dnn_objdetect
[ 84%] Built target opencv_dpm
[ 85%] Built target opencv_face
[ 85%] Built target opencv_test_face
[ 88%] Built target opencv_gapi
[ 92%] Built target opencv_test_gapi
[ 92%] Built target opencv_perf_gapi
[ 93%] Built target opencv_optflow
[ 94%] Built target opencv_test_optflow
[ 94%] Built target opencv_perf_optflow
[ 94%] Built target opencv_stitching
[ 95%] Built target opencv_test_stitching
[ 95%] Built target opencv_perf_stitching
[ 95%] Built target opencv_superres
[ 95%] Built target opencv_test_superres
[ 95%] Built target opencv_perf_superres
[ 97%] Built target opencv_tracking
[ 98%] Built target opencv_test_tracking
[ 98%] Built target opencv_perf_tracking
[ 99%] Built target opencv_stereo
[ 99%] Built target opencv_test_stereo
[100%] Built target opencv_perf_stereo
[100%] Built target gen_opencv_python_source
Scanning dependencies of target opencv_python3
[100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj
In file included from D:/opencv-4.5.0-source/modules/gapi/include/opencv2/gapi/own/mat.hpp:14:0,
                 from D:/opencv-4.5.0-source/modules/gapi/include/opencv2/gapi/garg.hpp:15,
                 from D:/opencv-4.5.0-source/modules/gapi/include/opencv2/gapi/gcall.hpp:11,
                 from D:/opencv-4.5.0-source/modules/gapi/include/opencv2/gapi/gkernel.hpp:20,
                 from D:/opencv-4.5.0-source/modules/gapi/include/opencv2/gapi/core.hpp:19,
                 from D:/opencv_4.5.0-build/modules/python_bindings_generator/pyopencv_generated_include.h:144,
                 from D:\opencv-4.5.0-source\modules\python\src2\cv2.cpp:38:
D:/opencv-4.5.0-source/modules/gapi/include/opencv2/gapi/own/saturate.hpp:71:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable: 4244)

D:/opencv-4.5.0-source/modules/gapi/include/opencv2/gapi/own/saturate.hpp:75:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(default: 4244)

[100%] Linking CXX shared module ..\..\lib\python3\cv2.cp36-win_amd64.pyd
[100%] Built target opencv_python3
[100%] Built target opencv_annotation
[100%] Built target opencv_visualisation
[100%] Built target opencv_interactive-calibration
[100%] Built target opencv_version
[100%] Built target opencv_version_win32

最后出现这句:[100%] Built target opencv_version_win32,就说明编译成功了

补充说明:在mingw32-make之后加上 -j4 (使用四个线程,数字可以自己改)确实可以提高编译速度。

mingw32-make -j4

 但是有一次跑到【100%】提示出错

[100%] Built target opencv_test_gapi

mingw32-make: *** [all] Error 2

却没有提示错误信息,考虑将线程数改为1:

mingw32-make -j1

之后错误信息可以被打印出来,推测可能是速度太快导致错误信息还没来得及打印,程序就退出了,所以当遇到编译的错误信息没有打印出来时(只提示Error),考虑将线程数置为1

 在Qt中使用opencv

在文章开头的第一篇文章OpenCV4.1.1+QT5.13+QtCreater4.9.1+MinGW730 在windows10下的环境搭建教程中最后提到的加载dll文件时的方法并不适合不同版本的opencv,

INCLUDEPATH += $$(OPENCV_SDK_DIR)/include

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

-lopencv_core411 \

-lopencv_highgui411 \

-lopencv_imgcodecs411 \

-lopencv_imgproc411 \

-lopencv_features2d411 \

-lopencv_calib3d411

 上面这段代码的意思就是添加include文件夹和依赖库文件。也就是:

D:\opencv_4.5.0-build\install\include\opencv2

D:\opencv_4.5.0-build\install\x64\mingw\lib

这两个文件夹中的内容。

Qt5.13.2中配置opencv4.5.0踩坑记录_第5张图片

其中以411结尾是因为那篇文章的作者使用的是opencv4.1.1,而我使用的是opencv4.5.0,所以我的依赖库文件是以450结尾的。

知道了原理,我们就可以直接在pro文件中添加这两个文件夹:

INCLUDEPATH += D:\opencv_4.5.0-build\install\include
LIBS += D:\opencv_4.5.0-build\install\x64\mingw\bin\libopencv_*.dll
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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 += D:\opencv_4.5.0-build\install\include
LIBS += D:\opencv_4.5.0-build\install\x64\mingw\bin\libopencv_*.dll

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

最后一点要注意,

#include
using namespace cv;

 在添加头文件之后,一定要使用opencv的命名空间using namespace cv; 否则会导致opencv中的变量类型和函数名无法被识别。

编译成功的opencv动态链接库:cmake_opencv450_mingw73_64_编译生成的动态链接库文件_install.zip

你可能感兴趣的:(Qt,OpenCV,cmake,makefile)