I have a OpenCV program using multiple Haar Classifiers to detect multiple objects in a single window. The first object is detected and the ellipse is drawn as it should however when the two secondary objects are detected the circle is not drawn for every instance it is detected (I am outputting to the console when an object is detected).
I am specifying three classifiers like so:
String cascade_name = "frontalface.xml";
String nestcascade_name = "body.xml";
String nested_cascade_name_two = "HandCascade.xml";
I am then loading the classifiers using:
cascade_one.load( cascade_name )
cascade_two.load( nested_cascade_name )
cascade_three.load( nested_cascade_name_two )
I then create three vectors for the three objects:
std::vector<Rect> firstObject;
std::vector<Rect> secondObject;
std::vector<Rect> thirdObject;
I then use the following code to detect and draw the objects on the screen:
cascade_one.detectMultiScale( frame_gray, firstObject, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t i = 0; i < firstObject.size(); i++ ) {
Point center( firstObject[i].x + firstObject[i].width*0.5, firstObject[i].y + firstObject[i].height*0.5 );
ellipse( frame, center, Size( firstObject[i].width*0.5, firstObject[i].height*0.5), 0, 0, 360, Scalar( 0, 255, 0 ), 4, 8, 0 ); //GREEN
std::cout << " " << cascade_name << " " << timeFound() << endl;
}
Changing cascade_one
firstObject
and cascade_name
with the relevant names for each object. Why is the first object working perfectly but the second and third are outputting multiple detections despite not drawing them all on the screen?
EDIT:
Full detect and draw code:
void detectAndDisplay( Mat frame ) {
std::vector<Rect> firstObject;
std::vector<Rect> secondObject;
std::vector<Rect> thirdObject;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect object
cascade_one.detectMultiScale( frame_gray, firstObject, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t i = 0; i < firstObject.size(); i++ ) {
Point center( firstObject[i].x + firstObject[i].width*0.5, firstObject[i].y + firstObject[i].height*0.5 );
ellipse( frame, center, Size( firstObject[i].width*0.5, firstObject[i].height*0.5), 0, 0, 360, Scalar( 0, 255, 0 ), 4, 8, 0 ); //GREEN
std::cout << " " << cascade_name << " " << timeFound() << endl;
}
//-- detect second object
cascade_two.detectMultiScale( frame_gray, secondObject, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t k = 0; k < secondObject.size(); k++ ) {
Point center( secondObject[k].x + secondObject[k].x + secondObject[k].width*0.5, secondObject[k].y + secondObject[k].y + secondObject[k].height*0.5 );
int radius = cvRound( (secondObject[k].width + secondObject[k].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 ); //BLUE
std::cout << " " << nested_cascade_name << " " << timeFound() << endl;
}
//-- detect third object
cascade_three.detectMultiScale( frame_gray, thirdObject, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t j = 0; j < thirdObject.size(); j++ ) {
Point center( thirdObject[j].x + thirdObject[j].x + thirdObject[j].width*0.5, thirdObject[j].y + thirdObject[j].y + thirdObject[j].height*0.5 );
int radius = cvRound( (thirdObject[j].width + thirdObject[j].height)*0.25 );
circle( frame, center, radius, Scalar( 0, 0, 255 ), 4, 8, 0 ); //RED
std::cout << " " << nested_cascade_name_two << " " << timeFound() << endl;
}
imshow( window_name, frame );
}