如何在人脸检测中使用detectMultiScale检测器进行ROI。请帮助我使用此代码

时间:2015-12-07 08:46:04

标签: java android eclipse face-detection opencv3.0

我正在使用这个面部检测的开放式cv 3.0示例项目。

我正在为我的Android应用程序编写代码。我已经改变了面部口腔检测的代码。

在检测到脸部后,我从FACE获得了MOUTH区域。但是当我把“moutharea”这个论点传递给“detectmultiscale”时

它向我显示此错误“CascadeClassifier类型中的方法detectMultiScale(Mat,MatOfRect,double,int,int,Size,Size)不适用于参数(Rect,MatOfRect,double,int,int,Size ,大小)“

Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++)

{

    Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);

    Rect r = facesArray[i]; ////// for each detected face
    Rect moutharea = new Rect (r.x , r.y+(r.height * 2/3), r.width,r.height/3 ); ////// for extracting lower portion of mouth

    MatOfRect mouth = new MatOfRect();

    if (mDetectorType == JAVA_DETECTOR) {
        if (mJavaDetectorMouth != null)
            mJavaDetectorMouth.detectMultiScale(motharea, mouth, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                    new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
    }
    else if (mDetectorType == NATIVE_DETECTOR) {
        if (mNativeDetector != null)
            mNativeDetector.detect(mGray, mouth);
    }
    else {
        Log.e(TAG, "Detection method is not selected!");
    }

    Rect[] mouthArray = mouth.toArray();

    for (int j = 0; j < mouthArray.length; j++){
    Imgproc.rectangle(mRgba, mouthArray[j].tl(), mouthArray[j].br(),new Scalar(255, 0, 0, 255), 2);
    }

   Imgproc.rectangle(mRgba, moutharea.tl(), moutharea.br(),new Scalar(255, 0, 0, 255), 2);

}

return mRgba;
}

2 个答案:

答案 0 :(得分:0)

我也在这个链接cascade classifier Object detection中搜索我找到的帮助。这正是你想要的。您可以将multiScaleDetector用于检测到的面部。但我不知道如何在android中实现它。

 #include "opencv2/objdetect/objdetect.hpp"
 #include "opencv2/highgui/highgui.hpp"
 #include "opencv2/imgproc/imgproc.hpp"

 #include <iostream>
 #include <stdio.h>

    using namespace std;
 using namespace cv;

   /** Function Headers */
   void detectAndDisplay( Mat frame );
  /** Global variables */
  String face_cascade_name = "haarcascade_frontalface_alt.xml";
     String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
     CascadeClassifier face_cascade;
     CascadeClassifier eyes_cascade;
    string window_name = "Capture - Face detection";
     RNG rng(12345);

     /** @function main */
       int main( int argc, const char** argv )
     {
     CvCapture* capture;
        Mat frame;

        //-- 1. Load the cascades
         if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error                      
        loading\n"); return -1; };
       if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error               
            loading\n"); return -1; };

               //-- 2. Read the video stream
         capture = cvCaptureFromCAM( -1 );
             if( capture )
             {
 while( true )
 {
       frame = cvQueryFrame( capture );

          //-- 3. Apply the classifier to the frame
          if( !frame.empty() )
        { detectAndDisplay( frame ); }
     else
          { printf(" --(!) No captured frame -- Break!"); break; }

            int c = waitKey(10);
               if( (char)c == 'c' ) { break; }
             }
           }
        return 0;
               }

    /** @function detectAndDisplay */
     void detectAndDisplay( Mat frame )
      {
     std::vector<Rect> faces;
       Mat frame_gray;

    cvtColor( frame, frame_gray, CV_BGR2GRAY );
       equalizeHist( frame_gray, frame_gray );

      //-- Detect faces
        face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2,          
   0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

          for( size_t i = 0; i < faces.size(); i++ )
       {
              Point center( faces[i].x + faces[i].width*0.5, faces[i].y +       
        faces[i].height*0.5 );
    ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 
   0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

       Mat faceROI = frame_gray( faces[i] );
         std::vector<Rect> eyes;

        //-- In each face, detect eyes

     eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0         
    |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

 for( size_t j = 0; j < eyes.size(); j++ )
 {
    Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + 
 eyes[j].y + eyes[j].height*0.5 );
   int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
   circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
   }
    }
     //-- Show what you got
  imshow( window_name, frame );
   }

你需要根据java语言进行编码...可能是somone在这里可以帮助你... :)

答案 1 :(得分:0)

void detectAndDisplay( Mat frame ,int i)
{

std::vector<Rect> faces;
Mat frame_gray;

cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );

//-- Detect faces
//face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 1, 0|CV_HAAR_SCALE_IMAGE, Size(20, 20) );

for( size_t i = 0; i < faces.size(); i++ )
{
    Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
    ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

    Mat faceROI = frame_gray( faces[i] );
    std::vector<Rect> eyes;

    //-- In each face, detect eyes
    //eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    for( size_t j = 0; j < eyes.size(); j++ )
    {
        Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
        int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
        circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
    }
}
//-- Show what you got
//imshow( window_name, frame );
char filename[512];
sprintf(filename,"C:\\out\\image%d.jpg",i);
imwrite(filename,frame);

}