目录
- 介绍
- 安装VsCode
- 安装mingw
- 安装cmake
- 安装opencv,以及其扩展库 opencv_contrib
- 安装python
- 利用cmake生成opencv的Makefile文件
- cmake命令进行编译,安装
- 配置opencv环境变量
- 配置VSCODE
- 测试DEMO
介绍
参考链接:https://www.cnblogs.com/czlhxm/p/13848278.html
想要使vscode能够支持windows下基于C++及mingw的OpenCV开发环境,需实现以下几个关键步骤:
(1)对于opencv的库导入和语法提示,可直接通过修改.vscode配置文件中c_cpp_properties.json实现;
(2)对于gcc对含有opencv源代码的编译,需修改task.json中编译命令行中加入相关头文件目录、链接库目录以及相关的链接库文件,而这本身opencv并不自带,需要自行通过利用cmake编译opencv源文件生成,而Cmake的编译又需要python的环境;
(3)对于gdb对含opencv相关代码的可执行调试,需要gdb加载支持可执行文件中opencv相关功能的dll,这依然是通过利用cmake编译opencv源文件代码生成的.
安装VsCode
官网下载安装,并安装C++扩展
安装mingw
源码下载地址,下载posix版本的
安装步骤:
(1)下载好的文件进行解压,将该路径添加到环境变量PATH;
# 解压后的路径,不要不要有空格等
D:\mingw64\bin
(2)验证是否安装成功,win+R,打开cmd:
# 输入以下命令,可以看到有版本等信息
gcc -v
g++ -v
安装cmake
用来对opencv源码进行编译
下载地址,选择cmake-3.20.0-windows-x86_64.zip
安装步骤:
(1)解压压缩包,添加环境变量PATH:
# 不要有空格等
D:\cmake-3.20.2\bin
(2)验证是否安装成功:
# cmd中输入命令,可以看到版本信息
cmake -version
安装opencv,以及其扩展库 opencv_contrib
opencv4以后很多功能都放在 opencv_contrib中,所以一定要安装这个库
下载地址,点进去,再点击tags,找到想要的版本,我这里装的是4.5.2,选择下载opencv-4.5.2-vc14_vc15.exe,同样下载opencv_contrib-4.5.2.zip
安装步骤:
(1)创建一个空文件夹,分别解压opencv-4.5.2-vc14_vc15.exe和opencv_contrib-4.5.2.zip到这个空文件夹中
安装python
下载地址,选择的是python3.8
安装步骤:
(1)点击直接安装,勾选配置环境变量(勾选了不一定有效,还得自己配置)
(2)在环境变量PATH中配置python路径
# 注意修改安装路径,这里忘记修改了,安装到了默认位置
C:\Users\chenm\AppData\Local\Programs\Python\Python38
C:\Users\chenm\AppData\Local\Programs\Python\Python38\Scripts
(3)验证是否安装成功,打开python IDE,或cmd,输入python可以看到python版本信息
利用cmake生成opencv的Makefile文件
步骤:
(0)需要下载一些东西,可以在hosts文件中添加下面内容,不一定有用
C:\Windows\System32\drivers\etc\hosts
151.101.72.133 raw.githubusercontent.com;
(1)在D:\cmake-3.20.2\bin目录下,打开cmake-gui.exe
-
在D:/opencv-4.5.2/opencv/build/x64/下创建一个空文件夹mingw,用于存放编译后的文件
-
source code那选择:D:/opencv-4.5.2/opencv/sources
-
build the binaries那选择:D:/opencv-4.5.2/opencv/build/x64/mingw
-
点击Configure,跳出的对话框中选择MinGW Makefiles(一定要是MinGW Makefiles),并选择第二项Specify native compilers
-
点击next,再弹出德对话框中选择Mingw的编译器
-
点击finish,就会开始编译
后面的就很重要了,finish结束后,应该会有很多文件没有成功下载的 -
去D:\opencv-4.5.2\opencv\build\x64\mingw目录下,打开CMakeDownloadLog.txt文件,上面有说XXX目录缺少XXX文件,就根据上面的下载地址,自己用迅雷等下载,并按要求直接复制到指定文件夹中
#check_md5 “D:/opencv/sources/.cache/ffmpeg/638065d5a0dab8a828879942375dcac4-opencv_videoio_ffmpeg.dll”
#mismatch_md5 “D:/opencv/sources/.cache/ffmpeg/638065d5a0dab8a828879942375dcac4-opencv_videoio_ffmpeg.dll” “d41d8cd98f00b204e9800998ecf8427e”
#do_copy “opencv_videoio_ffmpeg.dll” “638065d5a0dab8a828879942375dcac4” “https://raw.githubusercontent.com/opencv/opencv_3rdparty/629590c3ba09fb0c8eaa9ab858ff13d3a84ca1aa/ffmpeg/opencv_videoio_ffmpeg.dll” “D:/opencv/build/x64/mingw/3rdparty/ffmpeg”
…复制的好像不完整
大致意思是:D:/opencv/build/x64/mingw/3rdparty/ffmpeg文件夹中缺少opencv_videoio_ffmpeg.dll,要先从"https://raw.githubusercontent.com/opencv/opencv_3rdparty/629590c3ba09fb0c8eaa9ab858ff13d3a84ca1aa/ffmpeg/opencv_videoio_ffmpeg.dll"下载这个文件,这个文件的hash编码是"638065d5a0dab8a828879942375dcac4",然后在D:/opencv/sources/.cache/ffmpeg/中要有待编码的文件,然后再从.cache中复制到目的文件夹中,其实.cache这一步可以省略,直接下载文件,不用hash码啥的,直接复制到D:/opencv/build/x64/mingw/3rdparty/ffmpeg中就行
-
缺的文件全部弄好后,在修改下配置文件,在Search这栏搜索opencv_extra 可以看到有这一项:OPENCV_EXTRA_MODULES_PATH,然后Value栏选怎opencv的扩展文件D:\opencv-4.5.2\opencv_contrib-4.5.2\modules
-
取消勾选BUILD_opencv_face,因为这个库编译总不成功,而且也是一个不用的库
-
取消全部test相关的,没必要,可能影响编译
-
取消BUILD_opencv_world,这个好像是说将编译好的库,再压缩成什么,没必要,而且勾选的话也是一直编译不成功的
-
看看有没有ENABLE_CXX11,没有的话,点击右上方的添加,注意value勾选
-
以上配置的差不多了,点击Configure,再看看CMakeDownloadLog.txt,能看到以下信息,并且框中没有红字后(有时候需要重复几次Configure),再点击旁边的Generate,最后有Configure done generate done就成功了
cmake命令进行编译,安装
在cmd窗口输入以下命令,输入第一个命令可能不成功
minGW32-make 或者 minGW32-make -j 4 (-j 4 表示4进程,根据自己机器情况设置,还可以8,16)
minGW32-make install
minGW32-make后可能出现下面BUG,参考https://www.codeleading.com/article/51025552597/
解决步骤:
(1)定位到你的opencv_contrib\modules\wechat_qrcode\src\zxing这一目录下,打开zxing.hpp文件
(2)将第30行的#if defined(ANDROID_API)改为#if defined(ANDROID_API) || defined(_MSC_VER) || defined(MINGW32) || defined(MINGW64)
(3)删掉第54到第62行的内容
(4)保存文件,重新Configure,Generate,再编译
配置opencv环境变量
配置VSCODE
新建一个.vscode文件夹,新建c_cpp_properties.json,lunch.json,task.json
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/opencv-4.5.2/opencv/build/x64/mingw/install/include",
"D:/opencv-4.5.2/opencv/build/x64/mingw/install/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "g++",//调试前执行的任务,就是之前配置的tasks.json中的label字段,"Compile" "g++.exe build active file"
"type": "cppdbg",//配置类型,只能为cppdbg
"request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\\Debugger\\${fileBasenameNoExtension}.exe",//调试程序的路径名称,这个Debugger需要自己创建,为了方便放置生成的exe文件
"args": [],//调试传递参数
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,此为工作区文件夹;改成${fileDirname}可变为文件所在目录
"environment": [],
"externalConsole": true,//true显示外置的控制台窗口,false显示内置终端
"internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe",// 调试器路径,Windows下后缀不能省略,Linux下则不要
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
tasj.json
{
"version": "2.0.0",
"tasks": [
{
// "type": "cppbuild",
"label": "g++", // 任务名称,与launch.json的preLaunchTask相对应,或者选择Compile或者"C/C++: g++.exe 生成活动文件"
"command": "D:/mingw64/bin/g++.exe", // 要使用的编译器,C++用g++
"args": [
"-fdiagnostics-color=always",
"-g", // 生成和调试有关的信息
"${file}", //当前文件名
"-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
"${fileDirname}\\Debugger\\${fileBasenameNoExtension}.exe",
"-m64", // 不知为何有时会生成16位应用而无法运行,加上此条可强制生成64位的
"-Wall", // 开启额外警告
"-static-libgcc", // 静态链接libgcc,一般都会加上
"-finput-charset=UTF-8",
"-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这条会导致Win下输出中文乱码;繁体系统改成BIG5
"-std=c++17", // 要用的语言标准,根据自己的需要修改。c++可用c++14
// 扩展参数
// -I 头文件目录
// -L 库文件目录
// -l 库文件
"-I", "D:/opencv-4.5.2/opencv/build/x64/mingw/install/include",
"-I", "D:/opencv-4.5.2/opencv/build/x64/mingw/install/include/opencv2",
"-L", "D:/opencv-4.5.2/opencv/build/x64/mingw/install/x64/mingw/bin",
// "-l", "libopencv_calib3d452",
// "-l", "libopencv_core452",
// "-l", "libopencv_dnn452",
// "-l", "libopencv_features2d452",
// "-l", "libopencv_flann452",
// "-l", "libopencv_gapi452",
// "-l", "libopencv_highgui452",
// "-l", "libopencv_imgcodecs452",
// "-l", "libopencv_imgproc452",
// "-l", "libopencv_ml452",
// "-l", "libopencv_objdetect452",
// "-l", "libopencv_photo452",
// "-l", "libopencv_stitching452",
// "-l", "libopencv_video452",
// "-l", "libopencv_videoio452",
// "-l", "libopencv_ximgproc452" ,
"-l", "libopencv_aruco452",
"-l", "libopencv_bgsegm452",
"-l", "libopencv_bioinspired452",
"-l", "libopencv_calib3d452",
"-l", "libopencv_ccalib452",
"-l", "libopencv_core452",
"-l", "libopencv_datasets452",
"-l", "libopencv_dnn452",
"-l", "libopencv_dnn_objdetect452",
"-l", "libopencv_dnn_superres452",
"-l", "libopencv_dpm452",
"-l", "libopencv_features2d452",
"-l", "libopencv_flann452",
"-l", "libopencv_fuzzy452",
"-l", "libopencv_gapi452",
"-l", "libopencv_hfs452",
"-l", "libopencv_highgui452",
"-l", "libopencv_imgcodecs452",
"-l", "libopencv_imgproc452",
"-l", "libopencv_img_hash452",
"-l", "libopencv_intensity_transform452",
"-l", "libopencv_line_descriptor452",
"-l", "libopencv_mcc452",
"-l", "libopencv_ml452",
"-l", "libopencv_objdetect452",
"-l", "libopencv_optflow452",
"-l", "libopencv_phase_unwrapping452",
"-l", "libopencv_photo452",
"-l", "libopencv_plot452",
"-l", "libopencv_quality452",
"-l", "libopencv_rapid452",
"-l", "libopencv_reg452",
"-l", "libopencv_rgbd452",
"-l", "libopencv_saliency452",
"-l", "libopencv_shape452",
"-l", "libopencv_stereo452",
"-l", "libopencv_stitching452",
"-l", "libopencv_structured_light452",
"-l", "libopencv_superres452",
"-l", "libopencv_surface_matching452",
"-l", "libopencv_text452",
"-l", "libopencv_tracking452",
"-l", "libopencv_video452",
"-l", "libopencv_videoio452",
"-l", "libopencv_videostab452",
"-l", "libopencv_wechat_qrcode452",
"-l", "libopencv_xfeatures2d452",
"-l", "libopencv_ximgproc452",
"-l", "libopencv_xobjdetect452",
"-l", "libopencv_xphoto452",
"-l", "opencv_videoio_ffmpeg452_64"
// 这行是博主自己添加的扩展动态链接库,没安装扩展的这行可以删除。
],// 编译的命令,其实相当于VSC帮你在终端中输了这些东西
"options": {
"cwd": "D:/mingw64/bin"
},
"problemMatcher": [ // 捕捉编译时终端里的报错信息到问题面板中,修改代码后需要重新编译才会再次触发
"$gcc"
],
// "group": "build",
// "detail": "编译器: D:/mingw64/bin/g++.exe",
"type": "shell", // process是把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
"group": {
"kind": "build",
"isDefault": true // 不为true时ctrl shift B就要手动选择了
},
"presentation": {
"echo": true,
"reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档
"focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
}
}
]
}
测试DEMO
点击F5运行程序,可以看到有图片显示就成功了
#include <opencv2/opencv.hpp>
// using namespace 指令,这样在使用命名空间时就可以不用在前面加上命名空间的名称
using namespace cv;
int main(void)
{
// C++中Mat表示图像的内存对象,所有的图像对象皆为Mat
// 通道顺序:B,G,R
Mat srcImage = imread("F:\\VOCtrainval_11-May-2012\\VOCdevkit\\VOC2012\\JPEGImages\\2007_000027.jpg"); // 载入指定路径的图像
// WINDOW_AUTOSIZE自适应调整窗口大小,WINDOW_FREERATIO自由屏幕窗口,可以手动调整窗口大小
namedWindow("input", WINDOW_FREERATIO);
std::cout << "Mat格式:" <<srcImage.depth()<< std::endl;
// imshow只支持0~255像素格式图像的显示或浮点数数据的显示
imshow("input", srcImage); // 显示该图像
waitKey(0); // 表示阻塞等待用户键盘输入,用户按键盘任意键就会停止阻塞,继续执行直到程序正常退
destroyAllWindows();
return 0;
}