2014-06-07 2 views
3

내장 된 CoreImage 얼굴 검색 기능을 사용하여 감지 된 얼굴에 UIImage자를 잘라내려고합니다. 나는 얼굴을 제대로 감지 할 수있는 것 같지만 얼굴을 경계로 내 UIImage을 자르려고 할 때 아무데도 거의 맞지 않습니다.iOS - 감지 된 얼굴로 이미지 자르기 문제

-(NSArray *)facesForImage:(UIImage *)image { 
    CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage]; 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    NSDictionary *opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; 

    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:opts]; 
    NSArray *features = [detector featuresInImage:ciImage]; 

    return features; 
} 

을 ... 그리고 이미지를자를 수있는 코드는 다음과 같습니다 : 내 얼굴 인식 코드는 다음과 같습니다

-(UIImage *)imageCroppedToFaceAtIndex:(NSInteger)index forImage:(UIImage *)image { 
    NSArray *faces = [self facesForImage:image]; 
    if((index < 0) || (index >= faces.count)) { 
     DDLogError(@"Invalid face index provided"); 

     return nil; 
    } 

    CIFaceFeature *face = [faces objectAtIndex:index]; 
    CGRect faceBounds = face.bounds; 

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, faceBounds); 
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 

    return croppedImage; 
} 

내가 사용하고 그것에서 단지 1 얼굴 이미지가 테스트를 위해, 그리고 아무 문제없이 그것을 감지하는 것으로 보인다. 그러나 작물은 벗어났다. 이 코드의 문제점은 무엇입니까?

답변

3

-CGImage 변환은 UIImage 좌표 좌표 - 나는 내가 찾던 정확히 무엇을 달성하기 위해 CGAffineTransform을 사용하는 방법을 설명 this great article을 발견했다.

2

Core Geometry에서 UIImage 좌표로 얼굴 Geometry를 변환하는 코드는 까다 롭습니다. 나는 꽤 오랫동안 그것을 망쳐 놓지는 않았지만, 특히 나에게 맞는 이미지를 다룰 때 기억에 잘 맞는다는 것을 기억합니다.

Xcode 문서 검색으로 찾을 수있는 데모 응용 프로그램 "SquareCam"을 살펴 보는 것이 좋습니다. 얼굴 주위에 빨간색 사각형이 그려지기 때문에 좋은 출발점입니다.

코어 이미지에서 가져온 직사각형은 항상 사각형이며 때로는 너무 작게 만듭니다. 자르기 직사각형을 더 크고 넓게 만들어야 할 수도 있습니다. 다른 유사한 문제를 가진 사람들을위한

-1

나에게 도움이된다.

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

//Container for the face attributes 
UIView* faceContainer = [[UIView alloc] initWithFrame:facePicture.frame]; 

// flip faceContainer on y-axis to match coordinate system used by core image 
[faceContainer setTransform:CGAffineTransformMakeScale(1, -1)]; 

// create a face detector - since speed is not an issue we'll use a high accuracy 
// detector 
CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace 
              context:nil options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]]; 

// create an array containing all the detected faces from the detector 
NSArray* features = [detector featuresInImage:image]; 

    for(CIFaceFeature* faceFeature in features) 
    { 
     // get the width of the face 
     CGFloat faceWidth = faceFeature.bounds.size.width; 

     // create a UIView using the bounds of the face 
     UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds]; 


     if(faceFeature.hasLeftEyePosition) 
     { 
      // create a UIView with a size based on the width of the face 
      leftEyeView = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15, faceFeature.leftEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)]; 
     } 

     if(faceFeature.hasRightEyePosition) 
     { 
      // create a UIView with a size based on the width of the face 
      RightEyeView = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15, faceFeature.rightEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)]; 
     } 

     if(faceFeature.hasMouthPosition) 
     { 
      // create a UIView with a size based on the width of the face 
      mouth = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2, faceFeature.mouthPosition.y-faceWidth*0.2, faceWidth*0.4, faceWidth*0.4)]; 
     } 

     [view addSubview:faceContainer]; 

     CGFloat y = view.frame.size.height - (faceView.frame.origin.y+faceView.frame.size.height); 

     CGRect rect = CGRectMake(faceView.frame.origin.x, y, faceView.frame.size.width, faceView.frame.size.height); 

     CGImageRef imageRef = CGImageCreateWithImageInRect([<Original Image> CGImage],rect); 
     croppedImage = [UIImage imageWithCGImage:imageRef]; 

     CGImageRelease(imageRef); 

     //----cropped Image-------// 

     UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(faceView.frame.origin.x, y, faceView.frame.size.width, faceView.frame.size.height)]; 

     img.image = croppedImage; 
+0

이 코드에 문제가 있습니까? –