@[TOC](这里写目录标题)
# 一.文件下载:
## 1.MinGW下载
## 2.Cmake下载
## 3.Opencv下载
# 二.配置环境变量:
# 三.vscode 配置
## 1、launch.json
## 2、c_ cpp_ properties json
## 3、tasks json
# 四.测试
1.MinGW下载(MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net)
单独在一个盘里面新建一个文件夹 名字随意 我的(MinGw)下载完成会在这个文件夹里面 第一个文件是解压之后的文件
2.Cmake下载(https://cmake.org/files/v3.20/)
选择这个版本进行下载 然后像上面一样新建一个文件夹来存放cmake 我的(cmake)第一个是解压后的文件.
3.Opencv下载(https://github.com/huihut/OpenCV-MinGW-Build)
由于opencv的源代码用mingw编译坑挺多的 这里直接下载已经编译好的 省去那些麻烦 下载并且解压后得到这两个文件夹
1.配置环境变量
打开控制面板
1.添加MinGW的环境变量:(点中后确定)
会有下面这个路径 添加完后还要点击空白处!!!
2.添加cmake的环境变量:(选中文件点击确定)
出现这个路径说明成功
3.添加OpenCV环境变量 之前要点击空白处
出现这个路径表示成功 最后确定 环境变量配置完成
5.检查安装配置是否完成 ctrl+R ,输入cmd,调出 cmd命令窗口。
输入: gcc -v
说明MinGw安装配置完成。
输入:cmake -version
说明cmake只能装配置完成
三.vscode 配置
打开vscode 需要安装上相应的扩展
可以新建一个文件夹 专门放OpenCV工程
在vscode里面新建一个main.cpp 文件 要先创建一 个.vscode文件夹, 前面有一个点,这个文件夹存放3个json配置文件,3个json文件要放进.vscode里面!!!
在main.cpp里面加入这段代码
/*********************** 显示指定地址的图片*****************************/
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat image;
image = imread("D:\\OpenCV\\z23.jpg");
if (image.data == nullptr) //nullptr是c++11新出现的空指针常量
{
cout << "图片文件不存在" << endl;
}
else
{
//显示图片
imshow("meinv", image);
waitKey(0);
}
// 输出图片的基本信息
cout << "图像宽为:" << image.cols << "\t高度为:" << image.rows << "\t通道数为:" << image.channels() << endl;
// 遍历每个像素
//之所以用y这个名字表示行 是因为图片的坐标系中行号就是y
for (size_t y = 0; y < image.rows; y++)
{
unsigned char* row_ptr = image.ptr
(y); for (size_t x = 0; x < image.cols; ++x) {
//这是获得像素数据数组的头指针,注意像素数据可能会有多个通道所以才需要用数组存储
unsigned char* data_ptr = &row_ptr[x * image.channels()];
//对当前像素逐个通道输出颜色值
for (int i = 0; i < image.channels(); ++i) {
cout << int(data_ptr[i])<
}
}
}
system("pause");
return 0;
}
按F5运行会生成一个文件夹.vscode 然后配置里面三个文件 1. launch.json 2、c_ cpp_ properties json 3、tasks json
1. launch.json
// {
// // 使用 IntelliSense 了解相关属性。
// // 悬停以查看现有属性的描述。
// // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
// "version": "0.2.0",
// "configurations": []
// }
{
// 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",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGw\\MinGw\\bin\\gdb.exe",//修改这里
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
2、c_ cpp_ properties json
{
"configurations": [
{
"name": "Win",
"includePath": [
"${workspaceFolder}/**",
"D:\\OpenCV\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include",//修改这里
"D:\\OpenCV\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include\\opencv2"//修改这里
// "F:\\Tools\\opencv\\build\\include\\opencv"
],
"defines": [],
"compilerPath": "D:\\MinGw\\MinGw\\bin\\gcc.exe",//修改这里
"cStandard": "c11",
//"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
//"intelliSenseMode": "windows-gcc-x64"
//"intelliSenseMode": "${default}"
}
],
"version": 4
}
3、tasks json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"label": "g++",
"command": "g++",
//"command": "D:\\MinGw\\MinGw\\bin\\g++.exe",//修改这里
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.o",
"-I","D:/OpenCV/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/include",//修改这里
"-I","D:\\OpenCV\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include\\opencv2",//修改这里
"-L","D:/OpenCV/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/x64/mingw/lib",//修改这里
"-L","D:/OpenCV/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/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",
],// 编译命令参数
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
},
}
按 Ctrl+F5 运行 创建的text.cpp文件
,显示一张你选择的图片:
说明vscode配置OpenCV环境已经成功
感谢一下博主们提供的帮助 大家也可参考一下其他博主:
VScode搭建Opencv(C++开发环境)_河旬的博客-CSDN博客_vscode安装opencv
VsCode安装和配置c/c++环境(超完整,小白专用)_黄化的多多的博客-CSDN博客_vscode配置c/c++环境
windows平台使用vscode 配置opencv_呆呆的鑫的博客-CSDN博客_vscode配置opencv环境
windows系统【VSCode搭建OpenCV、C++开发环境】_一颗小树x的博客-CSDN博客
【图像处理】c++使用opencv读取图片_努力的袁的博客-CSDN博客_c++打开图片 opencv
vscode配置opencv_Wddfh_frw的博客-CSDN博客_vscode中的opencv