// 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; }
private async Task CalibThread(FrameReadyEventArgs e) { Debug.WriteLine("Calibrating " + _calibrationStep + " " + _drawing); e.Frame.Bitmap.Save(@"C:\temp\daforge\src\img" + (_calibrationStep < 10?"0":"") + _calibrationStep + "-" + (_drawing?"1":"0") + "-" + _errors + ".jpg", ImageFormat.Jpeg); if (_errors > 100) { //calibration not possible return; } if (_calibrationStep == CalibrationFrames && !_drawing) { _cc.FrameReady -= BaseCalibration; // TODO //Grid.Calculate(); _vs.Close(); CalibrationCompleted(this, new EventArgs()); } else { if (_calibrationStep > 2) { if (_drawing) { // draw diffimage _drawing = false; _vs.Clear(); FillRects(); diffFilter.OverlayImage = e.Frame.Bitmap; } else { // analyse diffimage _calibrationStep++; _drawing = true; _vs.Clear(); FillRects(); _calibrationStep--; var gbm = diffFilter.Apply(e.Frame.Bitmap); gbm.Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg); #if DEBUG actImg = (Bitmap)gbm.Clone(); #endif var d = UnmanagedImage.FromManagedImage(gbm); var stats = new ImageStatistics(d); var gcf = new ColorFiltering(new IntRange(0, 255), new IntRange((int)(stats.GreenWithoutBlack.Mean + stats.GreenWithoutBlack.StdDev + 5), 255), new IntRange(0, 255)); var bcf = new ColorFiltering(new IntRange(0, 255), new IntRange(0, 255), new IntRange((int)(stats.BlueWithoutBlack.Mean + stats.BlueWithoutBlack.StdDev), 255)); //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Median + " Blue: " + stats.BlueWithoutBlack.Median); //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Mean + " Blue: " + stats.BlueWithoutBlack.Mean); var bf = new Difference(gcf.Apply(d)); bcf.ApplyInPlace(d); bf.ApplyInPlace(d); d.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg); stats = new ImageStatistics(d); gcf = new ColorFiltering(new IntRange(0, 255), new IntRange((int)stats.GreenWithoutBlack.Mean, 255), new IntRange(0, 255)); bcf = new ColorFiltering(new IntRange(0, 255), new IntRange(0, 255), new IntRange((int)stats.BlueWithoutBlack.Mean, 255)); // split channels var bbm = bcf.Apply(d); bbm.ToManagedImage().Save(@"C:\temp\daforge\bimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp); gcf.ApplyInPlace(d); d.ToManagedImage().Save(@"C:\temp\daforge\gimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp); var gblobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.YX, MaxHeight = 60, MinHeight = 15, MaxWidth = 60, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false }; gblobCounter.ProcessImage(d); var bblobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.YX, MaxHeight = 60, MinHeight = 15, MaxWidth = 60, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false }; bblobCounter.ProcessImage(bbm); ProcessBlobs(gblobCounter, 0); ProcessBlobs(bblobCounter, 1); #if DEBUG actImg.Save(@"C:\temp\daforge\squares\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg); #endif _calibrationStep++; } } else { switch (_calibrationStep) { case 2: var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap); //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\src" + _errors + ".jpg", ImageFormat.Jpeg); bm = diffFilter.Apply(bm); var gf = new GaussianBlur(9.0, 3); gf.ApplyInPlace(bm); var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255), new IntRange(20, 255)); cf.ApplyInPlace(bm); var blobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true }; blobCounter.ProcessImage(bm); bm.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.Jpeg); var blobs = blobCounter.GetObjectsInformation(); if (blobs.Any()) { int i = 0; List <IntPoint> corners; do { corners = PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++])); } while (corners.Count != 4); RecursiveAForgeCalibrator.GridBlobs.InPlaceSort(corners); Grid.TopLeft = new Point(corners[0].X, corners[0].Y); Grid.TopRight = new Point(corners[1].X, corners[1].Y); Grid.BottomLeft = new Point(corners[2].X, corners[2].Y); Grid.BottomRight = new Point(corners[3].X, corners[3].Y); if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 && Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 && Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 && Grid.TopLeft.X < Grid.BottomRight.X && blobs[i - 1].Area > 60000 && Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X && Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y && Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y) { _calibrationStep++; _drawing = true; _vs.Clear(); FillRects(); Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y)); Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y)); Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y)); Grid.AddPoint(new Point(_vs.Width, _vs.Height), new Point(corners[3].X, corners[3].Y)); //_mapper.PredictFromCorners(); } else { _calibrationStep = 0; _errors++; } } else { _calibrationStep = 0; _errors++; _vs.Draw(); } break; case 1: diffFilter.OverlayImage = e.Frame.Bitmap; //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\srcf" + _errors + ".jpg", ImageFormat.Jpeg); //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255)); _vs.Clear(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; case 0: Grid = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height); Grid.ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height); _mapper = new CornerBarycentricMapper(Grid); var thread = new Thread(() => { _vs.Show(); _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0)); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; } } } Debug.WriteLine("Releasing"); await Task.Delay(500); _sem.Release(); }
// 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); }
//! @copydoc IMotionDetector::ProcessFrame(ref Bitmap,ref Bitmap) public void ProcessFrame(ref Bitmap image, ref Bitmap player) { if (lastProcessedImageTime + processImageInterval > DateTime.UtcNow) { return; } if (backgroundFrame == null) { // create initial backgroung image backgroundFrame = processingFilter1.Apply(image); backgroundPlayerFrame = processingFilter1.Apply(player); // 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); // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = backgroundFrame; //actualizar o backgroud backgroundFrame = tmpImage.Clone(new Rectangle(0, 0, width, height), PixelFormat.Format8bppIndexed); // 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(); Bitmap playerProccessed = processingFilter1.Apply(player); Add addFilter = new Add(playerProccessed); Bitmap tmpPlayer = addFilter.Apply(backgroundPlayerFrame); // calculate amount of changed pixels pixelsChanged = CalculateWhitePixels(tmpImage2, tmpPlayer); if (MotionLevel >= alarmLevel) { if (MotionDetected != null) { MotionDetected(this, EventArgs.Empty); } } tmpImage2.Dispose(); tmpPlayer.Dispose(); backgroundPlayerFrame = playerProccessed; //image = backgroundPlayerFrame; lastProcessedImageTime = DateTime.UtcNow; }
// 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(); } }