微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – UIImage人脸检测

我正在尝试编写一个带有UI Image的例程,并返回一个只包含face的新UIImage.这看起来非常简单,但我的大脑在绕过CoreImage和UIImage空间时遇到了问题.

这是基础知识:

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef,rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    CGImageRelease(newImageRef);
    return newImage;
}


-(UIImage *)getFaceImage:(UIImage *)picture {
  CIDetector  *detector = [CIDetector detectorOfType:CIDetectorTypeFace 
                                             context:nil 
                                             options:[NSDictionary dictionaryWithObject: CIDetectorAccuracyHigh forKey: CIDetectorAccuracy]];

  CIImage *ciImage = [CIImage imageWithCGImage: [picture CGImage]];
  NSArray *features = [detector featuresInImage:ciImage];

  // For simplicity,I'm grabbing the first one in this code sample,// and we can all pretend that the photo has one face for sure. :-)
  CIFaceFeature *faceFeature = [features objectAtIndex:0];

  return imageFromImage:picture inRect:faceFeature.bounds;
}

返回的图像来自翻转图像.我尝试使用以下方法调整faceFeature.bounds:

CGAffineTransform t = CGAffineTransformMakeScale(1.0f,-1.0f);
CGRect newRect = CGRectApplyAffineTransform(faceFeature.bounds,t);

…但是这给了我图像之外的结果.

我确定有一些简单的方法可以解决这个问题,但是如果没有计算自下而上,然后使用它作为X创建一个新的rect,是否有“正确”的方法来做到这一点?

谢谢!

解决方法

使用CIContext从图像中裁剪脸部会更容易,也不那么麻烦.像这样的东西:
CGImageRef cgImage = [_ciContext createCGImage:[CIImage imageWithCGImage:inputimage.CGImage] fromrect:faceFeature.bounds];
UIImage *croppedFace = [UIImage imageWithCGImage:cgImage];

其中inputimage是您的UIImage对象,faceFeature对象是您从[CIDetector featuresInImage:]方法获得的CIFaceFeature类型.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐