如何在 OpenCV 中增加 haar 检测器的窗口大小

发布于 2025-01-06 15:37:43 字数 2002 浏览 1 评论 0原文

我正在使用此网站中提供的代码: http://nashruddin.com/OpenCV_Face_Detection 进行人脸检测。

我想增加检测到的面部区域的大小。我不知道该怎么做。需要一些帮助..

我使用的代码是这样的: //

#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 );
}

I am using the code available in this website: http://nashruddin.com/OpenCV_Face_Detection to do face detection.

I would like to increase the size of the detected face region. I am not sure how to do it. Need some help on it..

The code i am using is this:
//

#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 );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和影子一齐双人舞 2025-01-13 15:37:43

这会增加脸部周围矩形的大小。如果您的意思是增加 haar 检测器的窗口大小,请更新您的问题。

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 );
}

This increases the size of the rectangle around the face. If you meant increasing the haar detector's window size, please update your question.

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文