ubuntu20.04配置opencv4环境

ubuntu换源准备(提高从国外下载资源的速度)

传送门

1.保证系统中各个软件和软件源都是更新过的

执行以下命令:

#更新源
sudo apt-get update
#更新已安装的包
sudo apt-get upgrade 

2.安装OpenCV依赖的库

sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

3.下载OpenCV源代码

官网:https://opencv.org/releases/
本人下载的版本为OpenCV-4.1.0
找到下载目录打开命令行输入:

unzip opencv-4.1.0.zip
mv opencv-4.1.0 opencv

进入到opencv目录,新建目录build并进入build目录:

cd opencv
mkdir build
cd build

使用cmake配置opencv

$ cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES ..

使用make编译

make -j8

使用make安装

sudo make install

4.环境配置

find / -iname opencv4.pc
sudo vim /etc/profile.d/pkgconfig.sh
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
source /etc/profile

验证是否成功

$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core

5.配置动态库环境

sudo vim /etc/ld.so.conf.d/opencv4.conf
/usr/local/lib
sudo ldconfig

6.测试

方法一:

pkg-config --modversion opencv4

输出结果应为:
4.1.0

方法二:
cd 到/opencv/samples/cpp/example_cmake目录下,依次执行以下命令:

cmake .
make
./opencv_example

此时会弹出一个摄像头的实时显示窗口
如要退出该程序只需在命令行同时按住ctrl+c

你可能感兴趣的:(opencv,ubuntu)