我是使用OpenCV的新手。我正在做一个示例人脸检测器应用程序控制台。我正在使用haarcascade从网络摄像头检测人脸。
我做了下一个代码:
int main(int, char**)
{
CascadeClassifier face_cascade;
face_cascade.load("haarcascade_frontalface_alt2.xml");
vector<Rect> faces;
Mat frame_gray;
const double scale_factor = 1.1;
const int min_neighbours = 2;
const int flags = 0 | CV_HAAR_SCALE_IMAGE;
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
Mat frame;
for (;;)
{
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray, faces, scale_factor, min_neighbours, flags, Size(30, 30));
if (faces.size() == 0)
{
cout << "No face detected" << endl;
}
else
{
for (unsigned int i = 0; i<faces.size(); i++)
{
Point pt1(faces[i].x, faces[i].y);
Point pt2(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 1.5, 8, 0);
}
}
if (waitKey(30) >= 0) break;
}
return 0;
}
我测试了网络摄像头的速度很慢。我想这可能是通过图像的分辨率(640x480(。我想知道是否有任何方法可以保持分辨率并提高每帧之间的速度以进行检测。
谢谢!
您可以:
- 将最小脸尺寸从 Size(30, 30( 增加到 Size(50, 50((它可以提高 2-3 倍的性能(。
- 将scale_factor的值从 1.1 更改为 1.2;(它提高了 2 倍的性能(。
- 使用LBP检测器代替Haar检测器(速度快2-3倍(。
- 检查编译器选项(可能使用调试模式(。