OpenCV环境配置:OpenCV-4.0.1+OpenCV_contrib-4.0.1+VS2017编译配置记录
- 1.下载opencv源码和contrib模块源码以及CMake
- 2.解压源码压缩包并配置cmake
- 3.使用VS2017进行编译
- 4.配置VS
- 4.1配置环境变量
- 4.2VS配置
- 5.附录
- 6.Windows下QtCreaor中配置OpenCV
1.下载opencv源码和contrib模块源码以及CMake
-
opencv
源码下载https://github.com/opencv/opencv/archive/4.0.1.zip -
contrib
模块源码下载https://github.com/opencv/opencv_contrib/archive/4.0.1.zip -
CMake
下载https://github.com/Kitware/CMake/releases/download/v3.14.1/cmake-3.14.1.zip
2.解压源码压缩包并配置cmake
如下图:
其中where is the source code
就是解压后的opencv源码目录,where to build the binaries
就是自己创建的目标目录。选定好后点击Configure
出现如上图的对话框,然后选择2017 win64
,点击Finish
。
出现如下图后即Configure
完成:
Configure
完成后在OPENCV_EXTRA_MODULES_PATH
中选择contrib
目录中的modules
,如下图:
再次点击Configure
至Configure done
后点击Generate
。
最后如下图:
3.使用VS2017进行编译
使用VS2017
打开构建目录中的OpenCV.sln
,然后点击重新构建解决方案,完成后如下图所示:
若遇到缺失python37_d.lib
可以参考此篇博客:缺少Python27_d.lib的解决方法。到CMakeTargets
中的INSTALL
,然后右键选择仅限于项目
–>仅生成INSTALL
,如下图:
然后结果如下图:
可以在下面的目录中看到生成的库文件:
4.配置VS
4.1配置环境变量
右键此电脑
->属性
->高级系统设置
->环境变量
,在系统变量path
中添加F:\opencv_4.0.1_build\install\x64\vc15\bin
,如下图:
4.2VS配置
新建一个空的DEBUG X64
项目,点击菜单栏的"视图"–>“其他窗口”–>“属性管理器”。
在Debug|X64
–>Microsoft.Cpp.x64.user
–> VC++目录
–>包含目录和VC++目录
中加入:
- F:\opencv_4.0.1_build\install\include
- F:\opencv_4.0.1_build\install\include\opencv2
注意opencv 4.0.1
中没有F:\opencv_4.0.1_build\install\include\opencv
。
在Debug|X64
–>Microsoft.Cpp.x64.user
–>VC++目录
–>库目录
中加入
- F:\opencv_4.0.1_build\install\x64\vc15\lib
如下图:
点击应用
–>确定
。
在Debug|X64
–>Microsoft.Cpp.x64.user
–>链接器
->输入
->附加依赖项
中添加:
opencv_aruco401d.lib
opencv_bgsegm401d.lib
opencv_bioinspired401d.lib
opencv_calib3d401d.lib
opencv_ccalib401d.lib
opencv_core401d.lib
opencv_datasets401d.lib
opencv_dnn401d.lib
opencv_dnn_objdetect401d.lib
opencv_dpm401d.lib
opencv_face401d.lib
opencv_features2d401d.lib
opencv_flann401d.lib
opencv_fuzzy401d.lib
opencv_gapi401d.lib
opencv_hdf401d.lib
opencv_hfs401d.lib
opencv_highgui401d.lib
opencv_imgcodecs401d.lib
opencv_imgproc401d.lib
opencv_img_hash401d.lib
opencv_line_descriptor401d.lib
opencv_ml401d.lib
opencv_objdetect401d.lib
opencv_optflow401d.lib
opencv_phase_unwrapping401d.lib
opencv_photo401d.lib
opencv_plot401d.lib
opencv_reg401d.lib
opencv_rgbd401d.lib
opencv_saliency401d.lib
opencv_shape401d.lib
opencv_stereo401d.lib
opencv_stitching401d.lib
opencv_structured_light401d.lib
opencv_superres401d.lib
opencv_surface_matching401d.lib
opencv_text401d.lib
opencv_tracking401d.lib
opencv_video401d.lib
opencv_videoio401d.lib
opencv_videostab401d.lib
opencv_xfeatures2d401d.lib
opencv_ximgproc401d.lib
opencv_xobjdetect401d.lib
opencv_xphoto401d.lib
注:可以使用如下python脚本打印出所有文件名
import os
for name in os.listdir('./'):
print(name)
至此环境配置完成,最好重启一下电脑让其生效。
5.附录
测试代码:
#include<opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char**argv)
{
Mat img = imread("test.jpg");
if (img.empty())
{
cout << "load image erroe!" << endl;
extit(-1);
}
else
{
imshow("test", img);
waitKey(0);
destroyAllWindows();
}
return 0;
}
测试结果:
6.Windows下QtCreaor中配置OpenCV
Qt
安装过程不再赘述,安装时选上MSVC2017-64
(因为我的电脑是64位的且VS
版本为2017)。有了先前编译好的库文件以后,最主要的就是在Qt
工程的.pro
文件中配置库的目录。
opencv_demo.pro
:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
INCLUDEPATH += F:/opencv_4.0.1_build/install/include
CONFIG(debug,debug|release):{
LIBS += -LF:/opencv_4.0.1_build/install/x64/vc15/lib \
-lopencv_aruco401d \
-lopencv_bgsegm401d \
-lopencv_bioinspired401d \
-lopencv_calib3d401d \
-lopencv_ccalib401d \
-lopencv_core401d \
-lopencv_datasets401d \
-lopencv_dnn401d \
-lopencv_dnn_objdetect401d \
-lopencv_dpm401d \
-lopencv_face401d \
-lopencv_features2d401d \
-lopencv_flann401d \
-lopencv_fuzzy401d \
-lopencv_gapi401d \
-lopencv_hdf401d \
-lopencv_hfs401d \
-lopencv_highgui401d \
-lopencv_imgcodecs401d \
-lopencv_imgproc401d \
-lopencv_img_hash401d \
-lopencv_line_descriptor401d \
-lopencv_ml401d \
-lopencv_objdetect401d \
-lopencv_optflow401d \
-lopencv_phase_unwrapping401d \
-lopencv_photo401d \
-lopencv_plot401d \
-lopencv_reg401d \
-lopencv_rgbd401d \
-lopencv_saliency401d \
-lopencv_shape401d \
-lopencv_stereo401d \
-lopencv_stitching401d \
-lopencv_structured_light401d \
-lopencv_superres401d \
-lopencv_surface_matching401d \
-lopencv_text401d \
-lopencv_tracking401d \
-lopencv_video401d \
-lopencv_videoio401d \
-lopencv_videostab401d \
-lopencv_xfeatures2d401d \
-lopencv_ximgproc401d \
-lopencv_xobjdetect401d \
-lopencv_xphoto401d
}else:CONFIG(relsase,debug|release):{
LIBS += -LF:/opencv_4.0.1_build/install/x64/vc15/lib \
-lopencv_aruco401 \
-lopencv_bgsegm401 \
-lopencv_bioinspired401 \
-lopencv_calib3d401 \
-lopencv_ccalib401 \
-lopencv_core401 \
-lopencv_datasets401 \
-lopencv_dnn401 \
-lopencv_dnn_objdetect401 \
-lopencv_dpm401 \
-lopencv_face401 \
-lopencv_features2d401 \
-lopencv_flann401 \
-lopencv_fuzzy401 \
-lopencv_gapi401 \
-lopencv_hdf401 \
-lopencv_hfs401 \
-lopencv_highgui401 \
-lopencv_imgcodecs401 \
-lopencv_imgproc401 \
-lopencv_img_hash401 \
-lopencv_line_descriptor401 \
-lopencv_ml401 \
-lopencv_objdetect401 \
-lopencv_optflow401 \
-lopencv_phase_unwrapping401 \
-lopencv_photo401 \
-lopencv_plot401 \
-lopencv_reg401 \
-lopencv_rgbd401 \
-lopencv_saliency401 \
-lopencv_shape401 \
-lopencv_stereo401 \
-lopencv_stitching401 \
-lopencv_structured_light401 \
-lopencv_superres401 \
-lopencv_surface_matching401 \
-lopencv_text401 \
-lopencv_tracking401 \
-lopencv_video401 \
-lopencv_videoio401 \
-lopencv_videostab401 \
-lopencv_xfeatures2d401 \
-lopencv_ximgproc401 \
-lopencv_xobjdetect401 \
-lopencv_xphoto401
}
SOURCES += main.cpp
测试代码:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv)
{
String file_path = "C://Users//Peco//Desktop//Qt//opencv_demo//test.jpg";
Mat img = imread(file_path);
imshow("test",img);
waitKey(0);
destroyAllWindows();
return 0;
}
结果:
注:
- 1.本部分参考了。
- 2.若提示:
OpenCV Error: Assertion failed (size.width>0 && size.height>0)
则很可能是图片路径问题,建议使用绝对路径。