使用opencv进行耳朵检测

时间:2014-03-16 18:14:49

标签: opencv face-detection face-recognition haar-wavelet

我正试图检测脸部轮廓图像(侧视图)中的耳朵。我尝试使用opencv中提供的harrcascades(haarcascade_mcs_rightear,haarcascade_mcs_leftear,left_ear.xml,right_ear.xml)。我能够检测到轮廓面。但是我无法用任何一个haarcascade检测耳朵。我正在使用轮廓面检测器指定感兴趣的区域。我无法找出我出错的地方。请帮助我。同样的代码片段会非常感谢。谢谢。以下是带注释的代码。

#include <stdio.h>
#include<conio.h>
#include "cv.h"
#include "highgui.h"
using namespace std;
using namespace cv;
CvMemStorage *storage;
int detectFeature(int,char *imname,IplImage* image,CvRect featureROI, Rect* feature_box);
const char *file_profileface = "haarcascade_profileface.xml";
const char *ear_profileface = "left_ear.xml";//cascade name
CvRect profile_face;
CvRect ear;
int main()
{
int flagFaceDetect;
storage = cvCreateMemStorage(0);
assert(storage);

Rect faceRect;Rect leftEar;Rect rightEar;
char myimage1[50];

sprintf(myimage1,"profile%d.jpg",1);
IplImage* img = cvLoadImage(myimage1, CV_LOAD_IMAGE_COLOR);
/*first detect profile face and then detect ears*/

Rect* rectptr = &faceRect;//rectangle for profile face
CvRect face_roi = cvGetImageROI(img);
flagFaceDetect = detectFeature(0,myimage1,img,face_roi,rectptr);

rectptr = &leftEar;
//set ROI for ear with respect to profile face rectangle
CvRect leftear_roi = cvRect(faceRect.x+faceRect.x*3/4,faceRect.y+faceRect.height /4,faceRect.x+faceRect.width+faceRect.width/10,faceRect.y+faceRect.height-faceRect.height/4);
flagFaceDetect = detectFeature(1,myimage1,img,leftear_roi,rectptr);



getch();
return 0;
 }

int detectFeature(int feature_index,char *imname,IplImage* image,CvRect featureROI, Rect* feature_box) {//general function to locate feature
     cvSetImageROI(image, featureROI);
     CvSeq* feature;
     CvHaarClassifierCascade* featureCascade;
    if(feature_index==0)//cascade for profile face
     featureCascade = (CvHaarClassifierCascade*) cvLoad(file_profileface, 0, 0, 0);
    if(feature_index==1)//cascade for ear
        featureCascade = (CvHaarClassifierCascade*) cvLoad(ear_profileface, 0, 0, 0);
  // feature = cvHaarDetectObjects(image,featureCascade,storage,1.1,2,CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50));
    feature = cvHaarDetectObjects(image,featureCascade,storage,1.2, 3,0,cvSize(18,12));
     cvResetImageROI(image);
     IplImage* displayImage = cvLoadImage(imname, CV_LOAD_IMAGE_COLOR);
     CvRect* r;
     int index_max_area;
     int x1, x2, y1, y2; // opposite vertices of the rectangle


     if (feature->total == 0) {
         cout<<"here";
             return 0;
     }
     else {

         CvRect *fture = (CvRect*)cvGetSeqElem(feature, 0);
         feature_box->x=fture->x;
         feature_box->y=fture->y;
         feature_box->height=fture->height;
         feature_box->width=fture->width;
          /* draw a red rectangle around the feature*/
          cvRectangle(displayImage,
                                cvPoint(fture->x+fture->x*3/4,fture->y+fture->height/4),
                                cvPoint(fture->x+fture->width+fture->width/10,fture->y+fture->height-fture->height/4),
                                CV_RGB(0, 0, 255),
                                1, 8, 0
                               );



             cvShowImage("frame",displayImage);
             cvWaitKey(0);
             return 1;
     }
 }

0 个答案:

没有答案