【发布时间】:2014-07-28 20:09:06
【问题描述】:
我正在尝试将UIImage 裁剪为已使用内置CoreImage 人脸检测功能检测到的人脸。我似乎能够正确检测到人脸,但是当我尝试将UIImage 裁剪到人脸边界时,它远不正确。我的人脸检测代码如下所示:
-(NSArray *)facesForImage:(UIImage *)image {
CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];
CIContext *context = [CIContext contextWithOptions:nil];
NSDictionary *opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh};
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:opts];
NSArray *features = [detector featuresInImage:ciImage];
return features;
}
...裁剪图像的代码如下所示:
-(UIImage *)imageCroppedToFaceAtIndex:(NSInteger)index forImage:(UIImage *)image {
NSArray *faces = [self facesForImage:image];
if((index < 0) || (index >= faces.count)) {
DDLogError(@"Invalid face index provided");
return nil;
}
CIFaceFeature *face = [faces objectAtIndex:index];
CGRect faceBounds = face.bounds;
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, faceBounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
return croppedImage;
}
我有一张只有一张脸的图像,我正在使用它进行测试,它似乎可以毫无问题地检测到它。但是收成很差。知道这段代码可能有什么问题吗?
【问题讨论】:
标签: ios objective-c uiimage crop face-detection