1.安装MinGW
并配置环境变量path
在终端输入gcc -v验证
2.安装cmake
3.官方下载opencv源码source
在cmake中编译,新建D:\opencv目录
先执行configure再执行generate
opencv目录下终端输入mingw32-make -j 8
完成后在输入
mingw32-make install
至此编译完成。
4.配置json文件
c_cpp_properties.json配置修改注释掉的两行
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:\\opencv\\install\\include",
"D:\\opencv\\install\\include\\opencv",
"D:\\opencv\\install\\include\\opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
//"compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe",
"compilerPath": "D:/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
//"intelliSenseMode": "windows-msvc-x64"
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
tasks.json配置在args下添加文章来源:https://www.toymoban.com/news/detail-520021.html
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\opencv\\install\\include",
"-I",
"D:\\opencv\\install\\include\\opencv",
"-I",
"D:\\opencv\\install\\include\\opencv2",
"-L",
"D:\\opencv\\bin",
"-l",
"libopencv_calib3d3416",
"-l",
"libopencv_core3416",
"-l",
"libopencv_dnn3416",
"-l",
"libopencv_features2d3416",
"-l",
"libopencv_flann3416",
"-l",
"libopencv_highgui3416",
"-l",
"libopencv_imgcodecs3416",
"-l",
"libopencv_imgproc3416",
"-l",
"libopencv_ml3416",
"-l",
"libopencv_objdetect3416",
"-l",
"libopencv_photo3416",
"-l",
"libopencv_shape3416",
"-l",
"libopencv_stitching3416",
"-l",
"libopencv_superres3416",
"-l",
"libopencv_video3416",
"-l",
"libopencv_videoio3416",
"-l",
"libopencv_videostab3416",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
#include <iostream>
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char** argv){
Mat image;
image = cv::imread("1.jpg");
imshow("image",image);
waitKey(0);
//if(image.data== nullptr)//nullptr是c++11新出现的空指针常量
//{
// cout<<"图片文件不存在"<<endl;
// return 0;
//}
//else
// cout<<image<<endl;//你会发现图片就是一个矩阵
return 0;
}
如果其间遇到问题
VScode:ERROR: Unable to start debugging. Unexpected GDB output from command “-exec-run“
原因:windows 环境变量中的设置顺序问题,导致 libstdc++ -6.dll 文件冲突了
简单来说就是 mingw64\bin 环境变量位置在后面, 导致 gdb 调试时选择的 libstdc++ -6.dll 不是 mingw64\bin 中的,而是前面的环境变量中的该文件
解决方法
方法一、将 mingw64\bin 中的 libstdc++ -6.dll 拷贝到项目目录
或者参考
测试代码
文章来源地址https://www.toymoban.com/news/detail-520021.html
到了这里,关于vscode配置opencv环境【完整版】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!