软件平台:Windows 10
、CMake 3.13.4
和Qt 5.11.3
下载Windows版本的OpenCV-3.1.0:https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.1.0/opencv-3.1.0.exe/download
下载的文件为:opencv-3.1.0.exe
,其是一个压缩文件,双击之,将其解压至一个选定的文件夹中。如下为项目的文件结构,其中,一个为build
文件夹,一个为sources
文件夹,build
为一个标准的项目构建目录,它适用于一般的情况。而如果build
不适用时,可选择编译sources
中的源代码,以生成一个适合自己系统使用的,和build
类似的构建目录。
本系统将使用的,用于编译项目的Qt编译套件为:Qt 5.11.3 MinGW 5.3.0 32bit
,为了顾名思义,在与build
的同路径中,新建一个构建目录mingw530
,以存储相关的编译文件和安装文件。
由于OpenCV是一个cmake项目,所以要编译OpenCV源码,需要先安装cmake工具。在安装了cmake工具后,可通过如下的步骤,对opencv项目进行构建与分析。
打开CMake(cmake-gui)
选定源代码路径和构建目录路径
第一行为源代码路径
,选择..\opencv\sources
文件夹,本系统的为:D:\Software\opencv\sources
。第二行为项目构建目录
,此目录存储了cmake、make和install输出的相关文件,路径位置可自选,本系统的为:D:\Software\opencv\mingw530
。
选定源代码路径和构建目录路径后,点击Configure
,以配置项目。第一次点击Configure
时,会出现如下的界面,用于设置生成器和编译器。
选择MinGW Makefiles
和Specify native compilers
,并点击Next
。
为项目选择代码编译器,此处将选择Qt中Tools下Qt 5.11.3 MinGW 5.3.0 32bit
编译套件中的C/C++编译器。本系统的C/C++编译器路径为:C编译器:D:/Software/Qt/Qt5.11.3/Tools/mingw530_32/bin/gcc.exe
,C++编译器:D:/Software/Qt/Qt5.11.3/Tools/mingw530_32/bin/g++.exe
,具体的路径可根据Qt安装路径修改。点击Finish
。
。
等待一会,待Configure
完后,在输出的cmake选项中,选择WITH_QT
(选择Qt支持)和WITH_OPENGL
,然后,再一次点击Configure
进行项目配置。配置结束后,点击Generate
,以生成用于编译的相关文件。待Configure
和Generate
这两步完成后,关闭掉CMake(cmake-gui)
界面,然后开始对项目进行编译与安装。
本系统将使用Qt 5.11.3 MinGW 5.3.0 32bit
编译套件编译与安装opencv项目,其编译和安装命令分别为:mingw32-make
和mingw32-make install
定位
# 打开命令行窗口,并定位至项目的构建目录
# 本系统的在:D:\Software\opencv\mingw530
#
cd d:
cd Software/opencv/mingw530
编译
mingw32-make
安装
# 项目的安装目录默认在构建目录下的install文件夹中
# 本系统的在:D:/Software/opencv/mingw530/install
#
mingw32-make install
在系统环境变量Path
中加入:D:\Software\opencv\mingw530\install\x86\mingw\bin;D:\Software\opencv\mingw530\bin;
如此,对opencv的编译、安装与配置就完成了!
如下为构建目录mingw530
的文件结构,其中,文件夹install
就存储了我们以后将要使用的相关安装文件
打开项目的安装文件夹install
,如下为install
的文件结构,其中,include
中包含的是项目头文件,x86
中包含的是项目库文件。而头文件与库文件就是调用一个库所必需的两类型文件。
要在程序中使用opencv,只要在项目中添加相关头文件与库文件的路径即可,如下即为opencv项目的相关头文件与库文件
要在Qt项目中使用opencv,需要在项目的项目管理文件.pro
中,添加以下代码,以完成往程序中添加相关头文件与库文件的操作(这些代码包含了opencv项目的所有头文件与库文件)
# 添加opencv项目的所有头文件
#
INCLUDEPATH += D:\Software\opencv\mingw530\install\include \
D:\Software\opencv\mingw530\install\include\opencv \
D:\Software\opencv\mingw530\install\include\opencv2
# 添加opencv项目的所有库文件
#
LIBS += D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_calib3d310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_core310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_features2d310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_flann310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_highgui310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_imgcodecs310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_imgproc310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ml310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_objdetect310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_photo310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_shape310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_stitching310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_superres310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ts310.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_video310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_videoio310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_videostab310.dll.a
基于qmake构建工具,新建一个Qt控制台应用(项目管理文件的后缀为:.pro
):test
新建文件或项目–>Application–>Qt Console Application–>项目名称:test–>一路next下去
测试项目的结构
./test
|
+---build
|
+---images
| |
| +---1.jpg
|
+---main.cpp
|
+---test.pro
|
+---test.pro.user
代码清单:main.cpp
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
cv::Mat img;
//根据图像的目录进行修改,记得用转义字符'\\'
string filePath="..\\images\\1.jpg";
img =cv::imread(filePath,IMREAD_COLOR);
//判断图像文件是否正确读取
if(img.data==nullptr)
{
cerr<<"文件:"<<filePath<<",不存在!"<<endl;
return 0;
}
//读取图像的宽、高和通道信息
cout<<"图像宽为:"<<img.cols<<",";
cout<<"高为:"<<img.rows<<",";
cout<<"通道为:"<<img.channels()<<endl;
cv::namedWindow("An image",WINDOW_NORMAL);
cv::resizeWindow("An image",800,450);
cv::imshow("An image",img);
cv::waitKey(0);
return 0;
}
往项目的项目管理文件test.pro
中添加以下的opencv头文件与库文件包含代码
,这些包含代码
可根据opencv项目安装情况的不同而做出相应的修改
INCLUDEPATH += D:\Software\opencv\mingw530\install\include \
D:\Software\opencv\mingw530\install\include\opencv \
D:\Software\opencv\mingw530\install\include\opencv2
LIBS += D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_calib3d310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_core310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_features2d310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_flann310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_highgui310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_imgcodecs310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_imgproc310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ml310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_objdetect310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_photo310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_shape310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_stitching310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_superres310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ts310.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_video310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_videoio310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_videostab310.dll.a
程序的运行结果
下载opencv-3.1.0对应的opencv_contrib-3.1.0模块:https://github.com/opencv/opencv_contrib/tree/3.1.0
。下载下来的是一个压缩包:opencv_contrib-3.1.0.zip,将其解压后,把其放到opencv的源代码中。
打开cmake-gui,选择之前安装opencv-3.1.0时设置好的源码路径和构建目录路径。选择好相应的路径后,会出现如下的界面,在中间的方框中,定位于OPENCV_EXTRA_MODULES_PATH
项,在其后面添加opencv_contrib中的模块路径,本系统的为:D:\Software\opencv\sources\opencv_contrib-3.1.0\modules
,注意,是精确至\modules
。设置好路径后,点击界面左下方的Configure
,待配置完后,如果中间处可能还会有红色的块,那继续点击Configure
进行配置(一般会点击两次Configure
),直到中间处的所有项都变白为止。至此,进行下一步,点击Generate
,以生成相应的用于编译的文件,如果结束后,提示Generate Done
,那么整个安装opencv_contrib模块的过程就成功一半了。
打开一个命令行窗口,并定位于opencv的构建目录,然后,输入编译命令:mingw32-make
对项目进行编译。
编译成功后,输入安装命令:mingw32-make install
对项目进行安装。
安装成功后,opencv_contrib模块就编译且安装成功了。
如下为安装了opencv_contrib模块后的库文件夹中的内容。与安装opencv_contrib模块之前相比,无疑增加了许多内容。
在Qt项目中使用opencv时,往项目的项目管理文件.pro
中,添加的头文件与库文件包含命令需作出如下的更新。
# 添加opencv项目的所有头文件
#
INCLUDEPATH += D:\Software\opencv\mingw530\install\include \
D:\Software\opencv\mingw530\install\include\opencv \
D:\Software\opencv\mingw530\install\include\opencv2
# 添加opencv项目的所有库文件
#
LIBS += D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_aruco310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_bgsegm310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_bioinspired310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_calib3d310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ccalib310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_core310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_cvv310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_datasets310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_dnn310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_dpm310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_face310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_features2d310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_flann310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_fuzzy310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_highgui310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_imgcodecs310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_imgproc310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_line_descriptor310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ml310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_objdetect310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_optflow310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_photo310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_plot310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_reg310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_rgbd310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_saliency310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_shape310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_stereo310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_stitching310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_structured_light310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_superres310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_surface_matching310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_text310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_tracking310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ts310.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_video310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_videoio310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_videostab310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_xfeatures2d310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_ximgproc310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_xobjdetect310.dll.a \
D:\Software\opencv\mingw530\install\x86\mingw\lib\libopencv_xphoto310.dll.a
参考文档
windows10下安装opencv3.2+QT Creator+MingGW:https://blog.csdn.net/a8039974/article/details/80572806