在横向模式下ios面部检测失败

时间:2013-05-09 14:50:08

标签: iphone opencv

我正在使用openCv框架来检测面部。我阻止了自动旋转。我正在使用这种方法来获得面子。

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;

2 个答案:

答案 0 :(得分:2)

快速修复 在初始化CVVideoCamera之前,添加以下这些行

NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@" orientation"];

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

答案 1 :(得分: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);
    }
}