人脸检测OpenCV + Qt + cvMat

face detect OpenCV + Qt + cvMat

本文关键字:Qt cvMat OpenCV 检测      更新时间:2023-10-16

我正试图将我旧的opencv面部检测代码从使用IplImage结构迁移到使用opencv的Mat类。

问题是,代码工作时没有Qt代码。下面是c++代码:

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);
}

这里是代码,当我使用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);
}

两个代码几乎是相同的,但我失去了为什么没有facedetect当我启动程序。它确实显示了来自网络摄像头的视频,但没有人脸检测矩形。

有人有什么想法吗?

我睡了好几个小时,有了新的想法,我就能工作了。这是一个愚蠢的错误,我忘记加载用于检测人脸的级联。这就是为什么没有错误和矩形。

face_cascade.load (face_cascade_name);

放在行前:

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

它现在工作了。但是,还是谢谢你的伟大的网站。

这是最终结果的截图