D盘新建一个opencv目录,然后去下载opencv最新版以及扩展包。下载链接,下载“opencv-4.6.0.tar.gz”与“opencv_contrib-4.6.0.zip”即可。
下载并安装cmake
下载并安装Mingw64,链接,解压到某个路径,并将对应的bin路径添加到path环境(我下载的是GCC 11.2.0 + MinGW-w64 10.0.0 (UCRT) - release 1)。
OPENCV_EXTRA_MODULES_PATH
,value填为opencv_contrib解压后的路径,然后默认添加“modules”即可,例如D:/opencv/opencv_contrib-4.6.0/modules
mingw32-make.exe -j10
mingw32-make install
cmake `
-D CMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE `
-D CUDAToolkit_ROOT="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6" `
-D WITH_CUDA=ON `
-D OPENCV_EXTRA_MODULES_PATH=D:/opencv/opencv_contrib-4.6.0/modules `
-D ENABLE_FAST_MATH=ON `
-D CUDA_FAST_MATH=ON `
-D WITH_CUBLAS=ON `
-D OPENCV_DNN_CUDA=ON `
-D CMAKE_BUILD_TYPE=Release `
..
cmake --build . -- -j10
//读取图片并显示
#include
#include
#include
using namespace cv;
int main()
{
Mat img = imread("C:/Users/18826/Pictures/头像.png");
namedWindow("测试opencv");
imshow("测试opencv", img);
waitKey(10000);
}
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\MinGW-10\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
{
"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/build/x64/mingw_10/install/include",
"-L",
"D:/opencv/build/x64/mingw_10/install/x64/mingw/lib",
"-L",
"D:/opencv/build/x64/mingw_10/install/x64/mingw/bin",
"-l",
"opencv_core460",
"-l",
"opencv_imgproc460",
"-l",
"opencv_imgcodecs460",
"-l",
"opencv_video460",
"-l",
"opencv_ml460",
"-l",
"opencv_highgui460",
"-l",
"opencv_objdetect460",
"-l",
"opencv_flann460",
"-l",
"opencv_imgcodecs460",
"-l",
"opencv_photo460",
"-l",
"opencv_videoio460"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
// {
// "description": "为 gdb 启用整齐打印",
// "text": "-enable-pretty-printing",
// "ignoreFailures": true
// }
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
INCLUDE
与LIB
,INCLUDE对应的是C/C++头文件信息,需要填上cl.exe所需头文件,不然标准库都会找不到的。我的INCLUDE变量与LIB变量内容分别如下:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include;
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\lib\x64;
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64;
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe 生成活动文件",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}