1.前言
由于业务需要本人通过vscode快速迭代配置了一版OpenCV的开发环境,因为要快所以直接用大佬们构建好的openCV就行。本人这里是64位的Window11下配置的。
2.前置工具
vscode IDE工具
3.安装VScode插件
- C/C++
- C/C++ Extension Pack
- C/C++ Themes
- CMake
- CMake Tools
4.配置需要的OpenCV,Mingw,CMake等三个环境变量配置
4.1选择OpenCV-4.5.2-x64 | zip | tar.gz版本
github地址:GitHub - huihut/OpenCV-MinGW-Build: ???? MinGW 32bit and 64bit version of OpenCV compiled on Windows. Including OpenCV
4.2 安装MinGW-x86_64-8.1.0-posix-seh-rt_v6-rev0
官方下载地址:MinGW-x86_64-8.1.0-posix-seh-rt_v6-rev0
4.3.安装配置CMake-3.18.4
github地址:Releases · Kitware/CMake · GitHub
4.2 将压缩包统一解压到D:/Operating-Environment/目录下,其他环境配置同理
5.VScode中运行程序
5.1 示例代码main.cpp和图片:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
string imagePath = "./test.png"; // 确保路径正确
cout << "Loading image from: " << imagePath << endl;
Mat image = imread(imagePath);
if (image.empty()) {
cerr << "Error: Could not open or find the image at " << imagePath << endl;
return -1;
}
imshow("Display Image", image);
waitKey(0);
return 0;
}
5.2 配置c_cpp_properties.json
5.2.1按F1选中C/C++配置项
5.2.1修改C/C++配置
5.2.2 C/C++配置代码json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:\\Operating-Environment\\mingw64\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/Operating-Environment/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
5.3 配置tasks.json
5.3.1按F1选中Tasks:Configure Default Build Task
5.3.2 将创建的模板替换成下面那个,环境路径要对应自己的
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "debug",
"command": "D:/Operating-Environment/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-I", "D:/Operating-Environment/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/include", // 包含路径 头文件 编译的时候到这个路径下找头文件 编译审生成可重定位文件
"-L", "D:/Operating-Environment/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/x64/mingw/bin", // 库路径 动态链接库地址,可执行文件运行起来需要这些库提供的机器码
// "-l","libopencv_world4100", // 链接需要使用这些dll作为可重定位文件
"-l","libopencv_calib3d452",
"-l","libopencv_photo452",
"-l","libopencv_core452",
"-l","libopencv_stitching452",
"-l","libopencv_dnn452",
"-l","libopencv_video452",
"-l","libopencv_features2d452",
"-l","libopencv_videoio452",
"-l","libopencv_flann452",
"-l","libopencv_gapi452",
"-l","libopencv_highgui452",
"-l","libopencv_imgcodecs452",
"-l","libopencv_imgproc452",
"-l","libopencv_ml452",
"-l","opencv_videoio_ffmpeg452_64",
"-l","libopencv_objdetect452",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:/Operating-Environment/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "test",
"isDefault": true
},
"detail": "compiler: D:/Operating-Environment/mingw64/bin/g++.exe"
}
]
}
5.4 配置launch.json
5.4.1 选择run下面的 start Debugging创建launch.json模板
5.4.1 在默认生成的模板替换成我的
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
// "preLaunchTask": "g++.exe build active file",//调试前执行的任务,就是之前配置的tasks.json中的label字段
"preLaunchTask": "debug",
"type": "cppdbg",//配置类型,只能为cppdbg
// "label": "C/C++: g++.exe build active file",
"request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称
"args": [],//调试传递参数
// "args": ["/F","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],//调试传递参数
// "stopAtEntry": false,
"stopAtEntry": false,
// "cwd": "${workspaceFolder}",
"cwd": "${fileDirname}",
"environment": [],
// "externalConsole": true,//true显示外置的控制台窗口,false显示内置终端
"externalConsole": false,//true显示外置的控制台窗口,false显示内置终端
"MIMode": "gdb",
"miDebuggerPath": "D:/Operating-Environment/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
// "preLaunchTask": "g++.exe build active file",
// "internalConsoleOptions": "neverOpen"
// "preLaunchTask": "build", // Ensure build task runs before launch
"internalConsoleOptions": "neverOpen"
}
]
}
5.4.1 点击运行就行
5.5 运行结果(真好看)
6.结语
自此结束了,累死我了,用了一天的时间进行配置。原图就送你们了。