为什么没有旋转代码在OpenCV中旋转图像?

时间:2011-06-10 14:08:48

标签: iphone opencv avcapturesession avcapturedevice avcapture

我正在使用 AVCaptureStillImageOutput AVCaptureSession AVCaptureVideoPreviewLayer 拍照并在 UIImageView 中显示。它的工作正常。

但是,在UIImage中显示图像之后。我正在从显示的图像中使用OpenCV检测面部。

检测到,但它返回旋转的图像。我没有使用任何代码来旋转图像。它会自动旋转图像。

我想停止转动。

这是我的代码。

+ (UIImage *) opencvFaceDetect:(UIImage *)originalImage  {
    cvSetErrMode(CV_ErrModeParent);

    IplImage *image = [self CreateIplImageFromUIImage:originalImage];

    // Scaling down
    IplImage *small_image = cvCreateImage(cvSize(image->width/2,image->height/2), IPL_DEPTH_8U, 3);
    cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
    int scale = 2;

    // Load XML
    NSString *path = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_default" ofType:@"xml"];
    CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad([path cStringUsingEncoding:NSASCIIStringEncoding], NULL, NULL, NULL);
    CvMemStorage* storage = cvCreateMemStorage(0);

    // Detect faces and draw rectangle on them
    CvSeq* faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2f, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20), cvSize(100, 100));
    cvReleaseImage(&small_image);

    NSLog(@"found %d faces in image", faces->total);

    // Create canvas to show the results
    CGImageRef imageRef = originalImage.CGImage;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef contextRef = CGBitmapContextCreate(NULL, originalImage.size.width, originalImage.size.height, 8, originalImage.size.width * 4,
                                                    colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
    CGContextDrawImage(contextRef, CGRectMake(0, 45, originalImage.size.width, originalImage.size.height), imageRef);

    CGContextSetLineWidth(contextRef, 4);
    CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 1.0, 0.5);

    // Draw results on the iamge
    for(int i = 0; i < faces->total; i++) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        // Calc the rect of faces
        CvRect cvrect = *(CvRect*)cvGetSeqElem(faces, i);
        CGRect face_rect = CGContextConvertRectToDeviceSpace(contextRef, 
                                CGRectMake(cvrect.x * scale, cvrect.y * scale, cvrect.width * scale, cvrect.height * scale));
        CGContextStrokeRect(contextRef, face_rect);

        [pool release];
    }

    UIImage *returnImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)];
    CGContextRelease(contextRef);
    CGColorSpaceRelease(colorSpace);

    cvReleaseMemStorage(&storage);
    cvReleaseHaarClassifierCascade(&cascade);

    return returnImage;
}

这是屏幕截图

1)调用面部检测方法之前的图像 Image before face detection method called

2)调用面部检测方法后的图像 Image after face detection method called

1 个答案:

答案 0 :(得分:0)

您是否正在使用UIImageJPEGRepresentation来获取这些图像?如果是这样,那么您可能对此感兴趣:

http://blog.logichigh.com/2008/06/05/uiimage-fix/

- &GT; “UIImageJPEGRepresentation(),将UIImage转换为JPEG的最简单(唯一?)方式使用底层CGImage,并忽略你的UIImage imageOrientation,因此无论拍摄照片时相机位置如何,导出的JPEG将始终以横向或正确的模式,最后是需要旋转的图片。“

这暗示即使您不使用此功能,在使用imageOrientation时也需要考虑隐式UIImage