其他分享
首页 > 其他分享> > Android试玩OpenCV

Android试玩OpenCV

作者:互联网

OpenCV目前在移动端的主要使用场景是人脸识别、车牌识别、图像处理等,可以说其发挥作用的场景是越来越多的,本文主要介绍OpenCV在安卓端的集成方法和简单用法,方便后续进一步研究。

OpenCV下载

OpenCV官网地址:https://opencv.org/
下载地址:https://sourceforge.net/projects/opencvlibrary/files/

sourceforge国内的下载速度及其缓慢,可以考虑使用VPN进行加速,或者用迅雷等专业的下载器来提高下载速度。

这边下载的是opencv-4.5.0-android-sdk.zip,解压后可以看到如下目录结构:
在这里插入图片描述
我们可能比较关心OpenCV-android的包体积,可以看到so库大约是8.9M左右:
在这里插入图片描述
如有自定义识别规则方法的,则体积会相应增加一些,通常也在10M以内。

物体检测-objdetect

我们运行face-detection这个sample工程,运行效果如下:当相机中出现疑似人脸时会用绿色框框进行标识,支持检测多个人脸。
在这里插入图片描述

1. 级联分类器CascadeClassifier

1.1 人脸检测

/**
     * Detects objects of different sizes in the input image. The detected objects are returned as a list
     *     of rectangles.
     *
     *     @param image Matrix of the type CV_8U containing an image where objects are detected.
     *     @param objects Vector of rectangles where each rectangle contains the detected object, the
     *     rectangles may be partially outside the original image.
     *     @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
     *     @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
     *     to retain it.
     *     @param flags Parameter with the same meaning for an old cascade as in the function
     *     cvHaarDetectObjects. It is not used for a new cascade.
     *     @param minSize Minimum possible object size. Objects smaller than that are ignored.
     *     @param maxSize Maximum possible object size. Objects larger than that are ignored. If {@code maxSize == minSize} model is evaluated on single scale.
     *
     *     The function is parallelized with the TBB library.
     *
     *     <b>Note:</b>
     * <ul>
     *   <li>
     *           (Python) A face detection example using cascade classifiers can be found at
     *             opencv_source_code/samples/python/facedetect.py
     *   </li>
     * </ul>
     */
    public void detectMultiScale(Mat image, MatOfRect objects, double scaleFactor, int minNeighbors, int flags, Size minSize, Size maxSize)

opencv android sdk中提供了CascadeClassifier级联分类器进行图片目标识别,上面就是其目标识别的方法,传入的图片是OpenCV的Mat格式,且是灰度图以减少计算量加快识别速度。
从文档可以看到这是一个通用目标识别方法,而非仅限于人脸,那么如何指定识别对象呢?

sample中演示了如何获取相机帧数据进行人脸检测,那么如何检测图片的人脸呢?这里可以提供一下思路:图片转bitmap,bitmap转Mat,mat灰度后就可以进行检测了。

1.2 设置识别目标

有两种方式可以设置分类器的识别目标,分别是构造方法和load方法。

/**
 *  Loads a classifier from a file.
 *
 *  @param filename Name of the file from which the classifier is loaded.
 */
 public CascadeClassifier(String filename)
/**
 * Loads a classifier from a file.
 *
 * @param filename Name of the file from which the classifier is loaded. The file may contain an old
 *     HAAR classifier trained by the haartraining application or a new cascade classifier trained by the
 *     traincascade application.
 * @return automatically generated
 */
public boolean load(String filename)

其中filename是配置文件的绝对路径,配置文件为xml格式,sample中人脸正脸的配置文件大小约为50KB。从load方法也可以看出,配置文件可以是由 haartraining应用训练的HAAR分类器,也可以是traincascade应用训练的cascade分类器。

扩展:
如果希望利用OpenCV实现更多特定物品识别,那么可以学习在上面提到两个算法应用进行训练,得到配置文件从而实现扩展。sdk的etc目录下也默认提供了多种识别配置文件。
在这里插入图片描述

2. 自定义识别

除了OpenCV sdk提供的CascadeClassifier,我们还可以利用OpenCV提供的基础能力,实现自定义人脸识别。详见:org.opencv.samples.facedetect.DetectionBasedTracker

3. 其他识别

除了基本的物品检测,OpenCV还支持

相机定标和三维重建-calib3d

运行camera-calibration这个sample,界面如下:
在这里插入图片描述
根据介绍此模块主要用于相机标定并根据标定结果对2d图像进行一些3d处理,比如视角转换等。

图像处理-imgproc

color-blob-detection这个sample演示了利用OpenCV图像处理工具Imgproc进行颜色梯度计算和绘制,运行后效果如下:
在这里插入图片描述
image-manipulations这个sample演示了
在这里插入图片描述

1. 核心类Imgproc

提供了丰富的图像处理方法,是学习OpenCV的重中之重。资料

2D特征框架-feature2d

学习如何使用OpenCV中的特征点检测,特征点描述,以及匹配算法。资料

视频分析-video

利用OpenCV对视频进行运动提取,特征跟踪和前景提取。资料

机器学习-ml

利用机器学习进行统计分类、数据回归和数据分组。资料

参考资料

标签:配置文件,opencv,image,param,OpenCV,试玩,Android,识别
来源: https://blog.csdn.net/csfchh/article/details/120843982