OpenCV环境配置:OpenCV-4.0.1+OpenCV_contrib-4.0.1+VS2017编译配置记录

1.下载opencv源码和contrib模块源码以及CMake

2.解压源码压缩包并配置cmake

如下图:

cmake

其中where is the source code就是解压后的opencv源码目录,where to build the binaries就是自己创建的目标目录。选定好后点击Configure出现如上图的对话框,然后选择2017 win64,点击Finish

出现如下图后即Configure完成:

config done


Configure完成后在OPENCV_EXTRA_MODULES_PATH中选择contrib目录中的modules,如下图:在这里插入图片描述
再次点击ConfigureConfigure done后点击Generate

最后如下图:

在这里插入图片描述


3.使用VS2017进行编译

使用VS2017打开构建目录中的OpenCV.sln,然后点击重新构建解决方案,完成后如下图所示:
vs
若遇到缺失python37_d.lib可以参考此篇博客:缺少Python27_d.lib的解决方法

CMakeTargets中的INSTALL,然后右键选择仅限于项目–>仅生成INSTALL,如下图:
vs
然后结果如下图:
在这里插入图片描述


可以在下面的目录中看到生成的库文件:
lib

4.配置VS

4.1配置环境变量

右键此电脑->属性->高级系统设置->环境变量,在系统变量path中添加F:\opencv_4.0.1_build\install\x64\vc15\bin,如下图:
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;
}

结果:
在这里插入图片描述


注:

相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页