linux下如何在vscode中配置opencv环境

  • Post author:
  • Post category:linux

这里使用的是C++作为编程语言,当然默认读者已经配置好了C++的编程环境

首先使用pkg-config --modversion opencv命令查看版本,确保成功安装了

o

p

e

n

c

v

opencv

opencv,我的版本是4.5.4

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(){
    cv::Mat image = cv::imread("tiger.jpg");
    cv::namedWindow("My Image");
    cv::imshow("My Image", image);
    cv::waitKey(5000);
    return 1;
}

如果现在新建一个cpp文件,输入上面的内容(这个tiger.jpg应该是当前目录下的一张图片)看到的应该是下面的画面
在这里插入图片描述

这时候,我们打开c_cpp_properties.json文件,在includePath中加入一行"/usr/local/include/**",改成下面这样

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/cpp",
            "cStandard": "gnu17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

这时候我们发现红色波浪线消失了
在这里插入图片描述
再次运行,报错如下
在这里插入图片描述
这时候,我们打开tasks.json文件,在上面的args中加入

                "-I", "/usr/local/include",
                "-I", "/usr/local/include/opencv4",
                "-L", "/usr/local/lib",
                "-l", "opencv_core",
                "-l", "opencv_imgproc",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_video",
                "-l", "opencv_ml",
                "-l", "opencv_highgui",
                "-l", "opencv_objdetect",
                "-l", "opencv_flann",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_photo",
                "-l", "opencv_videoio"

这个文件变成了下面这样

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-std=c++11",
                "-o",
                "${fileBasenameNoExtension}.out",

                "-I", "/usr/local/include",
                "-I", "/usr/local/include/opencv4",
                "-L", "/usr/local/lib",
                "-l", "opencv_core",
                "-l", "opencv_imgproc",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_video",
                "-l", "opencv_ml",
                "-l", "opencv_highgui",
                "-l", "opencv_objdetect",
                "-l", "opencv_flann",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_photo",
                "-l", "opencv_videoio"
            ]
        },
        {
            "type": "cppbuild",
            "label": "C/C++: cpp 生成活动文件",
            "command": "/usr/bin/cpp",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "编译器: /usr/bin/cpp"
        }
    ]
}

然后回来再次运行,依然报错
在这里插入图片描述
这是一个新的问题,按照指示,我们

i

n

s

t

a

l

l

 

l

i

b

g

t

k

2.0

d

e

v

 

a

n

d

 

p

k

g

c

o

n

f

i

g

install\ libgtk2.0-dev\ and\ pkg-config

install libgtk2.0dev and pkgconfig,使用sudo apt-get install命令安装即可,之后要重新编译一遍

o

p

e

n

c

v

opencv

opencv,可参考
https://blog.csdn.net/Lin_QC/article/details/104431330
接下来我们再次运行程序就成功了,大概是下面这个样子,这是我本地的一张图片
在这里插入图片描述
那么现在就配置好opencv了


版权声明:本文为roadtohacker原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。