我尝试将旧的opencv face检测代码从使用IplImage结构迁移到opencv中使用Mat类。
问题是当没有Qt代码时代码正在运行。 以下是codelite中的粗体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矩形。
有没有人有任何想法?
答案 0 :(得分:0)
face_cascade.load(face_cascade_name);
把它放在行前面:
face_cascade.detectMultiScale(grayMat, faces, 1.1, 2,1 | CV_HAAR.......
它现在有效。 但是,还是要感谢你们这个伟大的网站。