运动检测 [英] Motion Detection
问题描述
我真的不能让我解决这个的头,所以我希望有人能够给我一个小手^^
I really cannot get my head around this, so I hope that someone can give me a little hand ^^
我试图通过检测在C#中的运动我的摄像头。
I'm trying to detect motion in C# via my webcam.
到目前为止,我已经试过多个库(AForge库),但失败了,因为我不知道如何使用它。
So far I've tried multiple libraries (AForge Lib), but failed because I did not understand how to use it.
起初我只是想像素从最后一个当前帧进行比较,但竟然完全一样小号**吨的工作:我
At first I just wanted to compare the pixels from the current frame with the last one, but that turned out to work like utter s**t :I
现在,我的摄像头运行的事件webcam_ImageCaptured每次从网络摄像头,这就好比5-10 fps的画面。
Right now, my webcam runs an event "webcam_ImageCaptured" every time the picture from the webcam, which is like 5-10 fps.
但我不能找到一个简单的方式来获得来自两个图像的差异,或者至少一些作品体面。
But I cannot find a simple way to get the difference from the two images, or at least something that works decent.
有没有人上了车,我怎么能做到这一点很简单的一个想法(尽可能是)?
Has anybody got an idea on how I could do this rather simple (as possible as that is)?
推荐答案
获取运动检测用你提到是微不足道的图书馆工作。下面是一个AForge(2.2.4版)的例子。它的工作原理上的视频文件,但你可以轻松地适应摄像头的事件。
Getting motion detection to work using the libraries you mention is trivial. Following is an AForge (version 2.2.4) example. It works on a video file but you can easily adapt it to the webcam event.
约翰内斯'是正确的,但我觉得跟这些库玩弄简化的方式来了解基本图像处理。
Johannes' is right but I think playing around with these libraries eases the way to understanding basic image processing.
我的应用程序处理720p的视频在120FPS一个非常快的计算机上,使用固态硬盘和周围50FPS我开发的笔记本电脑。
My application processes 720p video at 120FPS on a very fast machine with SSDs and around 50FPS on my development laptop.
public static void Main()
{
float motionLevel = 0F;
System.Drawing.Bitmap bitmap = null;
AForge.Video.FFMPEG.VideoFileReader reader = null;
AForge.Vision.Motion.MotionDetector motionDetector = null;
motionDetector = GetDefaultMotionDetector();
reader.Open(@"C:\Temp.wmv");
while (true)
{
bitmap = reader.ReadVideoFrame();
if (bitmap == null) break;
// motionLevel will indicate the amount of motion as a percentage.
motionLevel = motionDetector.ProcessFrame(bitmap);
// You can also access the detected motion blobs as follows:
// ((AForge.Vision.Motion.BlobCountingObjectsProcessing) motionDetector.Processor).ObjectRectangles [i]...
}
reader.Close();
}
// Play around with this function to tweak results.
public static AForge.Vision.Motion.MotionDetector GetDefaultMotionDetector ()
{
AForge.Vision.Motion.IMotionDetector detector = null;
AForge.Vision.Motion.IMotionProcessing processor = null;
AForge.Vision.Motion.MotionDetector motionDetector = null;
//detector = new AForge.Vision.Motion.TwoFramesDifferenceDetector()
//{
// DifferenceThreshold = 15,
// SuppressNoise = true
//};
//detector = new AForge.Vision.Motion.CustomFrameDifferenceDetector()
//{
// DifferenceThreshold = 15,
// KeepObjectsEdges = true,
// SuppressNoise = true
//};
detector = new AForge.Vision.Motion.SimpleBackgroundModelingDetector()
{
DifferenceThreshold = 10,
FramesPerBackgroundUpdate = 10,
KeepObjectsEdges = true,
MillisecondsPerBackgroundUpdate = 0,
SuppressNoise = true
};
//processor = new AForge.Vision.Motion.GridMotionAreaProcessing()
//{
// HighlightColor = System.Drawing.Color.Red,
// HighlightMotionGrid = true,
// GridWidth = 100,
// GridHeight = 100,
// MotionAmountToHighlight = 100F
//};
processor = new AForge.Vision.Motion.BlobCountingObjectsProcessing()
{
HighlightColor = System.Drawing.Color.Red,
HighlightMotionRegions = true,
MinObjectsHeight = 10,
MinObjectsWidth = 10
};
motionDetector = new AForge.Vision.Motion.MotionDetector(detector, processor);
return (motionDetector);
}
这篇关于运动检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!