先来一段度娘的介绍信:
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 - OpenCVhttps://opencv.org/releases/
解压源码:
编译源码:
进入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 ../
之后执行make -j4
make install 安装:
安装目录:
其实,完整的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.gzhttps://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.4.tar.gz
删除上次的构建,重新创建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 ..
配置结果:
extra package would be present like this:
make -j4编译:
安装,执行make install进行安装,安装后,发现两个目录差别还是很大的,这说明contrib包中是有不少货的。
其他:
除此之外,在配置阶段还有一些选项用来对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目录则没有。
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 :
compile result
编译为静态库:
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
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 .
download an uncompress to some place waiting for visit:
after set the src dir and build dir in cmake-gui, then press the configure button, select the last "Specify options for cross-compilling"
then configure like this;
then you can swith on/off build flags in search blank box.
finish this then configure again, after this, press generate to create the Makefile.
after configuration, then goto the build dir, input the commands:
it is definitely arm arch library.
finish the built
结束!