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 に答える