【发布时间】:2012-03-14 06:42:47
【问题描述】:
我正在尝试编写一个例程,该例程采用 UIImage 并返回一个仅包含面部的新 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 创建一个新的矩形,有没有“正确”的方法来做到这一点?
谢谢!
【问题讨论】:
标签: ios xcode core-graphics face-detection