2013-05-09 45 views
1

我正在使用openCv框架檢測人臉。我阻止了自動旋轉。我正在使用這種方法來獲得面子。ios人臉檢測在橫向模式下失敗

detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(100, 100)); 

當我在縱向模式下持有iPhone的臉部檢測工作正常,但是當我將iPhone旋轉到橫向模式時臉部檢測失敗。 這種CvVideoCamera的植入

self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.imageView]; 
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront; 
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288; 
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait; 
self.videoCamera.defaultFPS = 30; 
self.videoCamera.grayscaleMode = NO; 
self.videoCamera.delegate = self; 

回答

1

最後我找到了解決方案。 需要將cv :: Mat旋轉到縱向。

這是我的代碼。

self.videoCamera是屬性,並且還向項目中添加文件「haarcascade_frontalface_alt.xml」。

//CvVideoCamera camera initialization. 

- (void)viewDidLoad 
{ 
    NSString *faceCascadePath = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_alt" ofType:@"xml"]; 

    if(!face_cascade.load([faceCascadePath UTF8String])) { 
     NSLog(@"Could not load face classifier!"); 
    } 

    self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.imageView]; 
    self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront; 
    self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288; 
    self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait; 
    self.videoCamera.defaultFPS = 30; 
    self.videoCamera.grayscaleMode = NO; 
    self.videoCamera.delegate = self; 
    [self.videoCamera start]; 
} 

//detect's the face in cv::Mat and displays rect around face. 
bool detectAndDisplay(Mat frame) 
{ 
    BOOL bFaceFound = false; 
    vector<cv::Rect> faces; 
    Mat frame_gray; 

    cvtColor(frame, frame_gray, CV_BGRA2GRAY); 
    equalizeHist(frame_gray, frame_gray); 

    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(100, 100)); 

    for(unsigned int i = 0; i < faces.size(); ++i) { 
     rectangle(frame, cv::Point(faces[i].x, faces[i].y), 
        cv::Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), 
        cv::Scalar(0,255,255)); 
     bFaceFound = true; 
    } 
    return bFaceFound; 
} 

//CvVideoCamera delegate 
- (void)processImage:(Mat&)image; 
{ 
    Mat tmpMat; 
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; 
    BOOL isInLandScapeMode = NO; 
    BOOL rotation = 1; 

    //Rotate cv::Mat to the portrait orientation 
    if(orientation == UIDeviceOrientationLandscapeRight) 
    { 
     isInLandScapeMode = YES; 
     rotation = 1; 
    } 
    else if(orientation == UIDeviceOrientationLandscapeLeft) 
    { 
     isInLandScapeMode = YES; 
     rotation = 0; 
    } 
    else if(orientation == UIDeviceOrientationPortraitUpsideDown) 
    { 
     cv::transpose(image, tmpMat); 
     cv::flip(tmpMat, image, rotation); 
     cv::transpose(image, tmpMat); 
     cv::flip(tmpMat, image, rotation); 
     cvtColor(image, image, CV_BGR2BGRA); 
     cvtColor(image, image, CV_BGR2RGB); 
    } 

    if(isInLandScapeMode) 
    { 
     cv::transpose(image, tmpMat); 
     cv::flip(tmpMat, image, rotation); 
     cvtColor(image, image, CV_BGR2BGRA); 
     cvtColor(image, image, CV_BGR2RGB); 
    } 

    detectAndDisplay(image); 

    if(isInLandScapeMode) 
    { 
     cv::transpose(image, tmpMat); 
     cv::flip(tmpMat, image, !rotation); 
     cvtColor(image, image, CV_BGR2RGB); 

    } 

    else if(orientation == UIDeviceOrientationPortraitUpsideDown) 
    { 
     cv::transpose(image, tmpMat); 
     cv::flip(tmpMat, image, !rotation); 
     cv::transpose(image, tmpMat); 
     cv::flip(tmpMat, image, !rotation); 
     cvtColor(image, image, CV_BGR2RGB); 
    } 
} 
2

快速修復 之前初始化CVVideoCamera添加這些這些行

的NSNumber *值= [NSNumber的numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@「orientation」];

self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.previewImage]; self.videoCamera.delegate = self;