我正在尝试检测图像中的面部,并尝试将检测到的面部另存为OpenCV中的图像。

下面的detectfaces功能存在一些问题。

#include "stdafx.h"

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

CvHaarClassifierCascade *cascade;
CvMemStorage            *storage;

void detectFaces( IplImage *img );

int _tmain(int argc, _TCHAR* argv[])
{
  //CvCapture *capture;
  IplImage  *img;//*out;
  int       key = 0;
  char      *filename = "C:/OpenCV2.1/data/haarcascades/haarcascade_frontalface_alt.xml";

  cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );
  storage = cvCreateMemStorage( 0 );
  img     = cvLoadImage("Yurico.png");

  assert( cascade && storage && img );

  cvNamedWindow( "video:", 1 );
  //cvNamedWindow( "video1:", 1 );
  //out = detectFaces( img );
  detectFaces( img );
  cvWaitKey( 0 );
  //cvShowImage( "video", out );
  cvDestroyWindow( "video:" );
  //cvDestroyWindow( "video1:" );
  cvReleaseImage( &img );
  cvReleaseHaarClassifierCascade( &cascade );
  cvReleaseMemStorage( &storage );

  return 0;
}

void detectFaces( IplImage *img )
{
    int i;
     CvRect *r;
    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 );
    cvSetImageROI(img, CvRect *r);

    IplImage *img2 = cvCreateImage(cvGetSize(img),
                              img->depth,
                               img->nChannels);

    cvSaveImage("Lakshmen.jpg",img2);
}

出现这样的错误:
 Error  1   error C2664: 'cvSetImageROI' : cannot convert parameter 2 from 'CvRect *' to 'CvRect'   c:\users\hp\documents\visual studio 2010\projects\facedetect\facedetect\facedetect.cpp  67  1   facedetect

想要将感兴趣的区域保存到另一个图像中。任何更正或改进都会告诉我..

最佳答案

您需要传递CvRect而不是CvRect *,因此不需要在r之前的指针(*)。
由于它已经是cvRect了,您应该这样写:

 cvSetImageROI(img, &r);

关于c - 检测面部并将检测到的面部保存在OpenCV中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9314900/

10-12 22:10