I am starting with OpenCV and wanted to test some samples. The sample i use puts a rectangle around the faces in the screen. But the resulting detections are jerky and sporadic, how can I improve my code to make the detections smoother? I use haarcascade_frontalface_alt.xml.
void detectAndDisplay( Mat frame )
{
vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0, Size(40, 40) );
for( size_t i = 0; i < faces.size(); i++ )
{
Mat faceROI = frame_gray( faces[i] );
int x = faces[i].x;
int y = faces[i].y;
int h = y+faces[i].height;
int w = x+faces[i].width;
rectangle(frame,
Point (x,y),
Point (w,h),
Scalar(255,0,255),
2,
8,
0);
}
imshow( "Capture - Face detection", frame );
}
Judging by your comment, you are actually detecting faces at each frame in a video sequence and that is sporadic, and that is where you are unhappy with the results. Correct me if I’m wrong.
The clips you see on YouTube are likely tracking based on the detected face. It is very common to detect faces in the first frame of a video sequence, and then use that as seeded input into a tracking algorithm to track faces. OpenCV has many tracking algorithms, such as Mean Shift and Kalman Filter trackers, that will allow you to track the face. The result of these trackers will be much more smooth than detecting at each frame.