Faces detection with detectMultiScale(...)

asked 2013-09-24 06:29:46 -0600

Infinite Life gravatar image

updated 2013-09-25 03:31:48 -0600

I have code:

void detectAndWrite( 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( int i = 0; i < faces.size(); i++ )
  {
    Point pt1(faces[i].x, faces[i].y);
    Point pt2(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
    cv::rectangle(frame,  pt1, pt2, Scalar( 100, 100, 255 ));
  }
  //-- Write
  imwrite("newimage.jpg", frame);
 }

I gave the function a picture, two times. In first time it found a one object, so I saved new picture with one rectangle. In second time, it found two objects! And saved it in a file with three rectangles! But there is only two. I understood that probably some information about that picture in process saving in Cascade object(face_cascade).

So I have two questions:

  1. Why in different time it found different area of object?
  2. How cascade remember picture with he already work? And object on it, how I can operate with it?

Image in first pass with one detected object:

image description

Image in second pass with two detected objects:

image description

Full code was like this:

 String face_cascade_name = "cascade_02.xml";
 CascadeClassifier face_cascade;

     int main( int argc, const char** argv )
    {
        if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
        IplImage* image = cvLoadImage("123.jpg", 1);
        int i = 2;
        while (i) {
            detectAndWrite(image);
            --i;
        }
    }
edit retag flag offensive close merge delete

Comments

Please supply the images and results that you are retrieving.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-24 06:32:56 -0600 )edit

2 - it definitely does not "remember" anything between passes

berak gravatar imageberak ( 2013-09-24 06:47:45 -0600 )edit
1

Update for images.

Infinite Life gravatar imageInfinite Life ( 2013-09-24 06:56:53 -0600 )edit

Can you explain what you mean with a second pass? The algorithm doesn't process the image twice, nor does your code.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-24 07:09:11 -0600 )edit

This function I call twice(can call more, but other different rectangles doesn't appear) in main function, with the same image. Global variable is only face_cascade.

Infinite Life gravatar imageInfinite Life ( 2013-09-24 07:30:06 -0600 )edit
1

The please put the complete code, with repeating, sequentially or in a loop, because it can influence the fact of how it runs.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-24 07:59:19 -0600 )edit

I wrote code and understood what is mean of not writing in C++ some time =) Answer is simple, I give function a pointer on image, so memory on pointer changes first time, and then - second.. :-)) And the answer on second question I suppose simple too - because image have some changes(add rectangle on image) in first run of function, it detects different objects in second pass. ))

Infinite Life gravatar imageInfinite Life ( 2013-09-25 03:28:14 -0600 )edit
1

StevenPuttemans, thank you for help :)

Infinite Life gravatar imageInfinite Life ( 2013-09-25 03:29:32 -0600 )edit