int MotionDetection(Bitmap CurrentFrame) { if (PreFrame == null) { PreFrame = GrayFilter.Apply(CurrentFrame); PixFilter.ApplyInPlace(PreFrame); return(0); } Bitmap ProcFrame = GrayFilter.Apply(CurrentFrame); PixFilter.ApplyInPlace(ProcFrame); MoveTowardsFilter.StepSize = 1; MoveTowardsFilter.OverlayImage = ProcFrame; MoveTowardsFilter.ApplyInPlace(PreFrame); DiffFilter.OverlayImage = PreFrame; Bitmap DiffImage = DiffFilter.Apply(ProcFrame); ThreFilter.ApplyInPlace(DiffImage); BlobsFilter.ProcessImage(DiffImage); Blob[] Blobs = BlobsFilter.GetObjectsInformation(); int ret = 0; if (Blobs.Length != 0) { Graphics graph = Graphics.FromImage(CurrentFrame); Pen pen = new Pen(Color.Yellow, 2); graph.DrawRectangle(pen, new Rectangle(3, 3, 10, 10)); foreach (Blob item in Blobs) { if (item.Area > blobArea) { if (ShowMotionCheckBox.Checked) { graph.DrawRectangle(pen, item.Rectangle); } ++ret; } } } videoShowPicBox.Image = CurrentFrame; PreFrame = ProcFrame; return(ret); }
// Process new frame public void ProcessFrame(ref Bitmap image) { if (backgroundFrame == null) { // create initial backgroung image backgroundFrame = processingFilter1.Apply(image); // get image dimension width = image.Width; height = image.Height; // just return for the first time return; } Bitmap tmpImage; // apply the the first filters sequence tmpImage = processingFilter1.Apply(image); if (++counter == 2) { counter = 0; // move background towards current frame moveTowardsFilter.OverlayImage = tmpImage; moveTowardsFilter.ApplyInPlace(backgroundFrame); } // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = backgroundFrame; // lock temporary image to apply several filters bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); // apply difference filter differenceFilter.ApplyInPlace(bitmapData); // apply threshold filter thresholdFilter.ApplyInPlace(bitmapData); // apply dilatation filter Bitmap tmpImage2 = dilatationFilter.Apply(bitmapData); // unlock temporary image tmpImage.UnlockBits(bitmapData); tmpImage.Dispose( ); // calculate amount of changed pixels pixelsChanged = (calculateMotionLevel) ? CalculateWhitePixels(tmpImage2) : 0; // find edges Bitmap tmpImage2b = edgesFilter.Apply(tmpImage2); tmpImage2.Dispose( ); // extract red channel from the original image Bitmap redChannel = extrachChannel.Apply(image); // merge red channel with moving object borders mergeFilter.OverlayImage = tmpImage2b; Bitmap tmpImage3 = mergeFilter.Apply(redChannel); redChannel.Dispose( ); tmpImage2b.Dispose( ); // replace red channel in the original image replaceChannel.ChannelImage = tmpImage3; Bitmap tmpImage4 = replaceChannel.Apply(image); tmpImage3.Dispose( ); image.Dispose( ); image = tmpImage4; }
// Process new frame public void ProcessFrame(ref Bitmap image) { if (backgroundFrame == null) { // create initial backgroung image backgroundFrame = processingFilter1.Apply(image); // get image dimension width = image.Width; height = image.Height; // just return for the first time return; } Bitmap tmpImage; // apply the the first filters sequence tmpImage = processingFilter1.Apply(image); if (++counter == 2) { counter = 0; // move background towards current frame moveTowardsFilter.OverlayImage = tmpImage; moveTowardsFilter.ApplyInPlace(backgroundFrame); } // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = backgroundFrame; // lock temporary image to apply several filters bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); // apply difference filter differenceFilter.ApplyInPlace(bitmapData); // apply threshold filter thresholdFilter.ApplyInPlace(bitmapData); // get object rectangles blobCounter.ProcessImage(bitmapData); Rectangle[] rects = blobCounter.GetObjectRectangles( ); // unlock temporary image tmpImage.UnlockBits(bitmapData); tmpImage.Dispose( ); pixelsChanged = 0; if (rects.Length != 0) { // create graphics object from initial image Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(Color.Red, 1)) { int n = 0; // draw each rectangle foreach (Rectangle rc in rects) { g.DrawRectangle(pen, rc); if ((n < 10) && (rc.Width > 15) && (rc.Height > 15)) { g.DrawImage(numbersBitmaps[n], rc.Left, rc.Top, 7, 9); n++; } // a little bit inaccurate, but fast if (calculateMotionLevel) { pixelsChanged += rc.Width * rc.Height; } } } g.Dispose( ); } }
public void ProcessFrame(Bitmap image) { pixelsChanged = 0; objectsCount = 0; if (backgroundFrame == null) { // create initial backgroung image backgroundFrame = processingFilter1.Apply(image); // get image dimension width = image.Width; height = image.Height; // just return for the first time return; } Bitmap tmpImage; // apply the the first filters sequence tmpImage = processingFilter1.Apply(image); if (++counter == 2) { counter = 0; // move background towards current frame moveTowardsFilter.OverlayImage = tmpImage; moveTowardsFilter.ApplyInPlace(backgroundFrame); } // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = backgroundFrame; // lock temporary image to apply several filters bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); // apply difference filter differenceFilter.ApplyInPlace(bitmapData); lock (thresholdFilter) { // apply threshold filter thresholdFilter.ApplyInPlace(bitmapData); } lock (blobCounter) { // get object rectangles blobCounter.ProcessImage(bitmapData); } // unlock temporary image tmpImage.UnlockBits(bitmapData); tmpImage.Dispose(); Rectangle[] rects = blobCounter.GetObjectRectangles(); if (rects.Length != 0) { // create graphics object from initial image Graphics g = Graphics.FromImage(image); Font font = SystemFonts.MessageBoxFont; using (Pen pen = new Pen(Color.Red, 1)) { int n = 0; // draw each rectangle foreach (Rectangle rc in rects) { if (rc.Width < this.MinObjectDimension && rc.Height < this.MinObjectDimension) { continue; } if (this.motionZone != null && !this.motionZone.RegionOverlapped(rc)) { continue; } objectsCount++; if (this.HighlightMotionRegions) { g.DrawRectangle(pen, rc); if ((n < 9) && (rc.Width > 15) && (rc.Height > 15)) { n++; g.DrawString(n.ToString(), font, Brushes.Red, new Point(rc.Left, rc.Top)); } } // a little bit inaccurate, but fast pixelsChanged += rc.Width * rc.Height; } } g.Dispose(); } }
private int width;// image width /// <summary> /// The ProcessFrame /// </summary> /// <param name="image">The image<see cref="Bitmap"/></param> public Bitmap ProcessFrame(Bitmap image) { if (backgroundFrame == null) { // create initial backgroung image backgroundFrame = grayscaleFilter.Apply(image); // get image dimension width = image.Width; height = image.Height; // just return for the first time return(null); } // apply the the grayscale file var tmpImage = grayscaleFilter.Apply(image); if (++counter == 2) { counter = 0; // move background towards current frame moveTowardsFilter.OverlayImage = tmpImage; moveTowardsFilter.ApplyInPlace(backgroundFrame); } // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = backgroundFrame; // lock temporary image to apply several filters bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); // apply difference filter differenceFilter.ApplyInPlace(bitmapData); // apply threshold filter thresholdFilter.ApplyInPlace(bitmapData); pixelsChanged = CalculateWhitePixels(bitmapData); var tmpImage2 = openingFilter.Apply(bitmapData); // unlock temporary image tmpImage.UnlockBits(bitmapData); tmpImage.Dispose(); // apply edges filter var tmpImage2b = edgesFilter.Apply(tmpImage2); tmpImage2.Dispose(); // extract red channel from the original image var redChannel = extrachChannel.Apply(image); // merge red channel with moving object borders mergeFilter.OverlayImage = tmpImage2b; var tmpImage3 = mergeFilter.Apply(redChannel); redChannel.Dispose(); tmpImage2b.Dispose(); // replace red channel in the original image replaceChannel.ChannelImage = tmpImage3; var tmpImage4 = replaceChannel.Apply(image); tmpImage3.Dispose(); image.Dispose(); image = tmpImage4; return(image); }
// Process new frame public void ProcessFrame(ref Bitmap image) { if (backgroundFrame == null) { // create initial backgroung image backgroundFrame = processingFilter1.Apply(image); // get image dimension width = image.Width; height = image.Height; // just return for the first time return; } Bitmap tmpImage; // apply the the first filters sequence tmpImage = processingFilter1.Apply(image); if (++counter == 2) { counter = 0; // move background towards current frame moveTowardsFilter.OverlayImage = tmpImage; moveTowardsFilter.ApplyInPlace(backgroundFrame); } // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = backgroundFrame; // lock temporary image to apply several filters bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); // apply difference filter differenceFilter.ApplyInPlace(bitmapData); // apply threshold filter thresholdFilter.ApplyInPlace(bitmapData); // get object rectangles blobCounter.ProcessImage(bitmapData); Rectangle[] rects = blobCounter.GetObjectsRectangles(); // unlock temporary image tmpImage.UnlockBits(bitmapData); tmpImage.Dispose(); pixelsChanged = 0; if (rects.Length != 0) { // create graphics object from initial image Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(Color.Red, 1)) { int n = 0; // draw each rectangle foreach (Rectangle rc in rects) { g.DrawRectangle(pen, rc); if ((n < 10) && (rc.Width > 15) && (rc.Height > 15)) { g.DrawImage(numbersBitmaps[n], rc.Left, rc.Top, 7, 9); n++; } // a little bit inaccurate, but fast if (calculateMotionLevel) { pixelsChanged += rc.Width * rc.Height; } } } String strValue = mac; strValue = Regex.Replace(strValue, @"-", ""); strValue = strValue.Remove(strValue.Length - 1); long time = CurrentTimeMillis(); String pictureFolderName = strValue + "\\image" + time + ".jpg"; String picturePath = Constants.SERVER_DIRECTORY + Constants.IMAGES_FOLDER + pictureFolderName; image.Save(picturePath); lastimage = picturePath; String RelativePath = Constants.IMAGES_FOLDER + pictureFolderName; String path = RelativePath.Replace("\\", "/"); Boolean ok = mDatabase.insertSuspiciousPicturePath(mac, time, @"./" + path); if (!ok) { Console.WriteLine("Error: Impossible to store picture: " + picturePath + " on the database!\n"); } g.Dispose(); } }