Pregunta

Estoy tomando una foto usando AvCapturestillImageOutput, Avcapturesession, AvCaptureVideoPreviewLayer y mostrarlo en UiImageView. Su trabajo correctamente.

Pero, después de la imagen de visualización en UIImage. Estoy detectando caras usando OpenCV de esa imagen mostrada.

Detecta, pero devuelve la imagen rotada. No uso ningún código para rotar la imagen. Gira automáticamente la imagen.

Quiero dejar de girar.

Aquí está mi código.

+ (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;
}

Aquí están las capturas de pantalla

1) Imagen para el método de detección de cara antes de llamarImage before face detection method called

2) Imagen para después del método de detección de cara después de llamarImage after face detection method called

¿Fue útil?

Solución

¿Está utilizando UIIMAGEJPEGREGRESENTACIÓN para sacar estas imágenes? Si es así, entonces podría estar interesado en esto:

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

-> "UiiMageJpegresentation (), la forma más fácil (¿única?) De convertir una uiImage a un JPEG utiliza la cgimage subyacente, e ignora su uiimage Imageorientation, por lo que, independientemente de la posición de la cámara, cuando se tome la imagen, el JPEG exportado siempre será orientado En el modo de paisaje o derecho, termina con imágenes que necesitan rotación ".

Esto sugiere que incluso si no está utilizando esta función, hay una implícita imageOrientation considerar al usar UIImage.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top