linux安装opencv

本人安装的linux版本为20.04

准备工作

由于linux系统本身安装源不是国内源,安装很慢或者会安装不上,所以先将安装源进行更改:

使用

sudo gedit /etc/apt/sources.list

出现以下内容

#deb cdrom:[Ubuntu 20.04.4 LTS _Focal Fossa_ - Release amd64 (20220223)]/ focal main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ focal main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ focal main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ focal-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ focal-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ focal universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ focal universe
deb http://cn.archive.ubuntu.com/ubuntu/ focal-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ focal-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://cn.archive.ubuntu.com/ubuntu/ focal multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ focal multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ focal-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ focal-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb http://security.ubuntu.com/ubuntu focal-security main restricted
# deb-src http://security.ubuntu.com/ubuntu focal-security main restricted
deb http://security.ubuntu.com/ubuntu focal-security universe
# deb-src http://security.ubuntu.com/ubuntu focal-security universe
deb http://security.ubuntu.com/ubuntu focal-security multiverse
# deb-src http://security.ubuntu.com/ubuntu focal-security multiverse

# This system was installed using small removable media
# (e.g. netinst, live or single CD). The matching "deb cdrom"
# entries were disabled at the end of the installation process.
# For information about how to configure apt package sources,
# see the sources.list(5) manual.

除第一行保留,其余行进行删除,对镜像原进行更改,我将其改成了阿里源

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

1、安装

1.1C++编译环境配置

linux系统下使用C++开发OPencv项目,首先需要搭建C++开发环境:

在终端输入:

sudo apt install gcc
sudo apt install g++

安装C++编译器GCC、G++

输入

sudo apt install cmake

安装cmake编译工具

1.2 安装相关依赖库

依次安装libgtk(GTK(GIMP Toolkit)是一个Linux平台下基于Xwindow图形窗口的图形用户编程接口工具,可以借助它来开发Linux平台下基于Xwindow的图形用户界面。)

安装pkg-config

安装ffmpeg,ffmpeg(命令行工具) 是一个快速的音视频转换工具

安装libavcodec-dev

安装libavformat-dev

安装libswscale-dev

sudo apt install libgtk2.0-dev
sudo apt install pkg-config
sudo apt install ffmpeg
sudo apt install libavcodec-dev 
sudo apt install libavformat-dev
sudo apt install libswscale-dev

2、安装Opencv

linux安装opencv_第1张图片

 

下载source

https://opencv.org/releases/

找到自己下载的位置

在终端输入

unzip opencv-4.5.5.zip

解压完成后当前目录下会生成opencv-4.5.5文件夹,进入该文件夹

cd opencv-4.5.5

在此路径下新建一个编译目录build

mkdir build

进入编译目录

cd build

进行cmake-make编译

然后使用cmake命令进行配置

 cmake -D WITH_TBB=ON -D WITH_EIGEN=ON -D OPENCV_GENERATE_PKGCONFIG=ON  -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF  -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=O -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

这条命令会在build目录里生成对应配置的Makefile文件,可以看到配置信息之间是通过空格和-D来分割和标示的,配置了很多信息,我觉得比较重要的一个是 -D OPENCV_GENERATE_PKGCONFIG=ON,生成opencv.pc文件的配置(注意,opencv4生成的文件叫做opencv4.pc),另外就是 CMAKE_INSTALL_PREFIX=/usr/local这个关于安装路径的配置,这里的安装路径是在/usr/local下,这也是opencv的默认配置。


此时当前目录仍是build,直接编译安装即可

8核编译

make -j8
make install

安装完成后,需要对环境变量进行配置。进入root权限,在终端输入:

sudo -i
vim /etc/ld.so.conf.d/opencv.conf

在vim中进入编辑模式输入:/usr/local/lib,退出保存。

 然后在终端输入:vim /etc/bash.bashrc,进入bash.bashrc文件,在尾行输入:PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH

 配置成功,退出root。

3、Opencv测试

终端输入:ldconfig -v | grep opencv,检查opnecv是否安装成功

ldconfig -v | grep opencv

linux安装opencv_第2张图片

 在任意目录下创建opencv文件夹,将测试图片放过去,编写测试代码

test.cpp

#include
#include
#include
#include
 
 
#define Usage()\
{std::cerr<<"usage: ./showpic FILE"<

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage test.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

在终端cmake-make编译

mkdir build
cd build
cmake ..
make

编译成功后,运行生成的可执行文件DisplayImage

./DisplayImage

到这里就结束了,会显示你的图片

本文参考连接

(52条消息) linux安装opencv_AI界扛把子的博客-CSDN博客_linux安装opencv

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