Ubuntu 18.04配置OpenCV 4.2.0

目录

本文主要介绍在Ubuntu 18.04中从源码安装配置OpenCV,并使用一个简单的例子验证是否安装成功;

具体安装配置步骤,参考文章见:https://cv-tricks.com/installation/opencv-4-1-ubuntu18-04/

与上述链接中提供的教程不同的是:

  • 部分依赖包安装可能需要修改
  • 本文安装配置OpenCV版本为4.2,支持使用CUDA对DNN模块加速计算
  • 本文不涉及Python接口配置,仅配置为C++使用,所以会跳过步骤Step2~5

Step 1: 安装OpenCV的依赖包

一步一步的安装下面的所有依赖包:

sudo apt-get update -y # Update the list of packages
sudo apt-get remove -y x264 libx264-dev # Remove the older version of libx264-dev and x264
sudo apt-get install -y build-essential checkinstall cmake pkg-config yasm
sudo apt-get install -y git gfortran
sudo add-apt-repository -y "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev
sudo apt-get install -y libtiff5-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install -y libxine2-dev libv4l-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install -y libvorbis-dev libxvidcore-dev
sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install -y x264 v4l-utils
 
# Some Optional Dependencies
sudo apt-get install -y libprotobuf-dev protobuf-compiler
sudo apt-get install -y libgoogle-glog-dev libgflags-dev
sudo apt-get install -y libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

在安装上述依赖包的过程中,可能会存在一些错误提示,这里我将自己遇到的问题列出,并给出解决方案;
错误1:

E: Unable to locate package libjasper-dev

执行:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update

再次执行安装依赖包就行;

错误2:

E: Unable to locate package libgstreamer0.10-dev

执行:

sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

即可;

Step 2: 下载OpenCV 4.2.0和OpenCV Contrib 4.2.0

OpenCV 4.2.0:
https://github.com/opencv/opencv/releases/tag/4.2.0

OpenCV Contib 4.2.0:
https://github.com/opencv/opencv_contrib/releases/tag/4.2.0

假如将两个压缩包保存到/home/username/opencv4.2/,进行解压;

另外,在编译的时候需要ippicv_2019_lnx_intel64_general_20180723.tgz这个文件,下载的时候会特别慢。这里提供一个链接,参考其中的第1,2两个步骤进行下载与配置;

如,我将下载得到的文件放在了opencv4.2这个文件夹中,修改成"/home/username/opencv4.2/";

最后目录结构如下:

/home/username/opencv4.2/
    opencv-4.2.0/
    opencv_contrib-4.2.0/
    ippicv_2019_lnx_intel64_general_20180723.tgz

Step 3: 使用cmake构建库

执行:

cd /home/username/opencv4.2/opencv-4.2.0
mkdir build
cd build

执行:

cmake -D CMAKE_BUILD_TYPE=RELEASE       -D CMAKE_INSTALL_PREFIX=/usr/local       -D INSTALL_C_EXAMPLES=ON       -D CUDA_ARCH_BIN='7.5'
      -D WITH_CUDA=ON
      -D WITH_TBB=ON       -D WITH_V4L=ON       -D WITH_QT=ON       -D WITH_OPENGL=ON       -D OPENCV_EXTRA_MODULES_PATH=/home/username/opencv4.2/opencv_contrib-4.2.0/modules       -D BUILD_EXAMPLES=ON       -D OPENCV_GENERATE_PKGCONFIG=YES ..

上述步骤需要修改的地方有两处:

CUDA_ARCH_BIN='7.5'

由于OpenCV 4.2支持使用CUDA对DNN模块进行加速计算,所以这里配置CUDA;在此之前需要自行配置好NVIDIA显卡的驱动与CUDA;

其中7.5指的是显卡的计算能力,我的是GTX 1660Ti,对应的计算力为7.5;

这里提供一个链接,可以参考:NVIDA CUDA显卡计算能力对应表

第二处需要修改的地方是:

OPENCV_EXTRA_MODULES_PATH=/home/username/opencv4.2/opencv_contrib-4.2.0/modules

这里修改成你本机的opencv_contrib-4.2.0/modules的位置

Step 4: 使用make构建库

查看CPU核心数:

nproc

如,我的CPU核心数为12,执行

cd /home/username/opencv4.2/
make -j12

等待一段时候,出现Configuration Done即可,

执行:

sudo make install

再次等待一段时候后,执行:

sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

Step 5: 修改opencv4.pc文件

如果上述配置成功,则会在/usr/local/lib/文件夹中出现一个pkgconfig文件夹,里面有一个opencv.pc文件,内容大致如下:

# Package Information for pkg-config

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv2
includedir_new=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.2.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -L/usr/lib/x86_64-linux-gnu -lGL -lGLU -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppicom -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda -llib64 -L-L/usr/lib -lx86_64-linux-gnu
Cflags: -I${includedir_old} -I${includedir_new}

注意:需要将第6行修改为:

includedir_old=${prefix}/include/opencv4/opencv2

如果没有自动生成,可以试着新建一个文件,将上述的内容复制进去,继续下一步;

Step 6: 在.bashrc文件中添加PKG_CONFIG_PATH

执行:

sudo gedit ~/.bashrc

在文件最后添加:

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

退出后,执行

source ~/.bashrc

# 判断路径时候添加成功,返回:/usr/local/lib/pkgconfig即可
echo $PKG_CONFIG_PATH

Step 7: 使用C++代码进行验证

在任意目录下创建test.cpp文件,加入下面内容:

#include "opencv.hpp"
 
using namespace cv;
using namespace std;
 
int main( int argc, char** argv )
{
  cout << "OpenCV version : " << CV_VERSION << endl;
  cout << "Major version : " << CV_MAJOR_VERSION << endl;
  cout << "Minor version : " << CV_MINOR_VERSION << endl;
  cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;
}

使用命令行在其文件夹下执行:

# 编译test.cpp程序,并生成可执行文件
g++ -std=c++11 test.cpp `pkg-config --libs --cflags opencv4` -o result

# 执行可执行文件
./result

输出以下内容,即表明配置成功

OpenCV version : 4.2.0
Major version : 4
Minor version : 2
Subminor version : 0