Ubuntu18下使用VSCode&&配置Opencv、c++

一、硬件设备

电脑:联想pro13 

系统:ubuntu18

二、目标

下载opencv

下载vscode

安装c++必备插件

配置opencv c++文件

三、实现

3.1、下载opencv

下载需要的opencv版本  找到目标版本后点击source安装

!注意opencv2和opencv3版本差别较大,opencv3和opencv4部分函数不通用

3.2、安装cmake

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get update
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev

3.3、配置(在opencv文件夹下打开终端)

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
make -j4
sudo make install
sudo make
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf 
加入       /usr/local/lib
sudo ldconfig
sudo gedit /etc/bash.bashrc 
写入
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
export PKG_CONFIG_PATH
sudo ldconfig
sudo cp /usr/local/include/opencv4/opencv2 /usr/local/include -r

3.4、测试

3.4.1、在终端测试

   在opencv文件夹下的/samples/cpp/example_cmake打开终端

   依次执行:

cmake .
make
./opencv_example

   打开摄像头则代表安装成功

3.4.2、在qt中测试

.pro文件

TEMPLATE = app

CONFIG += console c++11

CONFIG -= app_bundle

CONFIG -= qt

SOURCES += main.cpp

INCLUDEPATH += /usr/local/include \
                /usr/local/include/opencv
                /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_* \

调用摄像头

#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

int main()
{
    VideoCapture capture (0);
    Mat src;
    while(1)
    {

        capture >> src;
        imshow("capture",src);
        char(key)=(char)waitKey(1);
        if(key==27)
            break;
    }
    return 0;
}

3.2、配置VsCode

3.2.1、新建cpp文件,保存

在该文件所在文件夹打开终端输入:code .

3.2.2、配置

  a.launch.json文件

(左侧带小虫子的按钮)debug--open configuration--c++(GDB/LLDB)->clang

编辑launch.json文件:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - 生成和调试活动文件",
            "preLaunchTask": "build", 
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/run.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", 
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            // "preLaunchTask": "C/C++: g++ 生成活动文件",
            // "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

  b、task.json文件

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",	/* 定义任务是被作为进程运行还是在 shell 中作为命令运行,默认是shell,即是在终端中运行,因为终端执行的就是shell的脚本 */
			"label": "build",	/* 要与launch.json文件里的preLaunchTask的内容保持一致 */
			"command": "/usr/bin/g++",	/* 这里填写你的编译器地址 */
			"args": [
				/* 类似与qt的Pro文件里开始的那几句 */
				"-std=c++11",// 静态链接
                "-static-libgcc",
				"-Wall",// 开启额外警告
				
				/* 说明整个项目所需的头文件路径(.h)*/
				"-I","${workspaceFolder}/",
                "-I","/usr/local/include/",
                "-I","/usr/local/include/opencv4/",
                "-I","/usr/local/include/opencv4/opencv2/",

				/* 说明整个项目所需的源文件路径(.cpp) */
                "-g",
                "${workspaceFolder}/main.cpp",/* ${workspaceFolder}表示路径从当前项目文件夹开始 */

				"-o",	/* 编译输出文件的存放路径 */
				"${workspaceFolder}/run.o", /* 要与launch.json文件里的program的内容保持一致 */
				/* ${fileDirname} 是指 文件目录名 相当于${workspaceFolder}*/
				/* ${fileBasenameNoExtension}意思是指 该路径下没有扩展名的文件基本名称没有扩展名 */ 
				/* 也可以这样:"${workspaceFolder}/run.o", */

				/* OpenCV的lib库 */
				"/usr/local/lib/libopencv_*",
			],
			"options": {
                "cwd": "${workspaceFolder}"	/* 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录 */
			},
			"problemMatcher": [
				"$gcc"	/* 要使用的问题匹配程序。 */
			],
			"group": "build" /* 将任务标记为可通过 "运行生成任务" 命令访问的生成任务。*/
		}
	]
}

   c、ctrl+shift+p-->edit configurations(json)-->修改c_cpp_properties,json文件

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/",
                "/usr/local/include/opencv4/",
                "/usr/local/include/opencv4/opencv2/"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

测试代码:

#include 
#include
#include
using namespace std;
using namespace cv;
int main() 
{
    cout<<"hello world"<>frame;
        imshow("src",frame);
        waitKey(27);
    }
}

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