用于使用OpenCV生成面部识别的唯一ID的代码

时间:2013-06-07 09:02:26

标签: opencv image-processing emgucv

我已经使用OpenCV编写了面部检测代码。我有视频文件,我根据特定的给定间隔从视频中提取图像,并在每个图像上运行面部检测。因此可能存在这样的情况:人站在摄像机前5分钟并且图像提取间隔是1分钟,因此对于接下来的5个图像,人将是相同的。那么我怎样才能知道每个图像中的人是相同还是不同?下面是面部检测的代码:

private static Rectangle[] DetectFace(Image<Bgr, Byte> image, string faceFileName)
        {           
            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
                {
                    using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
                    using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
                    {
                        Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);

                        return faceRegion;
                    }
                }
            }
            else
            {
                //Read the HaarCascade objects
                using (CascadeClassifier face = new CascadeClassifier(faceFileName))
                {

                    using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>()) //Convert it to Grayscale
                    {
                        //normalizes brightness and increases contrast of the image
                        gray._EqualizeHist();                       

                        //Detect the faces  from the gray scale image and store the locations as rectangle
                        //The first dimensional is the channel
                        //The second dimension is the index of the rectangle in the specific channel
                        Rectangle[] facesDetected = face.DetectMultiScale(
                           gray,
                           1.1,
                           10,
                           new Size(filterWidth, filterHeight),
                           Size.Empty);

                        return facesDetected;
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

您可以通过计算最后N帧的中值来构建背景图像。 然后,您可以从每个新框架中减去背景,这样您只能看到更改。 尝试粗略识别人物(例如,通过blob大小或形状)。 根据相机的帧速率,您可以计算平均时间,有人在相机前花费。

我想更精确,但我们需要一些你已经尝试过的代码......