我有一个使用 openCV 检测人脸的视频流,然后如何捕获最靠近相机的人脸并将其保存到图像文件中

I have a video stream that detects faces using openCV, how could I then capture the face closest to the camera and save it to an image file?

本文关键字:相机 图像 保存 最靠近 文件 openCV 有一个 检测 的视频 然后 何捕获      更新时间:2023-10-16

所以我希望将最突出的脸捕获为图像文件。

以下是检测代码:

void detectAndDisplay(Mat frame) 
{ 
std::vector<Rect> faces;
Mat gray;
cvtColor(frame, gray, CV_BGR2GRAY);
equalizeHist(gray,gray);
//find faces
face_cascade.detectMultiScale(gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (size_t i = 0; i < faces.size(); i++)
{
    Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
    //draw around the face
    ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255,      255, 255), 4, 8, 0);
    Mat faceROI = gray(faces[i]);
}
imshow(window_name, frame);

}

如果检测到的脸的大小足以描述其突出程度(实际上并非如此),那么,由于faces的每个元素都是一个Rect,您可以使用Rect::area()作为度量。