人脸识别,并生成包含所有人脸的合适UIImage

#import <QuartzCore/QuartzCore.h>

+(UIImage*)betterFaceWithImage:(UIImage*)image showSize:(CGSize)showSize
{
    //低精度的探测:CIDetectorAccuracyLow已足够用。高精度的耗费时间。
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:nil
                                              options:@{CIDetectorAccuracyLow:CIDetectorAccuracy}];
    
    NSArray* features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
    
    CGSize imageSize = image.size;
    
    CGRect fixedRect = CGRectMake(MAXFLOAT, MAXFLOAT, 0, 0);
    CGFloat rightBorder = 0, bottomBorder = 0;
    for (CIFaceFeature *f in features){
        CGRect oneRect = f.bounds;
        oneRect.origin.y = imageSize.height - oneRect.origin.y - oneRect.size.height;
        
        fixedRect.origin.x = MIN(oneRect.origin.x, fixedRect.origin.x);
        fixedRect.origin.y = MIN(oneRect.origin.y, fixedRect.origin.y);
        
        rightBorder = MAX(oneRect.origin.x + oneRect.size.width, rightBorder);
        bottomBorder = MAX(oneRect.origin.y + oneRect.size.height, bottomBorder);
    }
    
    fixedRect.size.width = rightBorder - fixedRect.origin.x;
    fixedRect.size.height = bottomBorder - fixedRect.origin.y;
    
    CGPoint fixedCenter = CGPointMake(fixedRect.origin.x + fixedRect.size.width / 2.0,
                                      fixedRect.origin.y + fixedRect.size.height / 2.0);
    CGPoint offset = CGPointZero;
    CGSize finalSize = imageSize;
    if (imageSize.width / imageSize.height > showSize.width / showSize.height) {
        //move horizonal
        finalSize.height = showSize.height;
        finalSize.width = imageSize.width/imageSize.height * finalSize.height;
        fixedCenter.x = finalSize.width / imageSize.width * fixedCenter.x;
        fixedCenter.y = finalSize.width / imageSize.width * fixedCenter.y;
        
        offset.x = fixedCenter.x - showSize.width * 0.5;
        if (offset.x < 0) {
            offset.x = 0;
        } else if (offset.x + showSize.width > finalSize.width) {
            offset.x = finalSize.width - showSize.width;
        }
        offset.x = - offset.x;
    } else {
        //move vertical
        finalSize.width = showSize.width;
        finalSize.height = imageSize.height/imageSize.width * finalSize.width;
        fixedCenter.x = finalSize.width / imageSize.width * fixedCenter.x;
        fixedCenter.y = finalSize.width / imageSize.width * fixedCenter.y;
        
        //0.618 黄金分割
        offset.y = fixedCenter.y - showSize.height * (1-0.618);
        if (offset.y < 0) {
            offset.y = 0;
        } else if (offset.y + showSize.height > finalSize.height){
            offset.y = finalSize.height = showSize.height;
        }
        offset.y = - offset.y;
    }
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(offset.x,
                             offset.y,
                             finalSize.width,
                             finalSize.height);
    layer.contents = (id)image.CGImage;
    
    UIGraphicsBeginImageContextWithOptions(showSize, NO, 0);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    // 返回 一个基于当前图形上下文的图片
    UIImage* betterFace = UIGraphicsGetImageFromCurrentImageContext();
    // 移除栈顶 的基于当前位图的图形上下文
    UIGraphicsEndImageContext();
    
    return betterFace;
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值