以下步骤在ubuntu16.04、18.04中测试通过
1、下载指定版本opencv
wget https://github.com/opencv/opencv/archive/4.2.0.zip解压到opencv
wget https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
解压到opencv_contrib
解压后把opencv_contrib文件夹放进opencv中
例如,如果要下载opencv3.4.5,只需要把4.2.0.zip改为3.4.5.zip
2、安装依赖
sudo apt install build-essential cmake pkg-config git
//process images
sudo apt install libjpeg8-dev libtiff5-dev
ubuntu16.04:
sudo apt install libjasper-dev libpng12-dev
ubuntu18.04 把libpng12-dev替换成了libpng-dev
sudo apt install libpng-dev
ubuntu18.04安装 libjasper-dev:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper-dev
//process video streams and access frames from cameras安装inux中关于视频设备的内核驱动
sudo apt install libv4l-dev
安装视频编解码包
sudo apt install libxvidcore-dev libx264-dev libx265-dev
安装ffmpeg依赖库(这些库在源码编译ffmpeg的不用单独下载,因为ffmpeg源码中包含有这些库的源码)
sudo apt install libavutil-dev libavcodec-dev libavformat-dev libavdevice-dev libavfilter-dev libavresample-dev libswscale-dev libswresample-dev libpostproc-dev
安装ffmpeg
sudo apt install ffmpeg
安装1394协议
sudo apt install libdc1394-22-dev
libdc1394 is a library that provides a complete high level application programming interface (API) for developers who wish to control IEEE 1394 based cameras that conform to the 1394-based Digital Camera Specifications
安装qt
sudo apt install qt5-default qtcreator
//GUI
sudo apt install libgtk-3-dev
//Eigen,把opencv Mat转换为Eigen矩阵//但是使用Eigen的算法,仍需到Eigen官网下载Eigen库
sudo apt install libeigen3-dev
//optimize various functionalities inside OpenCV, such as matrix operations
sudo apt install libatlas-base-dev gfortran libtbb2 libtbb-dev
//others
sudo apt install libboost-all-dev
sudo apt install liblapacke-dev
BLAS 和LAPACK是基于fortran的一套接口规范,仅仅实现了基本功能,atlas、mkl、openBLAS是BLAS、LAPACK的优化实现。
cbals、clapack是BLAS 和LAPACK的C接口.
after dependencies,remember sudo ldconfig
切记:
需要退出所有conda环境,包括conda base环境
特别注意: OpenCV4默认不生成.pc文件,要开启OPENCV_GENERATE_PKGCONFIG选项
3、编译
切记:
此步骤需要翻墙才能下载用来加速opencv的IPP库和.i文件,没有.i文件不能正常编译opencv_contrib。
如果不想安装IPP,需设置WITH_IPP=OFF
cd <opencv-root>
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=~/app/opencv3 -D BUILD_SHARED_LIBS=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D WITH_V4L=ON -D WITH_LIBV4L=ON -D WITH_QT=ON -D WITH_OPENGL=OFF -D WITH_IPP=ON -D WITH_TBB=ON -D WITH_OPENMP=ON -D WITH_EIGEN=ON -D WITH_CUDA=OFF -D BUILD_OPENCV_PYTHON3=OFF -D BUILD_OPENCV_PYTHON2=OFF -D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv-4.2.0/opencv_contrib/modules ..
BUILD_SHARED_LIBS=OFF生成静态库
make -j20
make install
4、配置.bashrc
export PATH=/home/username/app/opencv3/bin${PATH:+:${PATH}}export LD_LIBRARY_PATH=/home/username/app/opencv3/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export PKG_CONFIG_PATH=/home/username/app/opencv3/lib/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}