1.去gitee上下载,opencv 的release 版本
https://gitee.com/mirrors_huihut/OpenCV-MinGW-Build
2 解压
3.将动态链接库加入到环境变量
新建 %OPENCV_LIB_HOME%
值为 D:\dev\C++Lib\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin
将 %OPENCV_LIB_HOME% 加入到Path 中去
g++ 参数简介:
-g: debug 信息
-I: 告诉编译器,头文件里的 #include<…> 去哪儿找
-L: 告诉编译器,添加一个要动态链接的目录
-l: 指定具体的动态链接库的名称
VS Code 项目配置
c_cpp_properties.json
参数说明:
includePath 头文件位置
/** 表示递归查找子目录
defines : 宏定义
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:\\dev\\C++Lib\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\BasicSoftware\\mingw64\\bin\\gcc.exe",
"cStandard": "c17",
"cppStandard": "gnu++14",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "g++ build",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\dev\\C++Lib\\eigen-3.4.0",
"-I",
"D:\\dev\\C++Lib\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include",
"-L",
"D:\\dev\\C++Lib\\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",
"-l",
"opencv_videoio_ffmpeg452_64"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "build task"
}
],
"version": "2.0.0"
}
launch.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": "(Windows) 启动",// 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppvsdbg",// 配置类型,这里只能为 cppdbg
"request": "launch",//请求配置类型,可以为 launch(启动)或 attach(附加)
"program": "${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}
main.cpp
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
int main()
{
Mat img = imread("abc.png");
imshow("image", img);
waitKey();
return 0;
}
运行效果