面部检测在Device中专门调整大小的图像上无法正常工作,为什么?

时间:2012-08-27 14:38:27

标签: iphone ios ipad ios-simulator face-detection

以下是我用于检测图像中的面部的代码:

- (void)detectFaces:(UIImageView *)photo
{
    CIImage *coreImage = [CIImage imageWithCGImage:photo.image.CGImage];
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace 
                                  context:nil 
                                  options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh 
                                                                      forKey:CIDetectorAccuracy]];
    NSArray* features = [detector featuresInImage:coreImage];

    for(CIFaceFeature* faceFeature in features)
    {
        NSLog(@"self.view %@",NSStringFromCGRect(self.view.frame));
        NSLog(@"self.view %@",NSStringFromCGRect(self.view.bounds));
        NSLog(@"self.vounds %@",NSStringFromCGRect(faceFeature.bounds));

        CGFloat faceWidth = faceFeature.bounds.size.width;
        UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
        faceView.layer.borderWidth = 1;
        faceView.layer.borderColor = [[UIColor redColor] CGColor];
        [self.view addSubview:faceView];

        if(faceFeature.hasLeftEyePosition)
        {

            UIView* leftEyeView = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15, faceFeature.leftEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];
            [leftEyeView setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]];
            [leftEyeView setCenter:faceFeature.leftEyePosition];
            leftEyeView.layer.cornerRadius = faceWidth*0.15;
            [self.view addSubview:leftEyeView];
        }

        if(faceFeature.hasRightEyePosition)
        {
            UIView* leftEye = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15, faceFeature.rightEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];

            [leftEye setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]];
            [leftEye setCenter:faceFeature.rightEyePosition];
            leftEye.layer.cornerRadius = faceWidth*0.15;
            [self.view addSubview:leftEye];
        }

        if(faceFeature.hasMouthPosition)
        {
            UIView* mouth = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2, faceFeature.mouthPosition.y-faceWidth*0.2, faceWidth*0.4, faceWidth*0.4)];
            [mouth setBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0.3]];
            [mouth setCenter:faceFeature.mouthPosition];
            mouth.layer.cornerRadius = faceWidth*0.2;
            [self.view addSubview:mouth];
        }
    }

}

这是我用来调整图片大小的代码:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

最后我正在调用这样的检测面部方法:

UIImageView *inputImage = [[UIImageView alloc] initWithImage:[self imageWithImage:[UIImage imageNamed:@"facedetectionpic.jpg"] scaledToSize:CGSizeMake(320, 460)]];
[self.view addSubview:inputImage];
[inputImage setTransform:CGAffineTransformMakeScale(1, -1)];
[self.view setTransform:CGAffineTransformMakeScale(1, -1)];
[self performSelectorInBackground:@selector(detectFaces:) withObject:inputImage];

它在模拟器中正常工作,但在设备中没有。任何人都可以帮我这个。 的模拟器: Simulator 设备: Device

当我将UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);中的选项更改为UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);后,它甚至开始在设备中工作。解决了这个问题。

1 个答案:

答案 0 :(得分:0)

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);中的选项更改为 UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);它会起作用。