1

这是我用来从图像中检测人脸的代码:

- (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];

它在模拟器中正常工作,但在设备中不正常。谁能帮我解决这个问题。 模拟器: 模拟器 设备: 设备

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

4

1 回答 1

0

更改选项UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);起作用。

于 2012-09-25T10:27:29.647 回答