private void ProcessFrame(object sender, EventArgs e)
{
using (MemStorage storage = new MemStorage()) //create storage for motion components
{
Image<Bgr, Byte> image = _capture.QuerySmallFrame().PyrUp(); //reduce noise from the image
capturedImageBox.Image = image;
//update the motion history
_motionHistory.Update(image.Convert<Gray, Byte>());
#region get a copy of the motion mask and enhance its color
Image<Gray, Byte> motionMask = _motionHistory.Mask;
double[] minValues, maxValues;
System.Drawing.Point[] minLoc, maxLoc;
motionMask.MinMax(out minValues, out maxValues, out minLoc, out maxLoc);
motionMask._Mul(255.0 / maxValues[0]);
#endregion
//create the motion image
Image<Bgr, Byte> motionImage = new Image<Bgr, byte>(motionMask.Size);
//display the motion pixels in blue (first channel)
motionImage[0] = motionMask;
//Threshold to define a motion area, reduce the value to detect smaller motion
double minArea = 100;
storage.Clear(); //clear the storage
Seq<MCvConnectedComp> motionComponents = _motionHistory.GetMotionComponents(storage);
//iterate through each of the motion component
foreach (MCvConnectedComp comp in motionComponents)
{
//reject the components that have small area;
if (comp.area < minArea) continue;
// find the angle and motion pixel count of the specific area
double angle, motionPixelCount;
_motionHistory.MotionInfo(comp.rect, out angle, out motionPixelCount);
//reject the area that contains too few motion
if (motionPixelCount < comp.area * 0.05) continue;
//Draw each individual motion in red
DrawMotion(motionImage, comp.rect, angle, new Bgr(Color.Red));
}
// find and draw the overall motion angle
double overallAngle, overallMotionPixelCount;
_motionHistory.MotionInfo(motionMask.ROI, out overallAngle, out overallMotionPixelCount);
DrawMotion(motionImage, motionMask.ROI, overallAngle, new Bgr(Color.Green));
//Display the amount of motions found on the current image
UpdateText(String.Format("Total Motions found: {0}; Motion Pixel count: {1}", motionComponents.Total, overallMotionPixelCount));
//Display the image of the motion
motionImageBox.Image = motionImage;
}
}