简体   繁体   中英

face detect OpenCV + Qt + cvMat

I'm trying to migrate my old opencv face detect code from the use of an IplImage structure to the use of the Mat class from opencv.

The issue is that the code is working when there is no Qt code. Here is the code in brute C++ in codelite:

void detectFace(Mat& img)
{
    std::vector<Rect> faces;
    Mat gray;
    cvtColor(img, gray, CV_BGR2GRAY);
    equalizeHist(gray, gray);
    // Face detect
    face_cascade.detectMultiScale(gray, faces, 1.1, 2,1 | CV_HAAR_SCALE_IMAGE, Size(100,100));
for (unsigned int i = 0; i < faces.size(); i++)
{
    //face detect rectangle
    Point upperLeftFace(faces[i].x, faces[i].y);
    Point lowerRightFace(faces[i].x+faces[i].width, faces[i].y+faces[i].height);
    rectangle(/*Matrice*/img, /*Rect*/upperLeftFace, /*Rect*/lowerRightFace, /*BGR Color*/Scalar(255, 255,0), /*Line height*/1, /*line type*/8);
}

//Show window
namedWindow("Alexey Eye", CV_WINDOW_AUTOSIZE);
imshow("Alexey Eye", img);
}

And here is the code when i use qt:

void Neski::m_faceDetect()
{
    *att_CamCapture >> att_MatCamera;
    std::vector<Rect> faces;
    Mat grayMat;
    cvtColor(att_MatCamera, grayMat, CV_BGR2GRAY);
    equalizeHist(grayMat, grayMat);
    // Face detect
    face_cascade.detectMultiScale(grayMat, faces, 1.1, 2,1 | CV_HAAR_SCALE_IMAGE, Size(150,150));
    for (unsigned int i = 0; i < faces.size(); i++)
    {
        //face detect rectangle
        Point upperLeftFace(faces[i].x, faces[i].y);
        Point lowerRightFace(faces[i].x+faces[i].width, faces[i].y+faces[i].height);
        rectangle(/*Matrice*/att_MatCamera, /*Rect*/upperLeftFace, /*Rect*/lowerRightFace, /*BGR Color*/Scalar(255, 255,0), /*Line height*/1, /*line type*/8);
    }
    cvtColor(att_MatCamera, att_MatCamera, CV_BGR2RGB);
    QImage att_QImageCamera((uchar*) att_MatCamera.data, att_MatCamera.cols, att_MatCamera.rows, att_MatCamera.step, QImage::Format_RGB888);
    *att_PixImageCamera = QPixmap::fromImage(att_QImageCamera.scaled(640, 480),Qt::AutoColor);
    att_ui->lab_image->setPixmap(*att_PixImageCamera);
}

Both codes are almost the same, but i'm lost on why is there no facedetect when i launch the program. It does show me a video from the webcam but there is no facedetect rectangle.

Does anyone have any ideas?

i got it working after hours of sleep and fresh ideas. It was a silly mistake, i forgot to load the cascade used to detect the face. That's why there were no error and no rectangle.

face_cascade.load(face_cascade_name);

Just put it before the line:

face_cascade.detectMultiScale(grayMat, faces, 1.1, 2,1 | CV_HAAR.......

And it works now. But, still thank you for the great site.

Here is a screenshoot of the final result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM