利用openCV实现自动抓拍,人脸识别,清晰度的验证等

1、本文主要涉及到opencv的视频帧抓拍和验证的相关问题,不包含如何集成opencv

2、主要讲解涉及到opencv中的关键类及一些常用的方法

3、着重讲解代理方法:

- (void)processImage:(cv::Mat &)image

4、集成过程中的注意事项

5、附上抓拍的小demo的下载地址

6、扩展,验证抓拍的图片中是否包含人脸

=====================================分割线==========================================

 

以下为正文

一、集成opencv需要添加的framework和静态库

二、OpenCV使用过程中的关键类及一些常用的方法

1、cap_ios.h、以下为此类的原始代码

/*  For iOS video I/O
 *  by Eduard Feicho on 29/07/12
 *  Copyright 2012. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#import <UIKit/UIKit.h>
#import <Accelerate/Accelerate.h>
#import <AVFoundation/AVFoundation.h>
#import <ImageIO/ImageIO.h>
#include "opencv2/core.hpp"

//! @addtogroup videoio_ios
//! @{

/////////////////////////////////////// CvAbstractCamera /////////////////////////////////////

@class CvAbstractCamera;

CV_EXPORTS @interface CvAbstractCamera : NSObject
{
    UIDeviceOrientation currentDeviceOrientation;

    BOOL cameraAvailable;
}

@property (nonatomic, strong) AVCaptureSession* captureSession;
@property (nonatomic, strong) AVCaptureConnection* videoCaptureConnection;

@property (nonatomic, readonly) BOOL running;
@property (nonatomic, readonly) BOOL captureSessionLoaded;

@property (nonatomic, assign) int defaultFPS;
@property (nonatomic, readonly) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
@property (nonatomic, assign) AVCaptureDevicePosition defaultAVCaptureDevicePosition;
@property (nonatomic, assign) AVCaptureVideoOrientation defaultAVCaptureVideoOrientation;
@property (nonatomic, assign) BOOL useAVCaptureVideoPreviewLayer;
@property (nonatomic, strong) NSString *const defaultAVCaptureSessionPreset;

@property (nonatomic, assign) int imageWidth;
@property (nonatomic, assign) int imageHeight;

@property (nonatomic, strong) UIView* parentView;

- (void)start;
- (void)stop;
- (void)switchCameras;

- (id)initWithParentView:(UIView*)parent;

- (void)createCaptureOutput;
- (void)createVideoPreviewLayer;
- (void)updateOrientation;

- (void)lockFocus;
- (void)unlockFocus;
- (void)lockExposure;
- (void)unlockExposure;
- (void)lockBalance;
- (void)unlockBalance;

@end

///////////////////////////////// CvVideoCamera ///////////////////////////////////////////

@class CvVideoCamera;

CV_EXPORTS @protocol CvVideoCameraDelegate <NSObject>

#ifdef __cplusplus
// delegate method for processing image frames
- (void)processImage:(cv::Mat&)image;
#endif

@end

CV_EXPORTS @interface CvVideoCamera : CvAbstractCamera<AVCaptureVideoDataOutputSampleBufferDelegate>
{
    AVCaptureVideoDataOutput *videoDataOutput;

    dispatch_queue_t videoDataOutputQueue;
    CALayer *customPreviewLayer;

    CMTime lastSampleTime;

}

@property (nonatomic, weak) id<CvVideoCameraDelegate> delegate;
@property (nonatomic, assign) BOOL grayscaleMode;

@property (nonatomic, assign) BOOL recordVideo;
@property (nonatomic, assign) BOOL rotateVideo;
@property (nonatomic, strong) AVAssetWriterInput* recordAssetWriterInput;
@property (nonatomic, strong) AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor;
@property (nonatomic, strong) AVAssetWriter* recordAssetWriter;

- (void)adjustLayoutToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (void)layoutPreviewLayer;
- (void)saveVideo;
- (NSURL *)videoFileURL;
- (NSString *)videoFileString;

@end

///////////////////////////////// CvPhotoCamera ///////////////////////////////////////////

@class CvPhotoCamera;

CV_EXPORTS @protocol CvPhotoCameraDelegate <NSObject>

- (void)photoCamera:(CvPhotoCamera*)photoCamera capturedImage:(UIImage *)image;
- (void)photoCameraCancel:(CvPhotoCamera*)photoCamera;

@end

CV_EXPORTS @interface CvPhotoCamera : CvAbstractCamera
{
    AVCaptureStillImageOutput *stillImageOutput;
}

@property (nonatomic, weak) id<CvPhotoCameraDelegate> delegate;

- (void)takePicture;

@end

//! @} videoio_ios

  

以上方法从名知意,且命名简洁明了,无需过多的注释说明 ,此为值得我等ITboy学习和观摩的地方

2、关键方法说明

此处不对CvPhotoCamera做说明,主要针对 CvVideoCameraDelegate 的代理方法进行说明

- (void)processImage:(cv::Mat&)image;

此方法视频帧的抓取代理,其中的image对象为非正常的RGB对象,为一个灰度对象,在使用过程中,需要进行色值的转换

- (void)processImage:(cv::Mat &)image
{
    cv::Mat outCopyImg;
    image.copyTo(outCopyImg);
    cv::cvtColor(outCopyImg, outCopyImg, CV_BGR2RGB);
    //此处说明:cv::cvtColor为颜色转换方法,最后一个参数即为我们常用的RGB色值
    if ([self whetherTheImageBlurry:image]) {     //此为一个清晰度的验证,也是来自于网上的摘录,下方会贴出代码
        [self.videoCamera stop];
        keepMatImg = outCopyImg;

        if (isNeedToCut == YES) {
            CGFloat mianW = UIScreen.mainScreen.bounds.size.width;
            CGFloat  NH = mianW * 1920 / 1080;
            cv::Rect rect(0,(1920 - NH)/2,1080,NH);
            cv::Mat image_roi = outCopyImg(rect);
            self.keepImageAlive = MatToUIImage(image_roi);       //说明:网上有很多将cv::Mat类型的数据转换为UIimage的方法 ,但是OpenCV本身就提供了此方法 MatToUIImage(),所以此处不再引用其他方法
        }else{
            self.keepImageAlive = MatToUIImage(outCopyImg);
        }

        NSLog(@"keepImageAlive.size = %@",NSStringFromCGSize(self.keepImageAlive.size));
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self.keepImageAlive) {
                self.fuzzyText.text = @"清晰";
                self.resultImageView.image = self.keepImageAlive;
                self.resultImageView.hidden = NO;
            }
        });
    }else{
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.fuzzyText.text = @"模糊";
        });
    }

}

3、清晰度的验证的方法

- (BOOL)whetherTheImageBlurry:(cv::Mat)mat{

    unsigned char *data;
    int height,width,step;

    int Iij;

    double Iave = 0, Idelta = 0;

//    cv::Mat mat = [OpenCVExtension cvMatFromUIImage:image];

    if(!mat.empty()){
        cv::Mat gray;
        cv::Mat outGray;
        // 将图像转换为灰度显示
        cv::cvtColor(mat,gray,CV_RGB2GRAY);

        cv::Laplacian(gray, outGray, gray.depth());

        //        cv::convertScaleAbs( outGray, outGray );

        IplImage ipl_image(outGray);

        data   = (uchar*)ipl_image.imageData;
        height = ipl_image.height;
        width  = ipl_image.width;
        step   = ipl_image.widthStep;

        for(int i=0;i<height;i++)
        {
            for(int j=0;j<width;j++)
            {
                Iij    = (int) data
                [i*width+j];
                Idelta    = Idelta + (Iij-Iave)*(Iij-Iave);
            }
        }
        Idelta = Idelta/(width*height);
        std::cout<<"矩阵方差为:"<<Idelta<<std::endl;
    }

    return (Idelta > IdeltaCount) ? YES : NO;
}

  

demo下载地址:https://[email protected]/tianlin106/OpencvAutoTakeImage.git

三、人脸识别的扩展- (void)processImage:(cv::Mat &)image

{
    cv::Mat outCopyImg;
    image.copyTo(outCopyImg);
    cv::cvtColor(outCopyImg, outCopyImg, CV_BGR2RGB);

    if ([self isPhotoContainsFeature:MatToUIImage(outCopyImg)]) {
        if ([self isPhotoIsBrightness:image] == YES) {
            [self disposeCamare];
            keepMatImg = outCopyImg;
            UIImage * resultImage = MatToUIImage(outCopyImg);

            //需要上传
            [self uploadImage:resultImage];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.imageView removeFromSuperview];
            });
        }
    }
}

- (BOOL)isPhotoContainsFeature:(UIImage *)image{
    CIContext * context = [CIContext contextWithOptions:nil];

    NSDictionary * param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];

    CIDetector * faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:param];
    //此类为Core Image Framework 中的类 ,主要用于识别某些外貌特性,以下语言为其API的描述  //An image processor that identifies notable features (such as faces and barcodes) in a still image or video.
    CIImage * ciimage = [CIImage imageWithCGImage:image.CGImage];

    NSArray * detectResult = [faceDetector featuresInImage:ciimage];

    return detectResult.count;
}

//此方法计算图像的亮度是否符合要求
- (BOOL)isPhotoIsBrightness:(cv::Mat &)image
{

    cv::Mat imageSobel;
    Sobel(image, imageSobel, CV_16U, 1, 1);

    //图像的平均灰度
    double meanValue = 0.0;
    meanValue = mean(imageSobel)[0];

    if (meanValue > 1.3) {
        return YES;
    }
    return NO;
}

  

四:集成主要事项:

1、导入OpenCV类目的文件的控制器必须为.mm的C++混编的文件

2、在方法命名和定义形参时,尽量避免使用关键字开头或直接使用关键字,由于OC对此项的检查不是很严格,一旦包含C++的文件以后,对关键字的检测会很强,此为需要注意的事项

原文地址:https://www.cnblogs.com/tianlin106/p/9076465.html

时间: 2024-10-01 08:24:25

利用openCV实现自动抓拍,人脸识别,清晰度的验证等的相关文章

Activity Recognition from Silhouettes using Linear Systems and Model (In)validation Techniques 利用线性系统对人体轮廓行为识别及其建模验证

Duanxx的论文阅读: Activity Recognition from Silhouettes using Linear Systems and Model (In)validation Techniques 利用线性系统 对人体轮廓行为识别 及其建模验证 ? ? ? ? ????????????????????????????????????????????????????????????????--2015-04-27 ?????????????????????????????????

利用face_recognition,dlib与OpenCV调用摄像头进行人脸识别

用已经搭建好 face_recognition,dlib 环境来进行人脸识别 未搭建好环境请参考:https://www.cnblogs.com/guihua-pingting/p/12201077.html 使用OpenCV 调用摄像头 import face_recognition import cv2 video_capture = cv2.VideoCapture(0) # VideoCapture打开摄像头,0为笔记本内置摄像头,1为外USB摄像头,或写入视频路径 mayun_img

OpenCV实践之路——人脸识别之一数据收集和预处理

本文由@星沉阁冰不语出品,转载请注明作者和出处. 文章链接:http://blog.csdn.net/xingchenbingbuyu/article/details/51386949 微博:http://weibo.com/xingchenbing  前段时间对人脸检测和人脸标记进行了一些尝试:人脸检测(C++/Python)和用Dlib库进行人脸检测和人脸标记.但是检测和识别是不同的,检测解决的问题是图片中有没有人脸:而识别解决的问题是,如果一张图片中有人脸,这是谁的脸.人脸检测可以利用op

opencv+python3.4的人脸识别----2017-7-19

opencv3.1  +  python3.4 第一回合(抄代码,可实现):人脸识别涉及一个级联表,目前能力还无法理解. 流程:1.读取图像---2.转换为灰度图---3.创建级联表---4.对灰度图使用级联表方法过滤---5.迭代得到的结果依次标记出来---6.保存图像 完整代码: import cv2 #读取图片 img = cv2.imread('5.jpg') #转化为灰度图 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #特征级联表 face_

ios OpenCv的配置和人脸识别技术

作为一个好奇心非常重的人,面对未知的世界都想去一探到底. 于是做了个人脸识别的demo. 眼下国内的关于opencv技术文章非常少.都是互相抄袭.关键是抄个一小部分还不全.时间又是非常久之前的了,和如今的一些东西对不上. 没事,我是个实在人,啥也不多说,直接上開始.期间參考了国内很多opencv的文章,代码部分參考http://m.blog.csdn.net/blog/u013810454/27868973.大家能够查看.只是他那个项目下载下来有问题. 我这个融合了全部的长处,更加全面.从配置到

java+opencv+intellij idea实现人脸识别

首先当然是需要安装opencv了,我用的是opencv2.4.13.下载完之后就可以直接安装了,安装过程也很简单,直接下一步下一步就好,我就不上图了. 接下来在opencv下找到jar包,比如我直接安装在c盘,我的jar包在C:\opencv\build\java中. 然后将jar包拷贝到lib目录中,并且在idea中配置 接着在opencv的路径下找到lbpcascade_frontalface.xml.比如我的就是C:\opencv\sources\data\lbpcascades.然后将其

python中使用Opencv进行人脸识别

上一节讲到人脸检测,现在讲一下人脸识别.具体是通过程序采集图像并进行训练,并且基于这些训练的图像对人脸进行动态识别. 人脸识别前所需要的人脸库可以通过两种方式获得:1.自己从视频获取图像   2.从人脸数据库免费获得可用人脸图像,如ORL人脸库(包含40个人每人10张人脸,总共400张人脸),ORL人脸库中的每一张图像大小为92x112.若要对这些样本进行人脸识别必须要在包含人脸的样本图像上进行人脸识别.这里提供自己准备图像识别出自己的方法. 1.采集人脸信息:通过摄像头采集人脸信息,10张以上

Dlib+OpenCV深度学习人脸识别

目录(?)[+] DlibOpenCV深度学习人脸识别 前言 人脸数据库导入 人脸检测 人脸识别 异常处理 Dlib+OpenCV深度学习人脸识别 前言 人脸识别在LWF(Labeled Faces in the Wild)数据集上人脸识别率现在已经99.7%以上,这个识别率确实非常高了,但是真实的环境中的准确率有多少呢?我没有这方面的数据,但是可以确信的是真实环境中的识别率并没有那么乐观.现在虽然有一些商业应用如员工人脸识别管理系统.海关身份验证系统.甚至是银行人脸识别功能,但是我们可以仔细想

python opencv 人脸识别初识

甲.希望通过python加opencv搭建自己的人脸识别判断库 参照如下网页,配置python opencv开发环境, {博主ma6174} http://www.cnblogs.com/ma6174/archive/2013/03/31/2991315.html ubuntu中已有python-opencv的库,安装方便 sudo apt-get install libopencv-* sudo apt-get install python-opencv sudo apt-get instal