Ubuntu 配置 OpenCV 环境
Ubuntu 配置 OpenCV 环境

Ubuntu 配置 OpenCV 环境

安装python3、pip3和一些依赖

sudo apt install python3-dev python3-pip -y
sudo pip3 install cython setuptools numpy

安装一些依赖

sudo apt install gcc-arm* build-essential cmake git unzip pkg-config -y
sudo apt install protobuf-compiler libgflags-dev libgoogle-glog-dev -y
sudo apt install libjpeg-dev libpng-dev libtiff-dev -y
sudo apt install libavcodec-dev libavformat-dev libavutil-dev libxvidcore-dev libswscale-dev libx264-dev -y
sudo apt install liblapack-dev libxine2-dev gfortran -y
sudo apt install libpostproc-dev libswscale-dev -y
sudo apt install libmpikmeans-dev libmpikmeans1 mpi-default-dev -y
sudo apt install libgtk-3-dev -y

对于libjasper-dev, 它只在Ubuntu Xenial之前提供这里添加清华源下载

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper-dev -y
reboot 

安装带GPU的OpenCV

sudo dpkg -i <CUDA deb path>
sudo dpkg -i <CuDnn deb path>

确认是否安装

apt search cuda
sudo apt install <CUDA name>

apt search cudnn
sudo apt install <CuDnn name>

reboot
  • Gstreamer

GStreamer is a library for constructing graphs of media-handling components. The applications it supports range from simple Ogg/Vorbis playback, audio/video streaming to complex audio (mixing) and video (non-linear editing) processing.

sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev -y
  • Threading building blocks

Threading Building Blocks (Intel^®^ TBB) makes parallel performance and scalability accessible to software developers who are writing loop- and task-based applications. Build robust applications that abstract platform details and threading mechanisms while achieving performance that scales with increasing core count.

sudo apt install libtbb2 libtbb-dev -y
  • Handing medias

The v4l-utils are a series of packages for handling media devices, 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 (also known as the IIDC or DCAM Specifications).

sudo apt install v4l-utils libv4l-dev libdc1394-22-dev -y
  • BLAS

BLAS (Basic Linear Algebra Subroutines) is a set of efficient routines for most of the basic vector and matrix operations. They are widely used as the basis for other high quality linear algebra software, for example lapack and linpack.

sudo apt install libopenblas-dev libatlas-base-dev libblas-dev -y
  • QT5

Qt is a cross-platform C++ application framework. Qt’s primary feature is its rich set of widgets that provide standard GUI functionality.

sudo apt install qt5-default -y

uninstall Qt

sudo apt-get remove qt5-default
sudo apt-get remove qtcreator
sudo apt-get remove --auto-remove qtcreator
sudo apt-get purge qtcreator  
sudo apt-get purge --auto-remove qtcreator 
  • HDF5

Hierarchical Data Format (HDF) is a set of file formats (HDF4HDF5) designed to store and organize large amounts of data.

sudo apt install libhdf5-serial-dev hdf5-tools libhdf5-dev -y
  • Tesseract

Tesseract is an open source Optical Character Recognition (OCR) Engine. It can be used directly, or (for programmers) using an API to extract printed text from images. It supports a wide variety of languages.

sudo apt install libtesseract-dev tesseract-ocr -y
  • Eigen

Eigen is a versatile, fast, reliable and elegant C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms.

sudo apt install libeigen3-dev -y

下载源码

pushd /tmp
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip

解压源代码并创建build文件夹

unzip opencv.zip opencv_contrib.zip
pushd opencv-4.5.0
mkdir build
pushd build

优化编译

这里使用CMake

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.0/modules \
      -D WITH_OPENMP=ON \
      -D WITH_TBB=ON \
      -D BUILD_TBB=ON \
      -D BUILD_TIFF=ON \
      -D WITH_GSTREAMER=ON \
      -D WITH_V4L=ON \
      -D WITH_LIBV4L=ON \
      -D OPENCV_ENABLE_NONFREE=ON \
      -D INSTALL_C_EXAMPLES=OFF \
      -D INSTALL_PYTHON_EXAMPLES=OFF \
      -D BUILD_NEW_PYTHON_SUPPORT=ON \
      -D BUILD_opencv_python3=TRUE \
      -D WITH_QT=ON \
      -D BUILD_EXAMPLES=OFF \
      ..
  • Arch

While the devices has both AArch (ARMv8) CPU and software environment (64bit kernel and 64bit compiler), such as Jetson TX2, Raspberry Pi 4B with 64bit OS, there would be something different for CMake flags.

AArch32

-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \

AArch64

-D ENABLE_NEON=ON \
  • GPU

We have noticed that in most scenarios OpenCV utilizes CPU, which doesn’t always guarantee our the desired performance.

-D WITH_CUDA=ON \
-D WITH_CUBLAS=ON \
-D CUDA_FAST_MATH=ON \
-D ENABLE_FAST_MATH=ON \
-D WITH_OPENGL=ON \
  • Java API

Build OpenCV Java API requires Java runtime, make sure jdk is installed first, and then reconfigure CMake flags.

sudo apt install openjdk-11-jdk ant -y

注意:使用opencv-contrib可能需要下载外网资源包

开始编译

# 根据电脑线程自行修改 (不建议跑满线程)
make -j10

如果编译没有出现错误

sudo make install
sudo ldconfig

完结

参考链接

https://book.unbinilium.me/software/opensourced/#compile-and-test

发表评论

您的电子邮箱地址不会被公开。