Question

I am using C++ and opencv. I am using the code from this post OpenCv assertion failed ,ie,with the answer. However I slightly changed the code by dividing mouthROI.width and mouthROI.height by 5. The code displays a red rectangle near the eyes region when it should have detected the mouth. Does anyone know what is wrong with the code?thanks

Was it helpful?

Solution

What I could understand from your code, is your pt1 and pt2 are not initialized correctly in mouth detection.

they must be

Point pt1(faces[i].x + mouths[i].x, faces[i].y + mouths[i].y);
Point pt2(pt1.x + mouths[i].width, pt1.y + mouths[i].height);

Moreover, I don't know where you gonna use your code, but, it looks too long for just face and mouth, I am referring to the codes which you shared in previous questions. Incase, if you arent getting the output yet, just let me know, I am free to share my old code snippet on this thing.

It gave me results.Dont forget to add xml files into project directory.

#include "stdafx.h"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\imgproc\imgproc.hpp"

using namespace cv;

int main(int argc, const char** argv)
{
  VideoCapture cap(0);  
  CascadeClassifier face, mouth;
  face.load("haarcascade_frontalface_default.xml");
  mouth.load("haarcascade_mcs_mouth.xml");
  Mat frame, grayframe, testframe;

  while(1)
  {
    cap.read(frame);
    if(!cap.read(frame))
    {
        printf("an error while taking the frame from cap");
    }
    vector< Rect > faces;
    cvtColor(frame,grayframe, CV_BGR2GRAY);
    equalizeHist(grayframe,testframe);
    face.detectMultiScale(testframe, faces,1.1,3,   CV_HAAR_SCALE_IMAGE,Size(30,30));
    for(int i=0;i<faces.size();i++)
    {
        rectangle(frame,faces[i],Scalar(255,0,0),1,8,0);
        Mat face  = frame(faces[i]);
        cvtColor(face,face,CV_BGR2GRAY);
        vector <Rect> mouthi;
        mouth.detectMultiScale(face,mouthi);
        for(int k=0;k<mouthi.size();k++)
        {
        Point pt1(mouthi[0].x+faces[i].x , mouthi[0].y+faces[i].y);
        Point pt2(pt1.x+mouthi[0].width, pt1.y+mouthi[0].height);
        rectangle(frame, pt1,pt2,Scalar(255,0,0),1,8,0);
        }

    }

    imshow("output", frame);
    waitKey(33);
  }
  return 0;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top