一、概述
OpenCV是开源、高度优化的计算机视觉库,目前,在众多图像相关领域都会使用Opencv进行处理。Opencv有很多现成的图像处理函数可以帮助我们快速实现相关工程算法落地,并且效率和稳定性均有较好的保障。尤其近来,深度学习得到了大力的推广,在高版本的OpenCV中也集成了深度学习的相关功能,因此选择OpenCV作为研究或者部署的图像处理工具再适合不过。下面是一些opencv的一些核心功能组件:
- core(核心库). Core functionality
- imgproc(图像处理). Image Processing
- imgcodecs(图像读取和保存). Image file reading and writing
- videoio(视频输入输出). Video I/O
- highgui(高级GUI界面). High-level GUI
- video(视频分析). Video Analysis
- calib3d(相机矫正和三维重建). Camera Calibration and 3D Reconstruction
- features2d(二维特征框架). 2D Features Framework
- objdetect(物体检测). Object Detection
- dnn(深度学习). Deep Neural Network module
- ml(机器学习). Machine Learning
- flann(多维空间聚类和搜索). Clustering and Search in Multi-Dimensional Spaces
- photo(摄影成像). Computational Photography
- stitching(图像配准). Images stitching
- gapi(图形api). Graph API
本教程面向Ubuntu18.04操作系统,讲解如何在Ubuntu中安装最新的opencv4.5库,并最终在Python中实现调用。
二、安装
具体有两种安装方式。第一种就是采用预先编译好的二值包进行安装,第二种就是利用源码重新编译安装。总体来说,第一种方式安装比较简单,但是不能保证安装的opencv是最新版。第二种方式安装比较繁琐,但是可以自由选择版本,尤其是最新的opencv。下面分两种方式进行讲解。
从源码安装
首先安装git工具(提前切换阿里源或其它国内镜像源,否则下载速度会很慢):
sudo apt install git
然后从github上下载最新的opencv。可以从官网上看到最新的opencv当前版本是opencv4.4.0
开始下载:
-
cd ~/
-
git
clone https://github.com/opencv/opencv.git
如图所示:
下载完opencv以后再下载opencv_contrib
git clone https://github.com/opencv/opencv_contrib.git
将opencv_contrib文件夹移动到opencv文件夹里。然后在opencv文件夹里建立build文件夹,如下图所示:
接下来打开终端。进入build文件夹,然后安装环境依赖:
-
sudo apt-get install build-essential
-
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
接下来为了安装方便不容易出错,建议使用cmake-gui安装,安装命令如下:
sudo apt-get install cmake-gui
安装完毕打开cmake-gui,命令:
cmake-gui
在Where is the source code:选择opencv目录位置,在Where to build the binaries:选择build位置如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/4b95290cdf401442e35dec5d937d3b96.png)
选择完毕点击Configure,然后会弹出编译器选项,选择Unix Makefiles即可。
配置完毕如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/ecf9b6a4b602651942b1fad7ee782f53.png)
找到WITH_CUDA选项,后面的方框点上勾;
找到CUDA_DNN选项,后面的方框点上勾;
找到BUILD_opencv_world,后面的方框点上勾;
找到在CMAKE_BUILD_TYPE 值处输入RELEASE,其他保持不变;
找到OPENCV_ENABLE_NONFREE,在后面的方框点上勾(有的算法有专利,不点这个不能用,比如sift);
找到OPENCV_EXTRA_MODULES_PATH,选择opencv_contrib文件夹中的modules文件夹,注意是modules文件夹(我的路径是/home/qianbin/opencv/opencv_contrib/modules)。
![](https://i-blog.csdnimg.cn/blog_migrate/0def158394a416ca6233c067bdec2d87.png)
完成以上工作再点击Configure,耐心等一等,如果上面还有红色的再点击Configure,之到没有红色为止。Configure完成之后点击Generate,等一等即可。
![](https://i-blog.csdnimg.cn/blog_migrate/eaa56a86971ec6b60fc9fa7cbbe21226.png)
正常情况下,等Generate完成之后,我们可以直接在build目录下输入命令:
make
进行编译工作了。实际上这样很可能出错,我在编译了10分钟后报错了,出错的原因一般都是说缺少什么文件,缺少的文件主要是Cmake在配置时由于网络不好没下载下来。我在位于build下的CmakeDownloadLog.txt可以查看到有哪些文件下载失败。经常下载失败的文件我上传到了我的百度云,提取码:4zom。建议大家编译之前到build目录下的downloads/xfeatures2d文件夹里查看是否有如下11个文件:
![](https://i-blog.csdnimg.cn/blog_migrate/80b0162f7d726a00136b0ec43ba55502.png)
如果没有务必拷贝到build目录下的downloads/xfeatures2d文件夹里。我在安装时还有一个文件face_landmark_model.dat没有下载成功,将该文件放到build目录下的/share/opencv4/testdata/cv/face目录下,如果没有目录可自行建立。检查拷贝完毕,最好再次Configure一下,直到没有上面没有红色为止,然后再点击Generate。如果不放心可以再次检查日志。
最后在build目录下重新输入命令:
make
上述过程需要十几分钟甚至更长(取决于机器性能),编译不出错完成之后,出现下图所示界面:
![](https://i-blog.csdnimg.cn/blog_migrate/b6497bfed6cfc13efaa72401b6cc359c.png)
接下来在build目录下输入命令:
sudo make install
/usr/local/bin
/usr/local/include/opencv4
/usr/local/lib(与Python包放在了一起)
/usr/local/lib/cmake/opencv4
/usr/local/share/opencv4(与其他包放在了一起)
## ippicv
https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20191018
xfeatures2d
拷贝到build目录下的downloads/xfeatures2d文件夹里
face_landmark_model.dat
- Introduction
- OpenCV Tutorials
- OpenCV-Python Tutorials
- OpenCV.js Tutorials
- Tutorials for contrib modules
- Frequently Asked Questions
- Bibliography
- Main modules:
- core. Core functionality
- imgproc. Image Processing
- imgcodecs. Image file reading and writing
- videoio. Video I/O
- highgui. High-level GUI
- video. Video Analysis
- calib3d. Camera Calibration and 3D Reconstruction
- features2d. 2D Features Framework
- objdetect. Object Detection
- dnn. Deep Neural Network module
- ml. Machine Learning
- flann. Clustering and Search in Multi-Dimensional Spaces
- photo. Computational Photography
- stitching. Images stitching
- gapi. Graph API
- Extra modules:
- alphamat. Alpha Matting
- aruco. ArUco Marker Detection
- bgsegm. Improved Background-Foreground Segmentation Methods
- bioinspired. Biologically inspired vision models and derivated tools
- ccalib. Custom Calibration Pattern for 3D reconstruction
- cnn_3dobj. 3D object recognition and pose estimation API
- cudaarithm. Operations on Matrices
- cudabgsegm. Background Segmentation
- cudacodec. Video Encoding/Decoding
- cudafeatures2d. Feature Detection and Description
- cudafilters. Image Filtering
- cudaimgproc. Image Processing
- cudalegacy. Legacy support
- cudaobjdetect. Object Detection
- cudaoptflow. Optical Flow
- cudastereo. Stereo Correspondence
- cudawarping. Image Warping
- cudev. Device layer
- cvv. GUI for Interactive Visual Debugging of Computer Vision Programs
- datasets. Framework for working with different datasets
- dnn_objdetect. DNN used for object detection
- dnn_superres. DNN used for super resolution
- dpm. Deformable Part-based Models
- face. Face Analysis
- freetype. Drawing UTF-8 strings with freetype/harfbuzz
- fuzzy. Image processing based on fuzzy mathematics
- hdf. Hierarchical Data Format I/O routines
- hfs. Hierarchical Feature Selection for Efficient Image Segmentation
- img_hash. The module brings implementations of different image hashing algorithms.
- intensity_transform. The module brings implementations of intensity transformation algorithms to adjust image contrast.
- julia. Julia bindings for OpenCV
- line_descriptor. Binary descriptors for lines extracted from an image
- mcc. Macbeth Chart module
- optflow. Optical Flow Algorithms
- ovis. OGRE 3D Visualiser
- phase_unwrapping. Phase Unwrapping API
- plot. Plot function for Mat data
- quality. Image Quality Analysis (IQA) API
- rapid. silhouette based 3D object tracking
- reg. Image Registration
- rgbd. RGB-Depth Processing
- saliency. Saliency API
- sfm. Structure From Motion
- shape. Shape Distance and Matching
- stereo. Stereo Correspondance Algorithms
- structured_light. Structured Light API
- superres. Super Resolution
- surface_matching. Surface Matching
- text. Scene Text Detection and Recognition
- tracking. Tracking API
- videostab. Video Stabilization
- viz. 3D Visualizer
- xfeatures2d. Extra 2D Features Framework
- ximgproc. Extended Image Processing
- xobjdetect. Extended object detection
- xphoto. Additional photo processing algorithms