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; } }
private void ProcessFrame( object sender, EventArgs e) { using (MemStorage storage = new MemStorage()) //create storage for motion components { Image<Bgr, Byte> image = _capture.QuerySmallFrame().PyrUp();//capturing a frame. Image<Bgr, Byte> cloned = image.Clone();//creating a copy of orginal image cloned = image.Resize(320, 240);//setting the resolution to 320 x 240 capturedImageBox.Image = image; //Setting of region of interest //x1=get point x1 // y1=get point y1 //x2=get point x2 // y2=get point y2 //height=y1-y2 //width=x1-x2 //arg of rectangle are (x1,y1,width,height) image.ROI = new System.Drawing.Rectangle(10, 20, 300, 200); _motionHistory.Update(image.Convert<Gray, Byte>());//update the motion history #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)); string s1; string s2; //creating folder--single folder is created with name-motion.you can change name of folder according to you. string activeDir = @"d:\"; //Create a new subfolder under the current active folder string newPath = System.IO.Path.Combine(activeDir, "intro123"); activeDir = @newPath; string newPath1 = System.IO.Path.Combine(activeDir, "video"); string newPath2 = System.IO.Path.Combine(activeDir, "streaming"); // Create the subfolder System.IO.Directory.CreateDirectory(newPath1); System.IO.Directory.CreateDirectory(newPath2); thershold = 1000; if (overallMotionPixelCount>thershold)//check minimium threshold for consider motion. { s1 = String.Format("d:/intro123/video/{0}.jpg", i); s2 = String.Format("d:/intro123/streaming/{0}.jpg", i);//formatting the string as e:/images/1.jpg etc CvInvoke.cvSaveImage(s1, cloned.Ptr ); CvInvoke.cvSaveImage(s2, cloned.Ptr);//saving the image i++;//incrementing the pic counter } //Display the image of the motion motionImageBox.Image = motionImage; } }
//motion detection processing private Image<Bgr, Byte> ProcessFrame(Image<Bgr, Byte> image) { // using (Image<Bgr, Byte> image = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC)) using (MemStorage storage = new MemStorage()) //create storage for motion components { if (_forgroundDetector == null) { //_forgroundDetector = new BGCodeBookModel<Bgr>(); // _forgroundDetector = new FGDetector<Bgr>(Emgu.CV.CvEnum.FORGROUND_DETECTOR_TYPE.FGD); _forgroundDetector = new BGStatModel<Bgr>(image, Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL); } _forgroundDetector.Update(image); // imageBoxFrameGrabber.Image = image; //update the motion history _motionHistory.Update(_forgroundDetector.ForgroundMask); #region get a copy of the motion mask and enhance its color double[] minValues, maxValues; Point[] minLoc, maxLoc; _motionHistory.Mask.MinMax(out minValues, out maxValues, out minLoc, out maxLoc); Image<Gray, Byte> motionMask = _motionHistory.Mask.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); if (showGridLines) { LineSegment2D line = new LineSegment2D(new Point(0, 169), new Point(520, 169)); LineSegment2D line2 = new LineSegment2D(new Point(259, 0), new Point(259, 340)); image.Draw(line, new Bgr(Color.White), 2); image.Draw(line2, new Bgr(Color.White), 2); } if (displayPosNum) { for (int i = 0; i < dsPos.Tables[0].Rows.Count; i++) { if (showPos) image.Draw("# " + dsPos.Tables[0].Rows[i][0].ToString(), ref font, new Point(int.Parse(dsPos.Tables[0].Rows[i][1].ToString()) - 120, int.Parse(dsPos.Tables[0].Rows[i][2].ToString()) - 50), new Bgr(Color.Yellow)); if (showNames) image.Draw(dsPos.Tables[0].Rows[i][3].ToString(), ref font, new Point(int.Parse(dsPos.Tables[0].Rows[i][1].ToString()) - 120, int.Parse(dsPos.Tables[0].Rows[i][2].ToString()) - 70), new Bgr(Color.Yellow)); } } if (red1 && red1cnt < 100) { red1cnt++; image.Draw(new Rectangle(0, 0, 255, 165), new Bgr(Color.Red), 3); if (red1cnt == 99) { red1 = false; red1cnt = 0; } } if (red2 && red2cnt < 100) { red2cnt++; image.Draw(new Rectangle(262, 0, 257, 167), new Bgr(Color.Red), 3); if (red2cnt == 99) { red2 = false; red2cnt = 0; } } if (red3 && red3cnt < 100) { red3cnt++; image.Draw(new Rectangle(0, 170, 260, 170), new Bgr(Color.Red), 3); if (red3cnt == 99) { red3 = false; red3cnt = 0; } } if (red4 && red4cnt < 100) { red4cnt++; image.Draw(new Rectangle(260, 170, 260, 170), new Bgr(Color.Red), 3); if (red4cnt == 99) { red4 = false; red4cnt = 0; } } if (green1 && green1cnt < 200) { green1cnt++; image.Draw(new Rectangle(0, 0, 255, 165), new Bgr(Color.Green), 3); if (green1cnt == 199) { green1 = false; green1cnt = 0; } } if (green2 && green2cnt < 200) { green2cnt++; image.Draw(new Rectangle(262, 0, 257, 167), new Bgr(Color.Green), 3); if (green2cnt == 199) { green2 = false; green2cnt = 0; } } if (green3 && green3cnt < 200) { green3cnt++; image.Draw(new Rectangle(0, 170, 260, 170), new Bgr(Color.Green), 3); if (green3cnt == 199) { green3 = false; green3cnt = 0; } } if (green4 && green4cnt < 200) { green4cnt++; image.Draw(new Rectangle(260, 170, 260, 170), new Bgr(Color.Green), 3); if (green4cnt == 199) { green4 = false; green4cnt = 0; } } //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); //if (motionPixelCount > 100000) { image.Draw(l5 , new Bgr(Color.Red), 10); } else { image.Draw(l5 , new Bgr(Color.Green), 10); } //reject the area that contains too few motion // if (motionPixelCount < comp.area * 0.8) continue; if (motionPixelCount < comp.area * 0.05) continue; int nearpos = nearestPosition(comp.rect.X, comp.rect.Y); //if (1000 > comp.area) continue; //Draw each individual motion in red // DrawMotion(motionImage, comp.rect, angle, new Bgr(Color.Red)); if (nearpos == 3 && comp.area < 500) continue; if (nearpos == 4 && comp.area < 500) continue; if (comp.rect.X > 60 && comp.rect.Y > 60) { if (motionQueue.Count == 100) { motionQueue.Dequeue(); motionQueue.Enqueue(nearpos); } else { motionQueue.Enqueue(nearpos); } // LineSegment2D l5 = new LineSegment2D(new Point(comp.rect.X, comp.rect.Y), new Point(comp.rect.X, comp.rect.Y)); // image.Draw(l5, new Bgr(Color.Red), 10); // image.Draw(comp.area.ToString(), ref font, new Point(comp.rect.X, comp.rect.Y), new Bgr(Color.LightGreen)); if (showMotion) image.Draw(comp.rect, new Bgr(Color.Yellow), 2); } } // 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 // imageBoxFrameGrabber.Image = motionImage; ///motion image return image; } }