OpenCV由于其速度和直观的API,在图像处理领域是一个强大的工具。然而,配置 OpenCV 是一项艰巨的工作,尤其是在 Windows 上。
为了方便您在 VS Code 上配置 OpenCV,我决定写这篇文章来教您。
本教程假设您使用以下配置:
OS: Windows 10 64-bit
Compiler: Mingw-w64 (Specifically, x86_64-8.1.0-posix-seh-rt_v6-rev0)
Compiler binaries' path: C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin
OpenCV version: 4.1.1
OpenCV binaries' path: C:\\opencv\\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\\x64\\mingw\\bin
\\
分隔符是特意添加的。
根据 VS Code 文档的建议,可以按照以下步骤安装:
Install
去安装它。在这部分我选择了 Mingw-w64。请注意,安装时应选择以下设置:
选择这些设置的原因将在下一部分中提到。
我假设您将所有编译器内容安装在 C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0
从 OpenCV-MinGW-Build 下载预构建的 OpenCV 包。因为我们选择的是 4.1.1 版本,所以这个版本有一些规范:
我们可以看到这个包是用 MinGW-x86_64-8.1.0-posix-seh-rt_v6-rev0
编译的。因此,您应该将 MinGW 的配置设置与包使用的相同。
Path
变量为了让其他程序在不输入绝对路径的情况下也能找到编译器和 OpenCV 包的名称,您可以使用编译器和 OpenCV 包的二进制文件来设置您帐户的 Path
变量。
要编辑 Path
变量,您可以转到控制面板 > 系统 > 高级系统设置(Control Panel > System > Advanced system settings)
。然后切换到系统属性
窗口中的高级(Advanced)
选项卡,然后单击环境变量(Environment Variables)
按钮。最后,编辑您帐户的 Path
变量(添加编译器和 OpenCV 二进制文件路径)。
我假设你在 C:\opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64
中安装了所有 OpenCV 的东西
首先,创建一个名为 opencvexample
的新项目。然后复制并粘贴我从这个 repo 创建的 task.json
、launch.json
和 c_cpp_properties.json
。
我还对每个 json 文件和我应用的一些更改进行了一些简要说明。
command
和args
进行了一些更改:"command": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
...
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I","C:\\opencv\\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\\include",
"-L","C:\\opencv\\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\\x64\\mingw\\bin",
"-llibopencv_calib3d411",
"-llibopencv_core411",
"-llibopencv_dnn411",
"-llibopencv_features2d411",
"-llibopencv_flann411",
"-llibopencv_highgui411",
"-llibopencv_imgcodecs411",
"-llibopencv_imgproc411",
"-llibopencv_ml411",
"-llibopencv_objdetect411",
"-llibopencv_photo411",
"-llibopencv_stitching411",
"-llibopencv_video411",
"-llibopencv_videoio411"
],
...
command
将调用编译器。在这里,我将路径更改为我想要的 MinGW 编译器。
args
将作为编译器标志传递给编译命令。您可以看到该程序将与 OpenCV 库链接。
作为一个大拇指规则,只包括你将要使用的库,尽管我包括了 OpenCV 的所有库。
launch.json
文件将启动 GDB 调试器,我更改了如下所示的一些行:...
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
...
"miDebuggerPath": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
...
program
是该程序的可执行文件。
miDebuggerPath
找到调试器的可执行文件,然后我更改为我想使用的 GDB 调试器。
c_cpp_properties.json
让您可以更好地控制 C/C++ 扩展:...
"includePath": [
...
"C:\\opencv\\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\\include"
],
...
"compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
...
includePath
搜索将包含您想要的库的路径,我添加了 OpenCV 的路径。
compilerPath
找到编译器的路径,所以我设置为我要使用的编译器。
创建一个名为main.cpp
文件,写入以下代码
#include
#include
using namespace cv;
int main( int argc, char** argv )
{
std::cout << "aa" << std::endl;
std::string img = "lenna.jpg";
Mat srcImage = imread(img);
if (!srcImage.data) {
return 1;
}
imshow("srcImage", srcImage);
waitKey(0);
return 0;
}
然后放置一个名为 lenna.jpg 的图像。
之后,编译项目并运行它,如果没有问题,你应该会得到一个带有 Lenna 图像的窗口。
要构建和运行项目,您可以按 Ctrl+Shift+B
构建并键入要在终端中运行的可执行文件名称。有关更多信息,您可以查看此官方指南。
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\opencv\\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
launch.json
{
// This file originates from here: https://code.visualstudio.com/docs/cpp/config-mingw
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I","C:\\opencv\\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\\include",
"-L","C:\\opencv\\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\\x64\\mingw\\bin",
"-llibopencv_calib3d411",
"-llibopencv_core411",
"-llibopencv_dnn411",
"-llibopencv_features2d411",
"-llibopencv_flann411",
"-llibopencv_highgui411",
"-llibopencv_imgcodecs411",
"-llibopencv_imgproc411",
"-llibopencv_ml411",
"-llibopencv_objdetect411",
"-llibopencv_photo411",
"-llibopencv_stitching411",
"-llibopencv_video411",
"-llibopencv_videoio411"
],
"options": {
"cwd": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
在这篇文章中,我教授如何使用 OpenCV C++ 包从头开始设置 VS Code。
https://medium.com/analytics-vidhya/vs-code-with-opencv-c-on-windows-10-explained-256418442c52
https://code.visualstudio.com/docs/languages/cpp
https://cuda-chen.github.io/programming/image%20processing/2020/01/21/vscode-with-opencv-cpp-on-windows10-explained.html