OpenCv4.4.0+Qt5.12.2+OpenCv-Contrib-4.4.0

OpenCv4.4.0+Qt5.12.2+OpenCv-Contrib-4.4.0编译配置

简介

系统环境

System:Win10_64

  
 
  • 1

IDE版本

QtCreator:4.8.2
Qt:5.12.2
CMake:3.17.1
OpenCv:4.4.0

  
 
  • 1
  • 2
  • 3
  • 4

下载

OpenCv

OpenCv:OpenCv下载列表

OpenCv_4.4.0:OpenCv4.4.0

来自于某一个博主:

OpenCv:国内索引列表

OpenCv:OpenCv4.4.0

官网链接就不贴了。

OpenCv_Xontrib_4.4.0

OpenCv_contrib:OpenCv_contrib

OpenCv_contrib_4.4.0:OpenCv_Contrib_4.4.0

Qt5.12.2

Qt:Qt官网下载地址

Qt5.12.2:Qt5.12.2.exe

CMake

CMake:CMakeDownload

CMake3.17.1:GitHubCmake3.17.1

CMake3.17.1.msi:cmake-3.17.1-win64-x64.msi

安装

Opencv

配置图片

双击打开 opencv-4.4.0-vc14_vc15.exe,选择自己的安装路径。笔者:**E:\**E盘根目录

安装完为: E:\opencv

OpenCv-Contrib-4.4.0

解压opencv_contrib-4.4.0.zip,解压至 E:\opencv目录下

为:‘’

 目录: E:\opencv


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 9:30 build
d----- 2020/9/27 9:39 opencv_contrib-4.4.0
d----- 2020/9/27 9:34 sources
-a---- 2020/7/18 7:23 2336 LICENSE.txt
-a---- 2020/7/18 7:23 27913 LICENSE_FFMPEG.txt
-a---- 2020/7/18 7:23 731 README.md.txt

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Qt5.12.2

正常的双击安装。注意选择MinGw。安装目录为 E:\Qt

 目录: E:\Qt


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/8/15 17:08 Qt5.12.2
d----- 2020/8/15 12:03 Qt5.9.2

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意:如果没有Qt帐号需要断网安装,就可以点击skip 跳过登录界面。或者自己注册帐号登录也可以。根据自己需要配置一些依赖库。

CMake

双击安装。

编译

MinGw_64

CMake配置

当前目录结构

 目录: E:\opencv


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 9:30 build
d----- 2020/9/27 9:39 opencv_contrib-4.4.0
d----- 2020/9/27 9:34 sources
-a---- 2020/7/18 7:23 2336 LICENSE.txt
-a---- 2020/7/18 7:23 27913 LICENSE_FFMPEG.txt
-a---- 2020/7/18 7:23 731 README.md.txt

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

创建编译目录,安装目录

PS E:\opencv> mkdir MinGw_64 目录: E:\opencv


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 12:42 MinGw_64


PS E:\opencv> mkdir MinGw_64_build 目录: E:\opencv


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 12:42 MinGw_64_build

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

配置 source code 和 build the binaries

在这里插入图片描述

点击Configure

工程类型配置

工程类型选则: MinGw Makeifile

编译器选择:QtTools 下的 MinGw/bin gcc and g++
在这里插入图片描述
在这里插入图片描述
configure配置

等待配置完成 显示 Configuring done

配置安装路径

CMAKE_INSTALL_PREFIX  E:/opencv/MinGw_64

  
 
  • 1

配置modules

OPENCV_EXTRA_MODULES_PATH  E:/opencv/opencv_contrib-4.4.0/modules

  
 
  • 1

在CMakeGui Search栏 搜索 CMAKE_INSTALL_PREFIXOPENCV_EXTRA_MODULES_PATH,即可找到对应的配置项。

配置支持Qt

WITH_QT	TRUE

  
 
  • 1

在CMakeGui Search栏 搜索 WITH_QT 即可找到对应的配置项。

配置OPENGL

WITH_OPENGL TRUE

  
 
  • 1

取消Test编译

BUILD_PERE_TESTS FALSE
BUILD_TESTS FALSE
BUILD_opencv_python_tests FLASE

  
 
  • 1
  • 2
  • 3
OPENCV_ENABLE_ALLOCATOR_STATS FALSE
OPENCV_GENERATE_SETUPVARS FALSE

  
 
  • 1
  • 2

在CMakeGui Search栏 搜索 test 即可找到对应的配置项,全部取消

配置完成后继续点击Configure

QT5_DIR配置

在点击Configure 之后 会有如下错误提示:

CMake Error at cmake/OpenCVFindLibsGUI.cmake:18 (find_package):
  Could not find a package configuration file provided by "Qt5" with any of
  the following names:
Qt5Config.cmake qt5-config.cmake Qt5Config.cmake Add the installation prefix of "Qt5" to COPENCV_ENABLE_ALLOCATOR_STATSMAKE_PREFIX_PATH or set "Qt5_DIR"
  to a directory containing one of the above files.  If "Qt5" provides a
  separate development package or SDK, be sure it has been installed.
Call Stack (most recent call first):
  CMakeLists.txt:686 (include)
Configuring incomplete, errors occurred!

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

该错误是没有找到Qt5 的 安装路径。

在Everything 搜索 Qt5Config.cmake,即可找到对应路径。

 目录: E:\Qt\Qt5.12.2\5.12.2\mingw73_64\lib\cmake\Qt5


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019/3/12 17:49 1606 Qt5Config.cmake
-a---- 2019/3/12 17:49 288 Qt5ConfigVersion.cmake
-a---- 2019/3/12 17:49 249 Qt5ModuleLocation.cmake

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
Qt5_DIR E:\Qt\Qt5.12.2\5.12.2\mingw73_64\lib\cmake\Qt5

  
 
  • 1

配置完成之后,继续点击Configure

等到 Configuring done。继续配置。

取消

BUILD_opencv_rgbd FALSE
BUILD_opencv_python3 FALSE
BUILD_opencv_python3_building_generator FALSE
BUILD_opencv_rapid FALSE
OPENCV_GENERATE_SETUPVARS FALSE
OPENCV_ENABLE_ALLOCATOR_STATS FALSE

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置完成之后,继续点击Configure

等到 Configuring done.

检测下载

 目录: E:\opencv\MinGw_64_build\downloads\xfeatures2d


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/27 14:04 14852 boostdesc_bgm.i
-a---- 2020/9/27 14:04 14864 boostdesc_bgm_bi.i
-a---- 2020/9/27 14:04 7832 boostdesc_bgm_hd.i
-a---- 2020/9/27 14:04 138093 boostdesc_binboost_064.i
-a---- 2020/9/27 14:04 275322 boostdesc_binboost_128.i
-a---- 2020/9/27 14:04 549754 boostdesc_binboost_256.i
-a---- 2020/9/27 14:04 426280 boostdesc_lbgm.i
-a---- 2016/3/17 20:49 1257733 vgg_generated_120.i
-a---- 2016/3/17 20:49 773330 vgg_generated_48.i
-a---- 2016/3/17 20:49 915012 vgg_generated_64.i
-a---- 2016/3/17 20:49 1012932 vgg_generated_80.i

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
 目录: E:\opencv\MinGw_64_build\3rdparty\ffmpeg


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/27 14:03 361 ffmpeg_version.cmake
-a---- 2020/9/27 13:24 21213184 opencv_videoio_ffmpeg.dll
-a---- 2020/9/27 13:31 22100992 opencv_videoio_ffmpeg_64.dll

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
 目录: E:\opencv\MinGw_64_build\testdata\cv\face


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/27 13:40 72368157 face_landmark_model.dat

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在如上目录查看是都有对应的文件,没有则打开

E:\opencv\MinGw_64_build\CMakeDownloadLog.txt

  
 
  • 1

找到下载路径,下载对应的文件。

编译

管理员权限打开 powershell ,执行如下命令:

PS E:\opencv\MinGw_64_build> E:\Qt\Qt5.12.2\Tools\mingw730_64\bin\mingw32-make.exe -j8

  
 
  • 1
E:\Qt\Qt5.12.2\Tools\mingw730_64\bin\mingw32-make.exe -j8

  
 
  • 1

mingw32-make.exe 路径为对应 Qt mingw 安装路径。

相关错误

E:/Qt/Qt5.12.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/math.h:36:1: error: 'include' does not name a type
 include <cmath>

  
 
  • 1
  • 2
gcc: error: long: No such file or directory
E:\Qt\Qt5.12.2\Tools\mingw730_64\bin\windres.exe: preprocessing failed.

  
 
  • 1
  • 2
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
  CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
  directory
Call Stack (most recent call first):
  CMakeLists.txt:968 (include)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

Qt工程测试Demo

随便拿了之前的一段代码,图片合并或者说全景图拼接的。

工程目录

 目录: D:\8-Project\Qt-Project\opencv_hello


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/26 20:49 528828 01.png
-a---- 2020/9/26 20:49 519384 02.png
-a---- 2020/9/26 20:31 183 main.cpp
-a---- 2020/9/28 1:01 1843 mainwindow.cpp
-a---- 2020/9/26 21:02 343 mainwindow.h
-a---- 2020/9/26 21:01 1349 mainwindow.ui
-a---- 2020/9/28 0:52 1319 opencv_hello.pro
-a---- 2020/9/28 0:48 14674 opencv_hello.pro.user

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

libdir / includepath 配置

在Pro文件内部添加:

INCLUDEPATH += E:\opencv\MinGw_64\include\
INCLUDEPATH += E:\opencv\MinGw_64\include\opencv2\

win32:CONFIG(release, debug|release): LIBS += E:/opencv/MinGw_64/x64/mingw/lib/libopencv_*.a

  
 
  • 1
  • 2
  • 3
  • 4

因为只编译了 release 版本,所以只添加了Release

源码

bool MainWindow::OpencvStitch()
{
	//载入图片 cv::Mat fImage = cv::imread("./01.png"); cv::Mat sImage = cv::imread("./02.png");
	//图片列表 std::vector<cv::Mat> imgs; imgs.push_back(fImage); imgs.push_back(sImage);
	//图片生成 cv::Mat outImage; cv::Ptr<cv::Stitcher> stitcher = cv::Stitcher::create(); cv::Stitcher::Status status = stitcher->stitch(imgs,outImage);
	//图片合成结果 if (status != cv::Stitcher::Status::OK) { QString errmsg = ""; if (status == cv::Stitcher::Status::ERR_NEED_MORE_IMGS) errmsg = "image no overlapping area"; else if (status == cv::Stitcher::Status::ERR_HOMOGRAPHY_EST_FAIL) errmsg = "image no match "; else errmsg = "image parameter processing failed "; qDebug() << "stitch imgs fail! : " << errmsg; return false; } else { //重定向图片 cv::Mat stitchImage = cv::Mat::zeros(fImage.rows, fImage.cols, CV_8UC3); cv::resize(outImage, stitchImage, stitchImage.size()); //Opencv与Qt格式转换 有默认 BGR-> RGB cv::Mat qLabelImg; cv::cvtColor(stitchImage,qLabelImg,cv::COLOR_BGR2RGB); //生成QImage QImage qImg = QImage((const unsigned char*)(qLabelImg.data),qLabelImg.cols,qLabelImg.rows,qLabelImg.step,QImage::Format_RGB888); //QLabel显示图片 ui->label->setPixmap(QPixmap::fromImage(qImg)); ui->label->resize(qImg.size()); qDebug() << ui->label->size(); ui->label->show(); return true; }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

运行结果

源图片
运行结果

文章来源: blog.csdn.net,作者:何其不顾四月天,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/u011218356/article/details/108858522

(完)