1、下载opencv
2、下载CMake
3、下载MinGW
放到一个文件夹中
并解压另外两个文件
4、cmake编译opencv
新建文件夹mingw-build
双击cmake-gui
程序会开始自动生成Makefiles等文件配置,需要耐心等待一段时间。
简单总结下:finish->configuring done->configure->generate
5、安装
打开cmd,cd至刚刚的构建目录下C:\Users\wuxulong\cpp_env_2\opencv\build\mingw-build,
输入编译指令minGW32-make -j8
,完成后再输入minGW32-make install
也要等一段时间
6、配置环境变量
7、配置文件
c_cpp_properties.json
{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**","C:\\Users\\wuxulong\\cpp_env_2\\opencv\\build\\mingw-build\\install\\include",//修改这里"C:\\Users\\wuxulong\\cpp_env_2\\opencv\\build\\mingw-build\\install\\include\\opencv2"//修改这里// "C:\\Users\\wuxulong\\cpp_env\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include\\opencv2\\core\\core.hpp"// "F:\\Tools\\opencv\\build\\include\\opencv" ],"defines": ["_DEBUG","UNICODE","_UNICODE"],"windowsSdkVersion": "10.0.18362.0","compilerPath": "C:\\Users\\wuxulong\\cpp_env_2\\mingw64\\bin\\g++.exe",//修改这里"cStandard": "c11",//"cStandard": "c17","cppStandard": "c++17","intelliSenseMode": "gcc-x64"//"intelliSenseMode": "windows-gcc-x64"//"intelliSenseMode": "${default}"}],"version": 4
}
lanuch.json
{// 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字段"type": "cppdbg",//配置类型,只能为cppdbg"request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称"args": [],//调试传递参数"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,//true显示外置的控制台窗口,false显示内置终端"MIMode": "gdb","miDebuggerPath": "C:\\Users\\wuxulong\\cpp_env_2\\mingw64\\bin\\gdb.exe","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]}
tasks.json
{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "g++.exe build active file","command": "C:\\Users\\wuxulong\\cpp_env_2\\mingw64\\bin\\g++.exe","args": ["-fdiagnostics-color=always","-g",// "-std=c++11","${file}",//"E:\\Git-resp\\C++\\yolov8_CPP_Inference_OpenCV_ONNX\\inference.cpp","-o","${fileDirname}\\${fileBasenameNoExtension}.exe",//"-I","E:\\Git-resp\\C++\\yolov8_CPP_Inference_OpenCV_ONNX","-I","C:\\Users\\wuxulong\\cpp_env_2\\opencv\\build\\mingw-build\\install\\include","-I","C:\\Users\\wuxulong\\cpp_env_2\\opencv\\build\\mingw-build\\install\\include\\opencv2","-L","C:\\Users\\wuxulong\\cpp_env_2\\opencv\\build\\mingw-build\\install\\x64\\mingw\\bin\\lib*"],"options": {"cwd": "C:\\Users\\wuxulong\\cpp_env_2\\mingw64\\bin"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true//表示快捷键Ctrl+Shift+B可以运行该任务},// "group": "build","detail": "编译器: C:\\Users\\wuxulong\\cpp_env_2\\mingw64\\bin\\g++.exe"}]
}
main.cpp
/*********************** 显示指定地址的图片*****************************/#include<iostream>
#include<opencv2/opencv.hpp>
#include <unistd.h>
// #include<opencv2/core/core.hpp>
// #include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;int main(int argc, char** argv) { Mat image;image = imread("data/image/2.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;sleep(10);// 遍历每个像素//之所以用y这个名字表示行 是因为图片的坐标系中行号就是yfor (int y = 0; y < image.rows; y++){unsigned char* row_ptr = image.ptr<unsigned char>(y);for (int 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])<<endl;}}}// system("pause");return 0;
}
结果
参考:2023年最全 Windows + VSCode 配置 OpenCV C++ 一站式开发调试环境教程