我想写一个例程,需要一个UIImage并返回一个新的UIImage,只包含脸部。这看起来很简单,但是我的大脑在CoreImage和UIImage空间中遇到问题。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作为新的矩形,是否有一种“正确”的方式来做到这一点?
谢谢!
我正在研究同样的事情,你能解释你为y轴执行的计算吗?什么是'最大的面'? – 2012-08-20 13:52:08