记录一下自己vscode + opencv配置过程

vscode + opencv配置过程

    • 1. 配置vscode C/C++编译环境
    • 2. 下载cmake编译程序
    • 3. 使用cmake程序build已下载好的opencv库
    • 4. vscode配置opencv库
    • 5. 运行测试程序

1. 配置vscode C/C++编译环境

本文使用MinGW编译器, 详情配置请看该链接

2. 下载cmake编译程序

为了编译opencv库, 需要安装cmake程序。

  • cmake官网下载链接
  • cmake安装教程

3. 使用cmake程序build已下载好的opencv库

  • opencv官网下载
  • cmake如何build opencv

我的opencv build完成后文件部署情况:

记录一下自己vscode + opencv配置过程_第1张图片

4. vscode配置opencv库

在C/C++项目目录下创建.vscode文件,需要创建c_cpp_properties.jsonlaunch.jsontasks.json这3个文件。
配置过程中一定要注意原biild文件cmake程序build文件。

记录一下自己vscode + opencv配置过程_第2张图片

  • c_cpp_properties.json 配置
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                // 引入opencv库
                "E:\\codeLib\\opencv\\opencv\\build\\include",  // 原build文件夹下
                "E:\\codeLib\\opencv\\opencv\\build\\include\\opencv2", // 原build文件下
                "E:\\codeLib\\opencv\\opencv\\mingw_build\\lib"  // mingw_build为cmake build后的文件夹下
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "D:/software/Tools/gcc/mingw64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
  • launch.json 文件不需要修改
{
  "configurations": [
    {
      "name": "C/C++: cpp.exe 生成和调试活动文件",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "D:\\software\\Tools\\gcc\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "为 gdb 启用整齐打印",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "将反汇编风格设置为 Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: cpp.exe 生成活动文件"
    }
  ],
  "version": "2.0.0"
}
  • tasks.json
{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe 生成活动文件",
      "command": "D:\\software\\Tools\\gcc\\mingw64\\bin\\g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        // opencv  cmake build文件下
        "-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include",
        "-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include\\opencv2",
        "-L", "E:\\codeLib\\opencv\\opencv\\mingw_build\\lib",
        "-l", "opencv_core455",
        "-l", "libopencv_imgproc455",
        "-l", "libopencv_video455",
        "-l", "libopencv_ml455",
        "-l", "libopencv_highgui455",
        "-l", "libopencv_objdetect455",
        "-l", "libopencv_flann455",
        "-l", "libopencv_imgcodecs455",
        "-l", "libopencv_photo455",
        "-l", "libopencv_videoio455"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "调试器生成的任务。"
    },
    {
      "type": "cppbuild",
      "label": "C/C++: cpp.exe 生成活动文件",
      "command": "D:\\software\\Tools\\gcc\\mingw64\\bin\\cpp.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
          // opencv cmake build文件下
          "-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include",
          "-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include\\opencv2",
          "-L", "E:\\codeLib\\opencv\\opencv\\mingw_build\\lib",
          "-l", "opencv_core455",
          "-l", "libopencv_imgproc455",
          "-l", "libopencv_video455",
          "-l", "libopencv_ml455",
          "-l", "libopencv_highgui455",
          "-l", "libopencv_objdetect455",
          "-l", "libopencv_flann455",
          "-l", "libopencv_imgcodecs455",
          "-l", "libopencv_photo455",
          "-l", "libopencv_videoio455"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": "build",
      "detail": "调试器生成的任务。"
    }
  ],
  "version": "2.0.0"
}

5. 运行测试程序

#include 
#include 
using namespace cv;
int main()
{
    std::string imagePath = R"(E:\picture\background\1.jpg)";
    Mat img = imread(imagePath); //读取图片
    imshow("img", img);          //显示图片
    waitKey(-1);                 //等待按键 不然会一闪而过
    return 0;
}

运行成功,Nice !

你可能感兴趣的:(opencv,vscode)