特别是在Device中,人脸检测在调整大小的图像上无法正常工作,为什么?

问题描述:

这里是我使用的从图像中检测脸的代码:特别是在Device中,人脸检测在调整大小的图像上无法正常工作,为什么?

- (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);它开始,即使在设备工作。解决了这个问题。

+0

我相信你必须在这个上使用'convertRect'。 –

+0

即使使用convertRect后,我没有得到正确的位置。这里是我用来在屏幕上移动红色矩形的代码CGRect interactRect = [faceView convertRect:[faceView frame] toView:self.view]; [faceView setFrame:interactRect]; – Dee

更改选项UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);它会工作。