先来一段度娘的介绍信:

OpenCV是一个基于​​BSD​​​许可(开源)发行的跨平台​​计算机视觉​​​和​​机器学习​​​软件库,可以运行在​​Linux​​​、​​Windows​​​、​​Android​​​和​​Mac OS​​​操作系统上。
 它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了​​​图像处理​​和计算机视觉方面的很多通用算法。

OpenCV用C++语言编写,它具有C ++,​​Python​​​,​​Java​​​和​​MATLAB​​接口,并支持Windows,Linux,Android和Mac OS,OpenCV主要倾向于实时视觉应用,并在可用时利用MMX和SSE指令, 如今也提供对于C#、Ch、Ruby,GO的支持

应用领域

1、人机互动

2、物体识别

3、​​图像分割​

4、​​人脸识别​

5、动作识别

6、​​运动跟踪​

7、机器人

8、运动分析

9、机器视觉

10、结构分析

11、汽车安全驾驶


opencv安装环境:

  • OS:VirtualBox Ubuntu 18.04 64 bit

安装依赖:

sudo apt install -y build-essential cmake git pkg-config libgtk2.0-dev libopenexr-dev 
sudo apt install -y gfortran libblas-dev liblapack-dev libeigen3-dev
sudo apt install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libjasper-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libavutil-dev libavresample-dev libxvidcore-dev libx264-dev libv4l-dev
sudo apt install -y \
libx264-dev \
mesa-utils \
libgtk2.0-dev \
libxvidcore-dev \
yasm \
libxine2-dev \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
libv4l-dev \
libfaac-dev \
libmp3lame-dev \
libopencore-amrnb-dev \
libtheora-dev \
libvorbis-dev \
ffmpeg \
libeigen3-dev libeigen3-doc \
liblapack-dev \
tesseract-ocr \
tesseract-ocr-jpn \
libgflags-dev \
libleptonica-dev \
libtesseract-dev \
gphoto2 \
liblapacke-dev \
libgoogle-glog-dev \
libprotobuf-dev \
libprotoc-dev \
protobuf-compiler \
ccache \
libgphoto2-dev \
libavresample-dev \
libatlas-base-dev \
gfortran

下载源码:  ​Releases - OpenCV

Ubuntu18.04源码编译OpenCV_opencv

https://opencv.org/releases/​

Ubuntu18.04源码编译OpenCV_图像处理_02

解压源码:

Ubuntu18.04源码编译OpenCV_OpenCV_03

 编译源码:

进入opencv-4.5.4,执行 mkdir build

$cd opencv-4.5.4 && mkdir build && cd build
$cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/caozilong/Workspace/opencv/install ../

Ubuntu18.04源码编译OpenCV_图像处理_04

之后执行make -j4

Ubuntu18.04源码编译OpenCV_opencv_05

make install 安装:

Ubuntu18.04源码编译OpenCV_图像处理_06

安装目录:

Ubuntu18.04源码编译OpenCV_OpenCV_07

其实,完整的OpenCV包括两个部分:

  • opencv4.5.4
  • opencv_contrib-4.5.4

其中,必须的部分是标准库,也就是第一个opencv4.5.4,也就是上文介绍的部分。opencv_contrib-4.5.4是扩展库,包含一些更新的算法,但是可能不稳定, 因此在编译过程中opencv_contrib不是必需的,根据需求选择是否编译opencv_contrib,这种代码的组织方式有点像gstreamer.

下载opencv contrib:

​https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.4.tar.gz

Ubuntu18.04源码编译OpenCV_opencv

https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.4.tar.gz​

Ubuntu18.04源码编译OpenCV_计算机视觉_09

删除上次的构建,重新创建build目录,重新配置,并且这次重新安装在另一个目录install2,目的是和原来的安装目录作比较,看增加contrib包后,安装文件有哪些变化。

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/caozilong/Workspace/opencv/install2 -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.4/modules -D OPENCV_GENERATE_PKGCONFIG=YES ..

 配置结果:

Ubuntu18.04源码编译OpenCV_计算机视觉_10

extra package would be present like this:

Ubuntu18.04源码编译OpenCV_opencv_11

 make -j4编译:

Ubuntu18.04源码编译OpenCV_opencv_12

安装,执行make install进行安装,安装后,发现两个目录差别还是很大的,这说明contrib包中是有不少货的。 

Ubuntu18.04源码编译OpenCV_python_13

其他:

除此之外,在配置阶段还有一些选项用来对OPENCV进行配置,可以裁减掉某些不用的功能,比如下面介绍的这些选项:

-DBUILD_DOCS=ON \
-DBUILD_EXAMPLES=ON \
-DINSTALL_C_EXAMPLES=ON \
-DWITH_QT=ON \
-DWITH_GSTREAMER=ON \
-DVIDEOIO_PLUGIN_LIST=gstreamer \
-DOPENCV_ENABLE_NONFREE=ON \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=OFF \
-DWITH_LAPACK=ON \
-DWITH_EIGEN=ON \
-DWITH_OPENGL=ON \
-DWITH_CUDA=OFF \
-WITH_CUFFT=OFF \
-DWITH_CUBLAS=OFF \
-DWITH_CUDNN=OFF \
-WITH_NVCUVID=OFF

以OPENCV_GENERATE_PKGCONFIG=YES为例,由于第二次我们添加了这个选项,所以安装目录内会用opencv4.pc文件,而install目录则没有。

Ubuntu18.04源码编译OpenCV_python_14

cmake-gui compiler

previous compile method is use the pure command line in shell, but another method of use cmake-gui can be done if you prefer an IDE configuration step. but before start, you should install the cmake-gui with command 

sudo apt install cmake-qt-gui

select src dir and install dir then press the configue,select the default native tool chain. then configure success, after the configuration success, press the generate button to create the Makefile.

After this, you can goto the build dir and invoke the compile process with command :

make -j4 && make install

Ubuntu18.04源码编译OpenCV_python_15

compile result

Ubuntu18.04源码编译OpenCV_计算机视觉_16

编译为静态库:

cmake -DBUILD_SHARED_LIBS=OFF  ...

verify

g++ main.cpp -I ../install/include/opencv4/ -L ../install/lib/  -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_datasets -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_video -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_viz -lopencv_surface_matching -lopencv_text -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core

Ubuntu18.04源码编译OpenCV_OpenCV_17

Ubuntu18.04源码编译OpenCV_opencv_18

Ubuntu18.04源码编译OpenCV_python_19

Ubuntu18.04源码编译OpenCV_opencv_20

Cross Compiler opencv for ARM

Download the arm compile in this site:

​Arm GNU Toolchain | Arm GNU Toolchain Downloads – Arm Developer​

i select the 32 bit arm linux target compiler .

Ubuntu18.04源码编译OpenCV_python_21

download an uncompress to some place waiting for visit:

Ubuntu18.04源码编译OpenCV_计算机视觉_22

after set the src dir and build dir in cmake-gui, then press the configure button, select the last "Specify options for cross-compilling"

Ubuntu18.04源码编译OpenCV_OpenCV_23

then configure like this;

Ubuntu18.04源码编译OpenCV_python_24

then you can swith on/off build flags in search blank box.

Ubuntu18.04源码编译OpenCV_python_25

 finish this then configure again, after this, press generate to create the Makefile.

after configuration, then goto the build dir, input the commands:

make && make install

it is definitely arm arch library.

Ubuntu18.04源码编译OpenCV_OpenCV_26

finish the built

Ubuntu18.04源码编译OpenCV_opencv_27


结束!