Ask Your Question

detectMultiScale returns incorrect face values

asked 2017-08-08 08:21:47 -0500


I am trying to detect faces from one image usinf face_cascade.detectMultiScale, but it returns a faces vector with 258235440 size. I'm using opencv 3.1.0 x86 version. Exploring the vector I can see that some values have x and y coordinates with negative values. The image actually does not conatins faces. The image is 1920x1080 24 bits rgb. The xml used is haarcascade_frontalface_alt.xml contained in data folder in opencv3.1.0 code.

This is my code:

    bool Util_Faces::detectFace(const cv::Mat &img, cv::Rect &rect)

        std::vector<cv::Rect> faces;
        float ratio = 5.0;
        float invertRatio = 1 / ratio;

        int newCols = (int)(img.cols*invertRatio);
        int newRows = (int)(img.rows*invertRatio);

        //Scale image to 1/5 factor. (Image is 1920x1080 24rgb
        cv::Mat imgGray;
        cv::resize(img, imgGray, cv::Size(newCols, newRows));

        cv::cvtColor(imgGray, imgGray, cv::COLOR_BGR2GRAY);

        //-- Detect faces
        Util_Faces::face_cascade.detectMultiScale(imgGray, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE);

        if (faces.size() == 0)
            return false;

        cv::Rect ajustRect = faces[0];
        rect = cv::Rect(ajustRect.x*ratio, ajustRect.y*ratio, ajustRect.width*ratio, ajustRect.height*ratio);

        return true;
    catch (std::exception  ex)
        int a = 0;
        return false;


Anyone knows why am I receiving this values?


edit retag flag offensive close merge delete


" I'm using opencv 3.1.0 x86 " -- os ? bits ? compiler ?

i'm almost sure, that this is related to your other question (a linkage problem)

berak gravatar imageberak ( 2017-08-08 09:06:09 -0500 )edit

I'm using it in a 64 bits system, Windows 10 Enterprise 64 bits. OpenCV and my solution are compiled with Visual Studio 2015 x86 (Win32). The thing in this case is that It doesn't throw any error. It throws one when I try to create Rectangle with negative coordenates.

tralladas gravatar imagetralladas ( 2017-08-08 09:40:29 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-08-09 01:50:49 -0500

updated 2017-08-09 01:53:07 -0500


Finally It works. I created empty project again and linked opencv againg and It worked. So as you said berak, the problem could be linkage. What I see now is that I receive a lot of false positives (it detect faces where it shouldn't, for example if there is two lights more or less aligned.

Thanks, for you responses.

edit flag offensive delete link more


ah, good. try the same with your lbph program.

to reduce false positives, you could try to increase the "minNeighbours" param in detectMultiScale from 2 (currently) to 3-5

berak gravatar imageberak ( 2017-08-09 02:10:28 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-08-08 08:21:47 -0500

Seen: 117 times

Last updated: Aug 08 '17