2014-04-16 35 views
2

我用CoreImage檢測臉。我想在臉部檢測後裁剪臉部。我用這個片段來檢測臉部:如何裁剪檢測到的臉

-(void)markFaces:(UIImageView *)facePicture{ 


CIImage* image = [CIImage imageWithCGImage:imageView.image.CGImage]; 

CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace 
              context:nil options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]]; 


NSArray* features = [detector featuresInImage:image]; 


CGAffineTransform transform = CGAffineTransformMakeScale(1, -1); 
transform = CGAffineTransformTranslate(transform, 0, -imageView.bounds.size.height); 


for(CIFaceFeature* faceFeature in features) 
{ 
    // Get the face rect: Translate CoreImage coordinates to UIKit coordinates 
    const CGRect faceRect = CGRectApplyAffineTransform(faceFeature.bounds, transform); 


    faceView = [[UIView alloc] initWithFrame:faceRect]; 
    faceView.layer.borderWidth = 1; 
    faceView.layer.borderColor = [[UIColor redColor] CGColor]; 


    UIGraphicsBeginImageContext(faceView.bounds.size); 
    [faceView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //Blur the UIImage with a CIFilter 
    CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage]; 
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"]; 
    [gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"]; 
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"]; 
    CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"]; 
    UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage]; 

    //Place the UIImage in a UIImageView 
    UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
    newView.image = endImage; 
    [self.view addSubview:newView]; 

    CGFloat faceWidth = faceFeature.bounds.size.width; 

    [imageView addSubview:faceView]; 

    // LEFT EYE 
    if(faceFeature.hasLeftEyePosition) 
    { 

     const CGPoint leftEyePos = CGPointApplyAffineTransform(faceFeature.leftEyePosition, transform); 

     UIView *leftEyeView = [[UIView alloc] initWithFrame:CGRectMake(leftEyePos.x - faceWidth*EYE_SIZE_RATE*0.5f, 
                     leftEyePos.y - faceWidth*EYE_SIZE_RATE*0.5f 
                     ,faceWidth*EYE_SIZE_RATE, 
                     faceWidth*EYE_SIZE_RATE)]; 

     NSLog(@"Left Eye X = %0.1f Y = %0.1f Width = %0.1f Height = %0.1f",leftEyePos.x - faceWidth*EYE_SIZE_RATE*0.5f, 
       leftEyePos.y - faceWidth*EYE_SIZE_RATE*0.5f,faceWidth*EYE_SIZE_RATE, 
       faceWidth*EYE_SIZE_RATE); 

     leftEyeView.backgroundColor = [[UIColor magentaColor] colorWithAlphaComponent:0.3]; 
     leftEyeView.layer.cornerRadius = faceWidth*EYE_SIZE_RATE*0.5; 


     [imageView addSubview:leftEyeView]; 
    } 


    // RIGHT EYE 
    if(faceFeature.hasRightEyePosition) 
    { 

     const CGPoint rightEyePos = CGPointApplyAffineTransform(faceFeature.rightEyePosition, transform); 


     UIView *rightEye = [[UIView alloc] initWithFrame:CGRectMake(rightEyePos.x - faceWidth*EYE_SIZE_RATE*0.5, 
                    rightEyePos.y - faceWidth*EYE_SIZE_RATE*0.5, 
                    faceWidth*EYE_SIZE_RATE, 
                    faceWidth*EYE_SIZE_RATE)]; 



     NSLog(@"Right Eye X = %0.1f Y = %0.1f Width = %0.1f Height = %0.1f",rightEyePos.x - faceWidth*EYE_SIZE_RATE*0.5f, 
       rightEyePos.y - faceWidth*EYE_SIZE_RATE*0.5f,faceWidth*EYE_SIZE_RATE, 
       faceWidth*EYE_SIZE_RATE); 

     rightEye.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.2]; 
     rightEye.layer.cornerRadius = faceWidth*EYE_SIZE_RATE*0.5; 
     [imageView addSubview:rightEye]; 
    } 


    // MOUTH 
    if(faceFeature.hasMouthPosition) 
    { 

     const CGPoint mouthPos = CGPointApplyAffineTransform(faceFeature.mouthPosition, transform); 


     UIView* mouth = [[UIView alloc] initWithFrame:CGRectMake(mouthPos.x - faceWidth*MOUTH_SIZE_RATE*0.5, 
                   mouthPos.y - faceWidth*MOUTH_SIZE_RATE*0.5, 
                   faceWidth*MOUTH_SIZE_RATE, 
                   faceWidth*MOUTH_SIZE_RATE)]; 

     NSLog(@"Mouth X = %0.1f Y = %0.1f Width = %0.1f Height = %0.1f",mouthPos.x - faceWidth*MOUTH_SIZE_RATE*0.5f, 
       mouthPos.y - faceWidth*MOUTH_SIZE_RATE*0.5f,faceWidth*MOUTH_SIZE_RATE, 
       faceWidth*MOUTH_SIZE_RATE); 


     mouth.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.3]; 
     mouth.layer.cornerRadius = faceWidth*MOUTH_SIZE_RATE*0.5; 
     [imageView addSubview:mouth]; 

    } 
} 
} 

我想要的只是裁剪臉部。

+0

您發佈了一些使用臉部檢測的代碼來執行與您想要執行的操作無關的操作。該代碼包括在圖像座標中計算圖像中每個面的矩形。您接下來的任務是調整該代碼以將該矩形中的圖像提取併成像到另一個圖像中。您應該能夠創建一個與所需輸出圖像大小相同的上下文,然後使用drawInRect將您的臉部分渲染到該圖像上下文中。嘗試一下,如果您遇到問題,請發佈您的代碼並尋求幫助。 –

回答

0

您可以使用此功能輕鬆裁剪臉部,並且測試並正常工作。

-(void)faceWithFrame:(CGRect)frame{ 
    CGRect rect = frame; 
    CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageView.image CGImage], rect); 
    UIImage *cropedImage = [UIImage imageWithCGImage:imageRef]; 
    self.cropedImg.image =cropedImage; 
} 

你只要通過臉部框架,上述功能將給予裁剪臉部圖像。