简介:VSCode搭建opencv,需要下载cmake、opencv、mingw这3个安装包,mingw下载可参考文章
一. 安装包下载
1.Cmake下载
下载地址:
Index of /files/v3.27
https://cmake.org/files/v3.27/
下载cmake-3.27.2-windows-x86_64.zip这个版本

继续添加环境变量
这里,我的路径是:D:\cmake\cmake-3.27.2-windows-x86_64\cmake-3.27.2-windows-x86_64\bin

2.Opencv下载
下载地址:
Releases - OpenCV
https://opencv.org/releases/
下载window版本
接下来进行编译:vscode没办法直接使用opencv,我们需要把opencv的源文件进行编译,然后才能使用。这也是我们要用到cmake的原因。下面我们要使用cmake编译opencv的源文件。
二. cmake-gui
进入cmake的bin目录下,双击cmake_gui.exe


Where is the source code: 这里使用opencv目录下的source目录
Where to build the binaries: 这里是编译后的文件的放置目录
(这里的mingw是自己创建的空文件夹)
然后点configure

然后选择自己的gcc和g++路径,路径就是mingw/bin下的gcc.exe和g++.exe文件
继续finish

执行过程中消息框会出现一堆红色信息,最后显示Configure done,这个是正常的。

在执行完后,出现很多选项,勾选BUILD_opencv_world,WITH_OPENGL和BUILD_EXAMPLES,不勾选WITH_IPP、WITH_MSMF和ENABLE_PRECOMPILED_HEADERS,CPU_DISPATCH选空,其他的不要动。
然后点击Generate,
最后显示Generate done,成功。
三.编译
进入刚刚新建的文件夹:

打开终端,输入:minGW32-make -j 8(4和8都可以,这个是指线程数)

在运行的过程中,可能会报错,可能的错误如下:

解决方案:
进入D:\OpenCv\opencv\sources\samples\cpp路径下

打开CmakeLists,添加opencv_gapi

继续输入:minGW32-make -j 8进行编译;编译成功界面如图:

接下来,生成一个install目录,输入:minGW32-make install

成功界面如图:

生成的install目录在刚刚新建的文件夹下:

最后,继续添加环境变量:

到此,配置完成。
四.测试
1.代码
在vscode里进行测试:这里是调用摄像头
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "Error: Could not open camera" << endl;
return -1;
}
Mat img;
namedWindow("img", WINDOW_NORMAL);
while (1)
{
cap >> img;
if (img.empty())
break;
imshow("img", img);
char key = waitKey(20);
if (key == 27 || key == 'q') // 按 ESC 或 'q' 退出
break;
}
cap.release();
destroyAllWindows();
return 0;
}
2. 配置3个json文件
2.1 task.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:/MinGw/x86_64-8.5.0-release-posix-seh-rt_v10-rev0/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"D:/OpenCv/opencv/build/x64/mingw/bin/libopencv_world455.dll",
"-I",
"D:/OpenCv/opencv/build/x64/mingw/install/include",
"-I",
"D:/OpenCv/opencv/build/x64/mingw/install/include/opencv2"
],
"options": {
"cwd": "D:/MinGw/x86_64-8.5.0-release-posix-seh-rt_v10-rev0/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: D:/MinGw/x86_64-8.5.0-release-posix-seh-rt_v10-rev0/mingw64/bin/g++.exe"
}
]
}
2.2 c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/OpenCv/opencv/build/x64/mingw/install/include",
"D:/OpenCv/opencv/build/x64/mingw/install/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/MinGw/x86_64-8.5.0-release-posix-seh-rt_v10-rev0/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
2.3 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",//需要调试的可执行文件
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",//gdb的路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
最后,运行,如果能调用摄像头就说明大功告成 !
1436


&spm=1001.2101.3001.11974&articleId=145964746&d=1&t=3&u=0fcc38c44e1a4429841c85734df6d1b3)

被折叠的 条评论
为什么被折叠?



