opencv + vscode + linux/ window

OpenCV 配置安装+linux/win10+vscode

  • Linux环境
    • 1.安装依赖
    • 2.下载源码 4.6.0
    • 3.编译
    • 4.环境配置
    • 5.程序验证
    • 6.VSCode 配置
  • Window下 VSCode
    • 1.依赖环境
    • 2.下载源码 4.6.0
    • 3.编译
    • 4.添加环境变量
    • 5.程序验证
    • 6.VSCode配置
    • 安装opencv-contrib
      • Linux

Linux环境

前提:安装cmake

1.安装依赖

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt-get install libcanberra-gtk-module
sudo apt-get install pkg-config

2.下载源码 4.6.0

网址:https://opencv.org/releases/
opencv + vscode + linux/ window_第1张图片

3.编译

进入源码目录,新建build文件夹,并进入
若想同时安装opencv-contrib, 本节参考最后文介绍安装流程

cd ~/opencv-4.6.0/
mkdir -p build 
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local .. 
sudo make -j16  # 编译一阵子
sudo make install

4.环境配置

打开文件 :
打开文件 :
sudo gedit /etc/ld.so.conf
文件中添加:
include /usr/local/lib 
退出保存

# 这么做目的是避免出现 error while loading shared libraries错误 
cd /etc/ld.so.conf.d/
ls # 查看应该是没有opencv.conf的,则新建
sudo touch opencv.conf
sudo gedit 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
退出保存
source /etc/bash.bashrc

查看opencv安装成功情况

pkg-config opencv --modversion

如果报错提示没有找到文件 ,则自己创建.pc文件

cd /usr/local/lib/pkgconfig
touch opencv.pc
sudo gedit opencv.pc
文件内容添加 :
prefix=/usr/local
exec_prefix=${prefix}
includedir=/usr/local/include
libdir=/usr/local/lib
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.6.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}
退出保存

5.程序验证

/*  test.cpp */
#include 
#include 
using namespace cv;
using namespace std;
int main(int argc, char** argv )
{
  Mat A = (Mat_<double>(2,2) << 1,2,3,4);
  cout << A << endl;
  return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(MAIN)
find_package(OpenCV REQUIRED)
add_executable(main test.cpp) 
target_link_libraries(main PRIVATE ${OpenCV_LIBS})

编译方式:

cd build
cmake .. 
make -j12

6.VSCode 配置

c_cpp_properties.json 编写代码时,接口提示链接。

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

task.json 执行F5 时,运行程序所需编译参数,配置如下:
args 下 -I(大写i) ,后参数是include的文件夹。-l(小写L),链接so

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-I", "/usr/local/include/opencv4",
                "-I", "/usr/local/include/opencv4/opencv2",
                "-L", "/usr/local/lib/libopencv*"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

-L 引入批量so
-l 方法可通过, 查看CMake文件 变量${OpenCV_LIBS}
或者 :

ls /usr/local/lib | grep "libopen"

Window下 VSCode

1.依赖环境

  1. 安装cmake
  2. 安装vscode
  3. 安装mingw,window下g++编译程序。
    直接解压现有文件夹即可,并配置环境变量,如下图。
    opencv + vscode + linux/ window_第2张图片
    环境变量配置的:D:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin

2.下载源码 4.6.0

网址:https://opencv.org/releases/
选择Windows下载,下载后:
opencv + vscode + linux/ window_第3张图片

3.编译

window,开始菜单下,打开cmake-gui。,路径设置如下,没有其文件夹则先手动新建
opencv + vscode + linux/ window_第4张图片点击 Configure,选择 Mingw Makefiles, 然后点击Generate 。 等待完成Done。
打开cmd,或者vscode下终端,进入到 D:\opencv-4.6.0\opencv\build\x64\mingw 。
执行命令。

mingw32-make.exe
mingw32-make.exe install

4.添加环境变量

右键我的电脑-属性-环境配置
opencv + vscode + linux/ window_第5张图片

5.程序验证

参考linux下程序验证即可,编译方式:

cd build
cmake -G "MinGW Makefiles" .. 
mingw32-make.exe

6.VSCode配置

ctrl+shift+p , 输入框输入ui, 可以配置json文件
在这里插入图片描述
c_cpp_properties.json 编写代码时,接口提示链接。

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/opencv-4.6.0/opencv/build/x64/mingw/install/include",
                "D:/opencv-4.6.0/opencv/build/x64/mingw/install/include/opencv2",
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        },
        {
            "name": "g++",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/opencv-4.6.0/opencv/build/x64/mingw/install/include",
                "D:/opencv-4.6.0/opencv/build/x64/mingw/install/include/opencv2",

            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

task.json 执行F5 时,运行程序所需编译参数,配置如下:
args 下 -I(大写i) ,后参数是include的文件夹。-l(小写L),链接dll库

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I", "./include",
                "-I","D:/opencv-4.6.0/opencv/build/x64/mingw/install/include",
                "-I","D:/opencv-4.6.0/opencv/build/x64/mingw/install/include/opencv2",
				"-L","D:/opencv-4.6.0/opencv/build/x64/mingw/bin",
				"-l","libopencv_world460"
                
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

安装opencv-contrib

Linux

下载同版本代码,github地址:https://github.com/opencv/opencv_contrib/tree/4.6.0
选择 Tags 4.6.0 (本文opencv版本为460)

进入到opencv源码文件夹内, 上述编译linux下 第3节编译的同路径,已经建立了build,直接进入即可

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE 
-D CMAKE_INSTALL_PREFIX=/usr/local 
-D OPENCV_EXTRA_MODULES_PATH=/home/abc/opencv_contrib/modules 
-D OPENCV_ENABLE_NONFREE=True ..

sudo make -j16  # 编译一阵子
sudo make install

上述命令中 OPENCV_EXTRA_MODULES_PATH= 下载的opencv_contrib 的modules文件夹
之后进行环境配置,过程见上述4节环境配置即可。

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