用微软Kinect追踪斑点

本文关键字:斑点 追踪 Kinect 微软 | 更新日期: 2023-09-27 17:51:00

我正在做一个人员计数器。为此,我在门上安装了微软Kinect。我正在使用c#和EmguCV。我把人的头提取出来,使他们在黑色的图像上看起来像白色的斑点。然后我在头像周围创建了一个边界框。这很好。现在我知道了每帧有多少个斑点,还有它们的位置。这很好。但现在我想跟踪这些团因为我想计算进出的人数,但我不知道怎么做。有人能帮我吗?问题是,每一帧,新的斑点可能出现,旧的斑点可能消失。谁能给我一个算法或者代码?或者一篇论文。非常感谢!


确定。这是blobs的代码:

using (MemStorage stor = new MemStorage())
        {

            Contour<System.Drawing.Point> contours = head_image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL, stor);

            for (int i = 0; contours != null; contours = contours.HNext)
            {
                i++;

                //if ((contours.Area > Math.Pow(sliderMinSize.Value, 2)) && (contours.Area < Math.Pow(sliderMaxSize.Value, 2)))
                {
                    MCvBox2D box = contours.GetMinAreaRect();
                    blobCount++;
                    contour_image.Draw(box, new Bgr(System.Drawing.Color.Red), 1);

                    new_position = new System.Drawing.Point((int)(box.center.X), (int)(box.center.Y));
                    new_x = box.center.X;
                    new_y = box.center.Y;
                }
            }
        }

用微软Kinect追踪斑点

更多信息请参见Emgu CV Blob检测。假设您使用的是Emgu CV 2.1或更高版本,那么答案将有效。如果您使用的是1.5或更高版本,请参阅有关如何轻松检测blobs的帖子。或者看看下面的代码

     Capture capture = new Capture();
     ImageViewer viewer = new ImageViewer();
     BlobTrackerAutoParam param = new BlobTrackerAutoParam();
     param.ForgroundDetector = new ForgroundDetector(Emgu.CV.CvEnum.FORGROUND_DETECTOR_TYPE.FGD);
     param.FGTrainFrames = 10;
     BlobTrackerAuto tracker = new BlobTrackerAuto(param);
     Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
     {
        tracker.Process(capture.QuerySmallFrame().PyrUp());
        Image<Gray, Byte> img = tracker.GetForgroundMask();
        //viewer.Image = tracker.GetForgroundMask();
        MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0);
        foreach (MCvBlob blob in tracker)
        {
           img.Draw(Rectangle.Round(blob), new Gray(255.0), 2);
           img.Draw(blob.ID.ToString(), ref font, Point.Round(blob.Center), new Gray(255.0));
        }
        viewer.Image = img;
     });
     viewer.ShowDialog();

希望这对你有帮助!

编辑

我认为你应该每隔10帧左右(每秒3次)使用这段代码,并像这样做:

     Capture capture = new Capture();
     ImageViewer viewer = new ImageViewer();
     BlobTrackerAutoParam param = new BlobTrackerAutoParam();
     param.ForgroundDetector = new ForgroundDetector(Emgu.CV.CvEnum.FORGROUND_DETECTOR_TYPE.FGD);
     param.FGTrainFrames = 10;
     BlobTrackerAuto tracker = new BlobTrackerAuto(param);
     int frames = 0;
     Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
     {
        frames++;//Add to number of frames
        if (frames == 10)
        {
        frames = 0;//if it is after 10 frames, do processing and reset frames to 0
        tracker.Process(capture.QuerySmallFrame().PyrUp());
        Image<Gray, Byte> img = tracker.GetForgroundMask();
        //viewer.Image = tracker.GetForgroundMask();
        int blobs = 0;
        MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0);
        foreach (MCvBlob blob in tracker)
        {
           //img.Draw(Rectangle.Round(blob), new Gray(255.0), 2);
           //img.Draw(blob.ID.ToString(), ref font, Point.Round(blob.Center), new Gray(255.0));
           //Only uncomment these if you want to draw a rectangle around the blob and add text
           blobs++;//count each blob
        }
        blobs = /*your counter here*/;
        blobs = 0; //reset 
        viewer.Image = img;//get next frame
     });
     viewer.ShowDialog();

编辑2

听起来你只是想要识别blobs,听起来你想要McvBlob.ID。这是blob的ID,你可以检查哪些ID还在,哪些不在。我仍然会每隔10帧做一次这样的操作,以免减慢速度。您只需要一个简单的算法来观察ID是什么,以及它们是否发生了变化。我会将id存储在List<string>中,并每隔几帧检查该列表的变化。例子:

List<string> LastFrameIDs, CurrentFrameIDs;
         Capture capture = new Capture();
     ImageViewer viewer = new ImageViewer();
     BlobTrackerAutoParam param = new BlobTrackerAutoParam();
     param.ForgroundDetector = new ForgroundDetector(Emgu.CV.CvEnum.FORGROUND_DETECTOR_TYPE.FGD);
     param.FGTrainFrames = 10;
     BlobTrackerAuto tracker = new BlobTrackerAuto(param);
     int frames = 0;
     Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
     {
        frames++;//Add to number of frames
        if (frames == 10)
        {
        frames = 0;//if it is after 10 frames, do processing and reset frames to 0
        tracker.Process(capture.QuerySmallFrame().PyrUp());
        Image<Gray, Byte> img = tracker.GetForgroundMask();
        //viewer.Image = tracker.GetForgroundMask();
        int blobs = 0, i = 0;
        MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0);
        foreach (MCvBlob blob in tracker)
        {
           i++;
           //img.Draw(Rectangle.Round(blob), new Gray(255.0), 2);
           //img.Draw(blob.ID.ToString(), ref font, Point.Round(blob.Center), new Gray(255.0));
           //Only uncomment these if you want to draw a rectangle around the blob and add text
           CurrentFrameIDs.Add(blob.ID.ToString());
           if (CurrentFrameIDs[i] == LastFrameIDs[i])
               img.Draw(Rectangle.Round(blob), new Gray(0,0), 2);//mark the new/changed blob
           blobs++;//count each blob
        }
        blobs = /*your counter here*/;
        blobs = 0; //reset 
        i = 0;
        LastFrameIDs = CurrentFrameIDs;
        CurrentFrameIDs = null;
        viewer.Image = img;//get next frame
     });
     viewer.ShowDialog();