前提: 1.windows系统 2.软件vs code 3.opencv version: 4.5.2 4.gcc8.1.0(minGW中的) 5.cmake version: 3.17.0-rc3 步骤: 1.下载minGW,并将minGW的文件路径加入系统环境变量Path
下载minGW网址: https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/ 2.下载opencv官网源文件,并解压 vs code中C++使用opencv,需要自己编译源文件 3.下载最新版cmake,并添加环境变量 4.在cmake-3.17.0-rc3-win64-x64\bin中打开cmake-gui.exe,并配置 图中第一行将第2步骤中解压的源文件路径填入 (应和D:/studying/language/C++/libraries/opencv-4.5.2相似,且opencv-4.5.2文件夹中没有除源文件的其他无关文件,只要解压后,别往里面加东西就行) 图中第二行填入你想要将编译后的文件放入的文件夹路径 5.点击configure,选择MinGW Makefiles生成器,使用默认本地编译器(use the default native compilers),最后点击Finish 6.点击Generate,等待.若出错,则点击configure,接着等待.configure完成后(没问题后,进行后面的操作.若有问题找出原因),点击Generate. (确保Generate后任何警告和错误) 7.找到第4步骤中,填入的文件夹路径,使用shift+右键在Power Shell中打开文件夹.接着在power shell中运行minGW32-make(不需要任何参数).等待(若在power shell中进度条太长时间没有动,点击鼠标右键试试). 完成标志: 进度条显示为100%,且等待输入下一个命令(不要关闭窗口) 8.运行minGW32-make install,生成install文件夹 9.配置vscode c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}",
"D:\\studying\\language\\C++\\libraries\\opencv452\\install\\include",
"D:\\studying\\language\\C++\\libraries\\opencv452\\install\\include\\opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/studying/language/mingw64/bin/g++.exe",
"intelliSenseMode": "windows-gcc-x64",
"cppStandard": "c++17"
}
],
"version": 4
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "D:/studying/language/mingw64/bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\studying\\language\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:/studying/language/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++11",
"-I","D:\\studying\\language\\C++\\libraries\\opencv452\\install\\include",
"-I","D:\\studying\\language\\C++\\libraries\\opencv452\\install\\include\\opencv2",
"-L","D:\\studying\\language\\C++\\libraries\\opencv452\\lib",
"-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"
],
"options": {
"cwd": "D:\\studying\\language\\C++\\pro"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
测试源码:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
int blurAmount = 15;
static void onChange(int pos,void* userInput);
static void onMouse(int event,int x,int y ,int,void *userInput);
int main()
{
Mat airplane1 = imread("D:\\studying\\language\\C++\\pro\\opencv_test\\wallhaven-pkk69p.jpg");
namedWindow("origin_img");
namedWindow("airplane1");
imshow("origin_img",airplane1);
createTrackbar("airplane1","airplane1",&blurAmount,30,onChange,&airplane1);
setMouseCallback("airplane1",onMouse,&airplane1);
onChange(blurAmount,&airplane1);
waitKey(0);
destroyWindow("airplane1");
return 0;
}
static void onChange(int pos,void*userData)
{
if (pos <=0)
return;
Mat imgBlur;
Mat *img = (Mat*)userData;
cv::blur(*img,imgBlur,Size(pos,pos));
imshow("airplane1",imgBlur);
}
static void onMouse(int event, int x, int y,int, void *userInput)
{
if(event != EVENT_LBUTTONDOWN)
return;
Mat *img = (Mat*)userInput;
onChange(blurAmount, img);
}
参考: 1.win10下VSCode配置opencv4.4.0(超详细教程,亲测有效) 2.VScode中配置C++的Opencv库
|