文章目录
内容
windows10系统下安装opencv4.7.0+VSCode+(C++)环境搭建
写在前面(安装包可去官网下载)
1.VScode最新版
2.Opencv:opencv-4.7.0、opencv_contrib-4.7.0(扩展库,可自选是否安装)
3.MinGW-w64:选择GCCWindows版本
4.c-make工具:最新版,应选择二进制版本
注意:
- (1)以上安装包目录文件中不应包含空格空格和其他非法字符,否则后面会出问题;
- (2)系统已安装或配置Python和Java对应版本,切记安装路径不能包含空格和其他非法字符;
- (3)c-make解压至系统盘,放其他盘,后面cmake build可能会报错。
环境搭建过程
1 vscode安装:
打开解压好的vscode,运行code.exe进行安装配置,完成后,打开vscode配置c/c++扩展库(想中文显示面板,可在此查找中文插件,安装)。详细步骤如下:
MinGW-w64安装
将其安装包解压后再系统环境变量中添加并保存其bin的Path:如E:\MinGW\bin(避免非法字符)。然后再cmd中输入gcc -version,若如下显示则表示配置成功。
Opencv编译
1.解压c-make包,运行cmake包中bin目录的cmake-gui.exe(如C:\cmake-3.26.0-rc3-windows-x86_64\bin)
注
- (1)在"Specify the generator for this project"选择时,应选"MinGW Makefiles",编辑器可根据自己实际情况选择,若无要求,则使用默认编译器;
- (2)opencv扩展安装:在上述step3执行无误后,在Search中输入extra,并选择扩展路径E:\Microsoft-VS-Code\opencv_contrib-4.7.0。点击configure,配置完成后即可点击generate。
- 完成配置后,在自己创建的build文件夹==(用于存放opencv编译结果)==中,按住shift+鼠标右键,进入powershell窗口:
- (1) 输入 MinGW32-make 开始编译,若缺宝库则会很慢(性能较好的笔记本可用 MinGW32-make -j4 ,多CPU同时编译)。
- (2)编译完成,输入:MinGW32-make install 进行安装。
- (3)添加编译后的文件环境变量:E:\Microsoft-VS-Code\opencv-4.7.0_build
vscode配置opencv,并test运行
如图,进行创建vscode代码的存储文件夹、配置json文件夹:
对应json文件配置内容如下所示:
- c_cpp_properties.json 需配置“includePath” (make生成的文件夹对应位置) 和"compilerPath" (gcc编译器所在位置)
{"configurations":[{"name":"Win32","includePath":["${workspaceFolder}/**","E:/Microsoft-VS-Code/opencv-4.7.0_build/install/include","E:/Microsoft-VS-Code/opencv-4.7.0_build/install/include/opencv2"],"defines":["_DEBUG","UNICODE","_UNICODE"],"compilerPath":"E:/MinGW/bin/gcc.exe","cStandard":"c11","cppStandard":"c++17","intelliSenseMode":"clang-x64"}],"version":4}
- launch.json
{"version":"0.2.0","configurations":[{"name":"(gdb) Launch","type":"cppdbg","request":"launch","program":"${fileDirname}/${fileBasenameNoExtension}.exe","args":[],"stopAtEntry":false,"cwd":"${workspaceFolder}","environment":[],"externalConsole":true,"MIMode":"gdb","miDebuggerPath":"E:/MinGW/bin/gdb.exe","setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"preLaunchTask":"Compile"}]}
- Task.json
注意:"command"中g++的配置,如:“E:/MinGW/bin/g++.exe”,以及其他路径的配置。libopencv_core470中的 “470” 表示对应的opencv版本。
{"tasks":[{"label":"Compile","command":"E:/MinGW/bin/g++.exe","args":["-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe","-m64","-Wall","-static-libgcc","-finput-charset=UTF-8","-fexec-charset=GBK","-std=c++17","-I","E:/Microsoft-VS-Code/opencv-4.7.0_build/install/include","-I","E:/Microsoft-VS-Code/opencv-4.7.0_build/install/include/opencv2","-L","E:/Microsoft-VS-Code/opencv-4.7.0_build/install/x64/mingw/lib","-l","libopencv_calib3d470","-l","libopencv_core470","-l","libopencv_dnn470","-l","libopencv_features2d470","-l","libopencv_flann470","-l","libopencv_gapi470","-l","libopencv_highgui470","-l","libopencv_imgcodecs470","-l","libopencv_imgproc470","-l","libopencv_ml470","-l","libopencv_objdetect470","-l","libopencv_photo470","-l","libopencv_stitching470","-l","libopencv_video470","-l","libopencv_videoio470"],"type":"process","problemMatcher":"$gcc","group":"build","presentation":{"echo":true,"reveal":"always","focus":false,"panel":"shared"}},{"type":"cppbuild","label":"C/C++: g++.exe 生成活动文件","command":"E:\\MinGW\\bin\\g++.exe","args":["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options":{"cwd":"${fileDirname}"},"problemMatcher":["$gcc"],"group":{"kind":"build","isDefault":true},"detail":"调试器生成的任务。"}],"version":"2.0.0"}
代码运行测试
- demo
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
int main(){
Mat img=imread("C:/Users/Administrator/Desktop/test.jpg");//Mat resize_img(img);
Size dsize =Size(round(0.3* img.cols),round(0.3* img.rows));//Size型 改变尺寸resize(img, img, dsize,0,0,INTER_LINEAR);//使用双线性插值缩放一下尺寸//resize(img, resize_img, Size(img.cols/7, img.rows/7));cv::imshow("test",img);cv::waitKey();system("pause");return0;}
- 在vscode中按F5运行,结果如下: 如上图所示,则表示已完成了相关的安装配置,可进行下一步开发测试。
- 创作不易,支持原创,感谢大家的支持与理解。若在配置过程中遇到问题,欢迎留言!
版权归原作者 谦益菜鸟 所有, 如有侵权,请联系我们删除。