【发布时间】:2013-02-09 17:56:21
【问题描述】:
我需要从给定图像中裁剪出一张脸/多张脸,并将裁剪后的人脸图像用于其他用途。我正在使用 CoreImage 的 CIDetectorTypeFace。问题是只包含检测到的面部的新 UIImage 需要更大的尺寸,因为头发被剪掉或下颚被剪掉。如何增加initWithFrame:faceFeature.bounds 的大小?
我正在使用的示例代码:
CIImage* image = [CIImage imageWithCGImage:staticBG.image.CGImage];
CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];
NSArray* features = [detector featuresInImage:image];
for(CIFaceFeature* faceFeature in features)
{
UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
faceView.layer.borderWidth = 1;
faceView.layer.borderColor = [[UIColor redColor] CGColor];
[staticBG addSubview:faceView];
// cropping the face
CGImageRef imageRef = CGImageCreateWithImageInRect([staticBG.image CGImage], faceFeature.bounds);
[resultView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
}
注意:我为显示检测到的面部区域而制作的红框与裁剪后的图像完全不匹配。也许我没有正确显示框架,但由于我不需要显示框架,我真的需要裁剪的脸,我不太担心。
【问题讨论】:
标签: objective-c crop face-detection core-image