编程语言
首页 > 编程语言> > Win10 VSCode安装配置OpenCV(C/C++)

Win10 VSCode安装配置OpenCV(C/C++)

作者:互联网

VSCode安装配置OpenCV

前言

由于这个东西报错太多了,所以我干脆写一个完美的配置方案,并提供我配置调试成功的所有需要的依赖。只需按照此教程对应解包配置,就能完美复原我的环境。

需要的依赖

这些需要的资源都在下方的百度云链接中,有需要的可以自取。

链接:https://pan.baidu.com/s/19SMopAY7Wl3iJPxBmq1iqg
提取码:5llo

配置过程

具体步骤

1、解包到自己的路径下。

2、进行环境变量的配置。

3、创建一个存放vscode项目的目录,并使用vscode打开

4、为了确保配置以及调试成功,我安装了很多插件

5、在项目的 .vscode目录下手动新建文件 c_cpp_properties.jsonlaunch.jsontasks.jsonsettings.json。其中设置最后一个JSON文件是为了让 Code Runner插件也支持OpenCV

6、在文件中复制粘贴进对应文本,保存。

7、编写测试cpp程序,运行程序进行测试。

c_cpp_properties.json文件配置

点击查看代码
{
    "configurations": [
        {
            "name": "win",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/opencv/install/include",
                "D:/opencv/install/include/opencv2",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32/32"
                
            ],
            "defines": [],
            "compilerPath": "D:/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

launch.json文件配置

点击查看代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "opencv4.5.1 debuge",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "opencv4.5.1 compile task"
        }
    ]
}

tasks.json文件配置

点击查看代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "opencv4.5.1 compile task",
            "command": "D:/mingw64/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
                "D:/opencv/install/x64/mingw/bin/libopencv_world453.dll",
                "-I",
                "D:/opencv/install/include",
                "-I",
                "D:/opencv/install/include/opencv2"
            ],
            "options": {
                "cwd": "D:/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
	]
}

settings.json文件配置

点击查看代码
{
    "C_Cpp.errorSquiggles": "Enabled",
    "code-runner.executorMap": {
        
        "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=UTF-8 && &'$dir$fileNameWithoutExt'",

        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -I D:/opencv/install/include -I D:/opencv/install/include/opencv2 -L D:\\opencv\\install\\x64\\mingw\\lib -llibopencv_world453 && $dir$fileNameWithoutExt "
    }

}

测试用cpp代码

点击查看代码
 #include <opencv2/core.hpp>
 #include <opencv2/highgui.hpp>
 using namespace cv;
 int main(int argc, char const *argv[])
 {
    Mat image = imread("miller.jpg");
    namedWindow("image",0);
    imshow("image",image);
    waitKey(0);
    return 0;
 }

测试成功效果如下图:

Good Luck!

结语

好了,到这里基本上都成功运行了吧,开始愉快的编程吧!

标签:opencv,VSCode,C++,mingw64,OpenCV,json,64,install,include
来源: https://www.cnblogs.com/pisce/p/16552432.html