Pregunta

Estoy usando el código disponible en este sitio web: http://nashruddin.com/opencv_face_detection para hacer detección de cara.

Me gustaría aumentar el tamaño de la región de la cara detectada. No estoy seguro de cómo hacerlo. Necesito ayuda en él.

El código que estoy usando es este: //

#include "stdafx.h"

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

CvHaarClassifierCascade *cascade;
CvMemStorage            *storage;

void detectFaces( IplImage *img );

int main( int argc, char** argv )
{
    CvCapture *capture;
    IplImage  *frame;
    int       key;
    char      *filename = "C:/OpenCV2.1/data/haarcascades/haarcascade_frontalface_alt.xml";

    cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );
    storage = cvCreateMemStorage( 0 );
    capture = cvCaptureFromCAM( 0 );

    assert( cascade && storage && capture );

    cvNamedWindow( "video", 1 );

    while( key != 'q' ) {
        frame = cvQueryFrame( capture );

        if( !frame ) {
            fprintf( stderr, "Cannot query frame!\n" );
            break;
        }

        cvFlip( frame, frame, -1 );
        frame->origin = 0;

        detectFaces( frame );

        key = cvWaitKey( 10 );
    }

    cvReleaseCapture( &capture );
    cvDestroyWindow( "video" );
    cvReleaseHaarClassifierCascade( &cascade );
    cvReleaseMemStorage( &storage );

    return 0;
}

void detectFaces( IplImage *img )
{
    int i;

    CvSeq *faces = cvHaarDetectObjects(
            img,
            cascade,
            storage,
            1.1,
            3,
            0 /*CV_HAAR_DO_CANNY_PRUNNING*/,
            cvSize( 40, 40 ) );

    for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
        CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
        cvRectangle( img,
                     cvPoint( r->x, r->y ),
                     cvPoint( r->x + r->width, r->y + r->height ),
                     CV_RGB( 255, 0, 0 ), 1, 8, 0 );
    }

    cvShowImage( "video", img );
}
¿Fue útil?

Solución

Esto aumenta el tamaño del rectángulo alrededor de la cara. Si quiso decir aumentar el tamaño de la ventana del detector de Haar, actualice su pregunta.

int padding_width = 30; // pixels
int padding_height = 30; // pixels

for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
    CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );

    // Yes yes, all of this could be written much more compactly.
    // It was written like this for clarity.

    int topleft_x = r->x - (padding_width / 2);
    int topleft_y = r->y - (padding_height / 2);
    if (topleft_x < 0)
        topleft_x = 0;
    if (topleft_y < 0)
        topleft_y = 0;

    int bottomright_x = r->x + r->width + (padding_width / 2);
    int bottomright_y = r->y + r->height + (padding_height / 2);
    if (bottomright_x >= img->width)
        bottomright_x = img->width - 1;
    if (bottomright_y >= img->height)
        bottomright_y = img->height - 1;

    cvRectangle( img,
                 cvPoint(topleft_x, topleft_y),
                 cvPoint(bottomright_x, bottomright_y),
                 CV_RGB( 255, 0, 0 ), 1, 8, 0 );
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top