gpt4 book ai didi

c# - 使用 Microsoft Kinect 跟踪 Blob

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:16 24 4
gpt4 key购买 nike

我正在制作人员计数器。为此,我在门上安装了 Microsoft Kinect。我正在使用 C# 和 EmguCV。我提取了人物的头部,使它们在黑色图像上显示为白色 Blob 。然后我在头部周围创建了一个边界框。那很好用。所以我现在每帧有多少 Blob ,现在我也知道它们的位置。这很好用。但现在我想跟踪这些 Blob ,因为我想计算有多少人进出,但我不知道该怎么做。谁能帮我?问题是每一帧都会出现新的 Blob ,而旧的 Blob 可能会消失。谁能给我一个算法或者一些代码?或一张纸。非常感谢!


当然。这是 blob 的代码:

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;
}

}
}

最佳答案

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

     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();

希望这对您有所帮助!

编辑

我认为您应该每十帧左右(大约每秒 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

听起来你只是想识别 Blob ,听起来你想要 McvBlob.ID .这是 blob 的 ID,您可以检查哪些 ID 仍然存在,哪些不存在。我仍然会每十帧执行一次,以免减慢速度。您只需要一个可以观察 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();

关于c# - 使用 Microsoft Kinect 跟踪 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14206699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com