Faces detection with detectMultiScale(...)
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:
- Why in different time it found different area of object?
- 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 in second pass with two detected objects:
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;
}
}
Please supply the images and results that you are retrieving.
2 - it definitely does not "remember" anything between passes
Update for images.
Can you explain what you mean with a second pass? The algorithm doesn't process the image twice, nor does your code.
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.
The please put the complete code, with repeating, sequentially or in a loop, because it can influence the fact of how it runs.
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. ))
StevenPuttemans, thank you for help :)