ubuntu 20.04 opencv4.5.4安装记录

安装opencv

  1. 首先需要安装编译该工具cmake
sudo apt-get install cmake

下载Opencv和opencv_contrib并编译
opencv下载:https://github.com/opencv/opencv/releases
opencv_contrib下载:https://github.com/opencv/opencv_contrib/tags
2. 安装opencv依赖文件

 sudo apt-get install build-essential 
 sudo apt-get install git libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

遇到问题: Ubuntu20.04下安装OpenCv依赖包libjasper-dev无法安装的问题

sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"
sudo apt update
sudo apt install libjasper1 libjasper-dev
  1. 解压并编译
tar -zxvf opencv-4.5.4.tar.gz
tar -zxvf opencv_contrib-4.5.4.tar.gz
## 将OpenCV-contrib文件解压后放入opencv解压后的文件夹,并修改contrib文件夹名字为opencv_contrib
cd opencv-4.5.4/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..

如果不需要contrib模块,则输入:

cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D ENABLE_CXX11=1 -D OPENCV_ENABLE_NONFREE=True ..

如果如要GPU加速和contrib模块,则输入:

cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D ENABLE_CXX11=1 -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D OPENCV_ENABLE_NONFREE=True -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D WITH_OPENMP=ON -D WITH_CUFFT=ON -D WITH_CUBLAS=ON -D CUDA_FAST_MATH=1 -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..

输入

make -j$(nproc)
sudo make install

添加环境变量

1.终端输入

sudo gedit /etc/ld.so.conf.d/OpenCV.conf

添加

include /usr/loacal/lib 

2.执行命令

 sudo ldconfig

3.3.接着修改bash.bashrc文件 ,终端输入:

sudo gedit /etc/bash.bashrc 

文件末行加入:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
xport PKG_CONFIG_PATH 

4.最后source一下,让更改立即生效,终端输入:

source /etc/bash.bashrc  
sudo updatedb

**问题:**如果报错:sudo: updatedb:找不到命令

sudo apt-get install mlocate

5.通过 pkg-config 查看 opencv 版本

pkg-config --modversion opencv

测试

进入安装路径的build文件夹

cd ../samples/cpp/example_cmake
cmake .
make
./opencv_example

如果能够成功的调用摄像头,并通过串口显示图像,即安装成功。如果是通过ssh连接的远程主机,imshow语句不能正常使用。

如果输入pkg-config命令提示找不到opencv.c那就自己写一个

cd ~/local/lib
sudo mkdir pkgconfig && cd pkgconfig
sudo vim opencv.pc
prefix=~/local
exec_prefix=${prefix}
includedir=~/local/include
libdir=~/local/lib

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.1.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}

你可能感兴趣的:(树莓派,ubuntu,opencv,计算机视觉)