Linux安装OpenCV并配置VSCode环境
- 安装OpenCV环境
- 安装必需工具
- 下载并解压OpenCV库(Opencv Core Modules和opencv_contrib)
- 创建构建目录,进行构建
- 验证构建结果
- 安装
- 验证安装结果
- 配置VSCode环境
- 创建项目文件
- 修改配置信息
- 执行程序
安装环境
Ubuntu20.04
官方参考网址
OpenCV官网:https://opencv.org/
官方安装文档参考:https://docs.opencv.org/4.8.0/d7/d9f/tutorial_linux_install.html
安装OpenCV环境
安装必需工具
# cmake -> 构建工具
# g++ -> c++编译器
# unzip -> zip文件解压工具
# libgtk2.0-dev pkg-config是图形界面支持的工具,不安装执行程序时会报错
sudo apt update && sudo apt install -y cmake g++ libgtk2.0-dev pkg-config wget unzip
下载并解压OpenCV库(Opencv Core Modules和opencv_contrib)
Opencv Core Modules: OpenCV的核心库模块是OpenCV的主要部分,包含了图像处理和计算机视觉方面的核心功能。
opencv_contrib:OpenCV的附加库,它包含了OpenCV官方贡献的扩展功能和算法,非必须安装的库文件,仅当需要使用该库时安装即可。
# 进入到安装目录,创建opencv父目录
cd ~ # 安装目录自选,如进入到根目录下的子目录,后面的命令前均需要添加sudo
mkdir opencv && cd opencv
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip
unzip opencv.zip
unzip opencv_contrib.zip
# 删除安装包
sudo rm -rf opencv*.zip
创建构建目录,进行构建
# 在opencv目录下创建build文件
mkdir -p build && cd build
# 配置构建的 opencv-4.x 和 opencv_contrib-4.x 项目的路径,
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x
# 构建当前目录下的CMake项目,--build 指定编译生成的文件存放目录,其中就包括可执行文件,. 表示存放到当前目录
cmake --build .
构建过程花费时间很长,我这边用了将近半个小时,需要耐心等待一下
验证构建结果
构建完成后,我们可以在build/lib路径下看到库文件,在build/bin路径下看到一些可执行的文件(样例,测试,apps等),运行以下指令可以查看上述文件是否存在
ls bin
ls lib
在build目录下看到CMake package files,运行以下指令可查看
ls OpenCVConfig*.cmake
ls OpenCVModules.cmake
安装
若上述检查无误,进行安装
sudo make install
按照默认设置,opencv会被安装到 /usr/local 目录下,所有的文件会被复制到以下位置:
/usr/local/bin - executable files
/usr/local/lib - libraries (.so)
/usr/local/cmake/opencv4 - cmake package
/usr/local/include/opencv4 - headers
/usr/local/share/opencv4 - other files (e.g. trained cascades in XML format)
opencv官方说明:安装过程只将文件复制到预定义的位置,并进行少量的修补。使用这种方法进行安装不会将opencv集成到系统包注册表中,因此,例如,opencv不能自动卸载。由于可能与系统包冲突,我们不建议普通用户进行全系统安装。
验证安装结果
使用opencv自带的samples验证即可
cd ~/opencv-4.x/samples/cpp/example_cmake # 进入opencv核心库文件夹的samples下的cpp的案例文件中
# 构建
cmake .
# 编译并生成可执行文件
make
# 执行文件
./opencv_example
执行后,显示OpenCV的版本即代表安装成功
配置VSCode环境
创建项目文件
VSCode是基于文件夹管理项目,首先创建文件夹,使用VSCode打开同时创建Cpp文件(同级目录下添加一张图片,便于测试),cpp内容如下
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat srcImage=imread("1.jpg");
imshow("Origin",srcImage);
waitKey(0);
return 0;
}
安装C/C++拓展,点击
拓展
,自行搜索安装C/C++
和C/C++ Extension Pack
计科
修改配置信息
点击调试,选择
创建launch.json文件
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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/${fileBasenameNoExtension}.main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
点击
允许
,点击配置任务
,选择使用模板创建tasks.json文件 > Others
tasks.json
配置如下:
{
// 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",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.main.out",
"-I",
"/usr/local/include",
"-I",
"/usr/local/include/opencv4",
"-I",
"/usr/local/include/opencv4/opencv2",
"-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"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": [
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Ctrl+Shift+P
搜索找到Edit Configurations(JSON)/编辑配置(JSON)
c_cpp_properties.json
配置如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
// 添加头文件查找路径
"/usr/include",
"/usr/local/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/cpp",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
执行程序
环境配置完成,回到项目,单击
F5
执行程序,运行成功
注:如运行后出现如下信息,则是没有安装
libgtk2.0-dev
和pkg-config
,回到第一步,安装好所有工具重新进行构建、编辑、安装
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.8.0-dev) /usr/local/opencv/opencv-4.x/modules/highgui/src/window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'