在vscode中使用opencv

在vscode中使用opencv2.4.12,在已安装opencv3.x的情况下

之前我们讲了如何编译opencv2.4.12的方法,本文讲如何在vscode中使用opencv2412。实际上,由于ubuntu下面已经安装了opencv3.x版本,一般而言,opencv3.x在安装后,头文件一般会被放在/usr/local/include目录下面。opencv2.4.12编译后是不能make install的,否则会覆盖掉opencv3.x的目录,可能产生包的引用混乱问题。

实际上,由于/usr/local/include这个文件夹会被系统默认的加入到开发环境,因此在代码中引用opencv的时候,会自动在该文件夹下面找相应的头文件。

若要使用opencv2.4.12,我们需要修改vscode的.vscode文件夹下面几个配置文件。目的其实就在搜索opencv头文件时,用opencv2.4.12的头文件夹路径,是覆盖掉/usr/local/include下的opencv3。

具体地,

  1. .vscode文件夹下新建c_cpp_properties.json文件,内容如下:
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/home/wyx/software/opencv-2.4.12/sources/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules",
                "/home/wyx/software/opencv-2.4.12/sources/modules/legacy",
                "/home/wyx/software/opencv-2.4.12/sources/modules/legacy/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/core/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/flann/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/features2d/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/objdetect/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/calib3d/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/imgproc/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/video/include",
                "/home/wyx/software/opencv-2.4.12/sources/modules/highgui/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc-4.8",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

注意这里面的includePath是你的opencv2412的各个模块的头文件路径,代码中缺什么就补什么。c_cpp_properties.json的目的是,只是提示vscode,该从哪里去找头文件。

  1. tasks.json内容如下:
{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++-4.8",
            "args": [
                "-g",
                "-std=c++11",
                "-c",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.o",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/include/opencv",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/include/opencv2",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/legacy",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/legacy/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/core/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/flann/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/features2d/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/objdetect/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/calib3d/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/imgproc/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/video/include",
                "-I", "/home/wyx/software/opencv-2.4.12/sources/modules/highgui/include",

                "-L", "/home/wyx/software/opencv-2.4.12/build/lib",
                "-l", "opencv_aruco",
                "-l", "opencv_bgsegm",
                "-l", "opencv_bioinspired",
                "-l", "opencv_calib3d",
                "-l", "opencv_ccalib",
                "-l", "opencv_core",
                "-l", "opencv_datasets",
                "-l", "opencv_dnn_objdetect",
                "-l", "opencv_dnn",
                "-l", "opencv_dpm",
                "-l", "opencv_face",
                "-l", "opencv_features2d",
                "-l", "opencv_flann",
                "-l", "opencv_freetype",
                "-l", "opencv_fuzzy",
                "-l", "opencv_hfs",
                "-l", "opencv_highgui",                
                "-l", "opencv_imgcodecs",
                "-l", "opencv_img_hash",
                "-l", "opencv_imgproc",
                "-l", "opencv_line_descriptor",
                "-l", "opencv_ml",
                "-l", "opencv_objdetect",
                "-l", "opencv_optflow",
                "-l", "opencv_phase_unwrapping",
                "-l", "opencv_photo",
                "-l", "opencv_plot",
                "-l", "opencv_reg",
                "-l", "opencv_rgbd",
                "-l", "opencv_saliency",
                "-l", "opencv_shape",
                "-l", "opencv_stereo",
                "-l", "opencv_stitching",
                "-l", "opencv_structured_light",
                "-l", "opencv_superres",
                "-l", "opencv_surface_matching",
                "-l", "opencv_text",
                "-l", "opencv_tracking",
                "-l", "opencv_videoio",
                "-l", "opencv_video",
                "-l", "opencv_videostab",
                "-l", "opencv_xfeatures2d",
                "-l", "opencv_ximgproc",
                "-l", "opencv_xobjdetect",
                "-l", "opencv_xphoto"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

tasks.json是构建任务的默认配置,"-I"的部分和c_cpp_properties.json的头文件路径一致,-L就是编译opencv2412后的lib路径,-l就是库的名称,注意这里面可能有些名称在lib路径下是没有的,不过不影响。如果有-c参数的话,-L-l的部分是不需要的。

通过这样配置的话,就可以通过Ctrl+Shift+B对包含opencv2412的cpp文件进行编译了。

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