码迷,mamicode.com
首页 > 其他好文 > 详细

识别出脸部以及给脸部打马赛克

时间:2015-06-01 22:29:24      阅读:417      评论:0      收藏:0      [点我收藏+]

标签:

识别出脸部以及给脸部打马赛克

技术分享

 

原始图片 & 打马后的图片

技术分享 技术分享

 

说明

1. 通过CIDetector来识别出脸部获取相关数据

2. 将CoreImage坐标转换为UIKit坐标

3. 将脸部区域进行截图

4. 将截图进行打马处理(各位喜闻乐见^_^)

 

核心源码

https://github.com/YouXianMing/FaceDetection

//
//  UIImageView+FaceDetect.h
//  FaceDetectionExample
//
//  Created by YouXianMing on 15/5/29.
//  Copyright (c) 2015年 JID Marketing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIImageView (FaceDetect)

- (NSArray *)faceRects;

@end
//
//  UIImageView+FaceDetect.m
//  FaceDetectionExample
//
//  Created by YouXianMing on 15/5/29.
//  Copyright (c) 2015年 JID Marketing. All rights reserved.
//

#import "UIImageView+FaceDetect.h"

@implementation UIImageView (FaceDetect)

- (NSArray *)faceRects {
    
    if (self.image == nil) {
        return nil;
    }
    
    // 初始化数组
    NSMutableArray *faceRectsArray = [NSMutableArray array];
    
    // 获取CIImage
    CIImage* image                 = [CIImage imageWithCGImage:self.image.CGImage];
    
    // 创建脸部识别器
    CIDetector* detector           = [CIDetector detectorOfType:CIDetectorTypeFace
                                                        context:nil
                                                        options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh
                                                                                            forKey:CIDetectorAccuracy]];
    
    // 获取侦测到的数据
    NSArray* features              = [detector featuresInImage:image];
    
    
    // CoreImage坐标转换为UIKit坐标
    CGAffineTransform transform    = CGAffineTransformMakeScale(1, -1);
    transform                      = CGAffineTransformTranslate(transform, 0, -self.bounds.size.height);
    
    // 处理
    for(CIFaceFeature* faceFeature in features)
    {
        // 转换坐标系统
        const CGRect faceRect = CGRectApplyAffineTransform(faceFeature.bounds, transform);
        
        // 添加rect
        [faceRectsArray addObject:[NSValue valueWithCGRect:faceRect]];
    }
    
    return faceRectsArray;
}

@end
//
//  ViewController.m
//  CoreImageMask
//
//  Created by YouXianMing on 15/5/29.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+FaceDetect.h"
#import "CIFilter+Utility.h"
#import "UIImage+Crop.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed:@"demo.png"];
    
    
    // 加载ImageView
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.center       = self.view.center;
    [self.view addSubview:imageView];
    
    
    NSArray *faceRectArray = [imageView faceRects];
    for (int i = 0; i < faceRectArray.count; i++) {
    
        // 获取脸部的rect
        NSValue *value  = faceRectArray[i];
        CGRect faceRect = value.CGRectValue;
        
        // 截取脸部的图片
        UIImage *faceImage = [image cropAtRect:faceRect];
        
        
        // 添加脸部的图片
        UIImageView *faceImageView = [[UIImageView alloc] initWithImage:[self CIPixellateImage:faceImage]];
        faceImageView.frame        = faceRect;
        [imageView addSubview:faceImageView];
    }
}

- (UIImage *)CIPixellateImage:(UIImage *)image {
    
    CIImage *ciImage  = [[CIImage alloc] initWithImage:image];
    CIFilter *filter  = [CIFilter filterWithName:@"CIPixellate"];
    filter.inputImage = ciImage;
    filter.inputScale = @(5.f);
    
    return [filter outputUIImage];
}


@end

技术分享

识别出脸部以及给脸部打马赛克

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/4543188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!