Why does detectMultiScale detect faces only when they are close to the centre of the frame? [closed]

asked 2014-07-11 02:16:39 -0600

bluenotebook gravatar image

updated 2014-07-11 02:25:22 -0600

I am trying a very simple program to detect faces in a webcam feed. I am noticing that the faces are detected well when my face is in the centre of the frame. Whenever I move a bit to the sides, the face detector either completely misses my face or gives no detection. Is this bias because of the way I am using the function (code appended) or is it an inherent bias in the HAAR Classifiers? Note that in either case (my face being in the approximate centre of the frame or my face being somewhere near the boundaries), my face is completely visible, i.e so side profiles/or cutting of the face.

//A live face detector Program. Takes feed from the camera and detects face in the given frame

#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
#include"opencv2/video/video.hpp"
using namespace cv;
using namespace std;



 int main(){
     cv::Mat frame;
     cv::VideoCapture cap(0);
     cv::namedWindow("Frame");
 do{
        cap >> frame;

        Rect r1,r2;
        vector<Rect> faces1,faces2;
        CascadeClassifier cascade1;
        CascadeClassifier cascade2;
        //cascade1.load("C:/opencv2.4.9/sources/data/lbpcascades/lbpcascade_frontalface.xml");
        cascade1.load("C:/opencv2.4.9/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");
        cascade2.load("C:/opencv2.4.9/sources/data/lbpcascades/lbpcascade_profileface.xml");
        cascade1.detectMultiScale(frame, faces1,1.05, 6, CV_HAAR_FIND_BIGGEST_OBJECT, Size(0, 0));
        cascade2.detectMultiScale(frame, faces2,1.05, 6, CV_HAAR_FIND_BIGGEST_OBJECT, Size(0, 0));
        if (faces1.size()!=0){
            cout << "face1 found";
            r1 = faces1[0];
        }

        if (faces2.size()!=0){
            cout << "face2 found";
            r2 = faces2[0];
        }

            rectangle(frame, Point(r1.y,r1.x), Point(r1.y+r1.height,r1.x+r1.width), Scalar(0,255,0),2, 8);
            rectangle(frame, Point(r2.y,r2.x), Point(r2.y+r2.height,r2.x+r2.width), Scalar(255,0,0),2, 8);
            imshow("Frame",frame);
        }while(waitKey(30) < 0);

 cap.release();
 return 0;
 }

Also here: http://stackoverflow.com/questions/24691701/why-does-detectmultiscale-detect-faces-only-when-they-are-close-to-the-centre-of

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by StevenPuttemans
close date 2014-07-11 03:55:22.869863

Comments

Could be helpful if you post a couple of example images

Martin Peris gravatar imageMartin Peris ( 2014-07-11 03:12:23 -0600 )edit

I am sorry. This problem is solved. The detections were good. I was displaying them incorrectly. The co-ordinates were inverted. Which also explains why detections in the centre worked well.

bluenotebook gravatar imagebluenotebook ( 2014-07-11 03:30:58 -0600 )edit

Don't worry, it happened to all of us at some point ;)

Martin Peris gravatar imageMartin Peris ( 2014-07-11 03:32:12 -0600 )edit

^It did happen because of a Point. Some user at SO mentioned that point follows a column, row format. Hence the slip. I should have confirmed.

bluenotebook gravatar imagebluenotebook ( 2014-07-11 03:43:39 -0600 )edit

Let me close this then since there wasn't a very general problem that could help others. Good to see you got it solved!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-11 03:50:46 -0600 )edit