0


vscode配置opencv


前言

    本篇文章主要用来记录使用vscode配置opencv的全过程,在整个过程中需要用到的工具包括vscode安装包、MinGW-w64和opencv的源码。vs studio配置opencv比较简单,opencv官网中已经有用vs studio编译器编译好的opencv库,但是对于vscode而言,不能直接使用利用vs studio编译器编译好的opencv库,因此需要借助MinGW-w64和CMake工具对opencv源码重新进行编译,编译的前提是你的C:\Users目录下的用户名称不包含中文,否则无法成功编译,从头编译的过程参照以下链接,

https://blog.csdn.net/Xiao_Xue_Seng/article/details/108552346

因为在cmake的过程中将在C:\Users\XXX\AppData下创建相关的文件夹,如果路径中包含中文则无法创建相应的文件夹导致编译出错,如果头铁也可以通过改注册表的方式修改用户名为英文,修改方式参照下面的链接,

https://blog.csdn.net/qq_38232598/article/details/102836028

修改注册表可能导致计算机崩溃,不怕麻烦的话可以尝试一下,由于怕麻烦,因此本文从下载编译好的opencv库开始配置。


一、安装MinGW-w64

    从下面链接中下载MinGW-w64的安装文件,安装完成后将D:\mingw64\bin添加到环境变量,具体路径按照你自己的安装位置确定。

https://sourceforge.net/projects/mingw-w64/files/

二、下载编译好的opencv库

    从下面的连接中下载**编译好的opencv库,**

https://github.com/huihut/OpenCV-MinGW-Build

从该网站中下载所需要的opencv库,下载完成后解压到电脑中的随机位置。

三、配置vscode

    配置方式有两种选择,第一种是vscode中安装了code-runner工具,在该情况下,需要修改code-runner.executorMap,导入编译所需要的相关文件。方法是在vscode界面,同时按住ctrl + ','(逗号),在弹出来的搜索设置中输入runner.executorMap,

点击在settings.json中编辑,

首次打开settings.json可能只有"window.zoomLevel"和"code-runner.runInTerminal":两项内容,换行键入"code-runner.executorMap":之后,将会自动补全下面的内容,找到"cpp"这一项,用下面的内容替换原来的内容,

"cpp":"cd $dir && g++ $fileName -o $fileNameWithoutExt -I D:/softInstaller/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/include -L D:/softInstaller/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin -l libopencv_calib3d410 -l libopencv_core410 -l libopencv_dnn410 -l libopencv_features2d410 -l libopencv_flann410 -l libopencv_gapi410 -l libopencv_highgui410 -l libopencv_imgcodecs410 -l libopencv_imgproc410 -l libopencv_ml410 -l libopencv_objdetect410 -l libopencv_photo410 -l libopencv_stitching410 -l libopencv_video410 -l libopencv_videoio410 -l opencv_ffmpeg410_64 && $dir$fileNameWithoutExt",

其中,-I表示导入头文件文件夹,-L表示依赖库导入,-l表示依赖库中dll文件的名称,dll的名称要按照下载的opencv库里的内容加以修改,否则会报错。另外就是有些博客中会有libopencv_worldxxx.dll,该文件仅仅是所有依赖的集成体,不是必须的,可以通过像上述一条一条添加的方式解决。

    第二种方式是不借助code-runner,使用vscode自带的调试工具,此时需要launch.json,tasks.json和c_cpp_properties.json三个文件,分别如下所示,

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",                
            "type": "cppdbg",                         
            "request": "launch",                        
            "targetArchitecture": "x86",                
            "program": "${workspaceRoot}\\${fileBasename}.exe",                 
            "miDebuggerPath":"D:\\mingw64\\bin\\gdb.exe", 
            "args": [],     
            "stopAtEntry": false,                  
            "cwd": "${workspaceRoot}",                  
            "externalConsole": true,                  
            "preLaunchTask": "g++"             
            }
    ]
 }

注意修改"miDebuggerPath"的路径。

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "win",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/softInstaller/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/include",
                "D:/softInstaller/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/include/opencv2"       
            ],
            "defines": [],
            "compilerPath": "D:/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

注意修改"includePath"、 "compilerPath"的路径。

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "opencv4.1.0 compile task",
            "type": "shell",
            "command": "g++",
            "args":[
            "-g",
            // "${fileBasename}",
            "${workspaceRoot}\\${fileBasename}",
            "-o",
            "${workspaceRoot}\\${fileBasenameNoExtension}", 
            // "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
            "-I",
            "D:/softInstaller/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/include",
            "-L",
            "D:/softInstaller/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin",
            "-l",
            "libopencv_calib3d410",
            "-l",
            "libopencv_core410",
            "-l",
            "libopencv_dnn410",
            "-l",
            "libopencv_features2d410",
            "-l",
            "libopencv_flann410",
            "-l",
            "libopencv_gapi410",
            "-l",
            "libopencv_highgui410",
            "-l",
            "libopencv_imgcodecs410",
            "-l",
            "libopencv_imgproc410",
            "-l",
            "libopencv_ml410",
            "-l",
            "libopencv_objdetect410",
            "-l",
            "libopencv_photo410",
            // "-l",
            // "libopencv_shape341",
            "-l",
            "libopencv_stitching410",
            // "-l",
            // "libopencv_superres341",
            "-l",
            "libopencv_video410",
            "-l",
            "libopencv_videoio410",
            // "-l",
            // "libopencv_videostab341"
            "-l",
            "opencv_ffmpeg410_64"
            ],
            "options": {
                "cwd": "D:/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": { 
                "panel": "new" //默认为“shared“表示共享,改成new之后每个进程创建新的端口
            }
        }
    ]
}

注意修改-I、-L和-l的内容,参数的含义同上文。此时,按住CTRL+shift+B可以进行编译,正常编译后将在-o指定的位置生成可执行文件。

    以上两种方法,都需要在打开cpp文件的时候运行,因为"${file}"参数指代的是当前的文件,如果打开一个.json文件运行编译将会报错。

2.测试用例

代码如下:

#include "opencv2/opencv.hpp"

#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;

int main()

{

Mat img=imread("E:/c++Project/images/Screenshot220315052912.png");

imshow("image",img);

waitKey();

return 0;

}。


参考

https://www.cnblogs.com/kensporger/archive/2020/02/19/12320622.html

标签: c++ vscode opencv

本文转载自: https://blog.csdn.net/Wddfh_frw/article/details/123762213
版权归原作者 Wddfh_frw 所有, 如有侵权,请联系我们删除。

“vscode配置opencv”的评论:

还没有评论