private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap obraz) { int x = 0; Bitmap objectsImage = null; Bitmap mImage = null; mImage = (Bitmap)obraz.Clone(); filter.CenterColor = Color.FromArgb(kolor.ToArgb()); filter.Radius = (short)range; objectsImage = obraz; filter.ApplyInPlace(objectsImage); if (czyKolorWybrany == true) { BitmapData objectsData = objectsImage.LockBits(new Rectangle(0, 0, obraz.Width, obraz.Height), ImageLockMode.ReadOnly, obraz.PixelFormat); UnmanagedImage grayImage = filtrSzarosci.Apply(new UnmanagedImage(objectsData)); objectsImage.UnlockBits(objectsData); licznikDrobin.ProcessImage(grayImage); Rectangle[] kwadraty = licznikDrobin.GetObjectRectangles(); if (kwadraty.Length > 0) { foreach (Rectangle aktualnyKwadrat in kwadraty) { Graphics grafika = Graphics.FromImage(mImage); using (Pen pen = new Pen(Color.FromArgb(0, 255, 0), 2)) { grafika.DrawRectangle(pen, aktualnyKwadrat); } RozmiaryFigury a = new RozmiaryFigury(aktualnyKwadrat.Height, aktualnyKwadrat.Width); //tutaj wypelniaja sie liczniki (wewnatrz klasy RozmiaryFigury) tablicaFigur[x] = a; x++; grafika.Dispose(); } if (aktualnyStanWykresu == "dynamiczny") { backgroundWorker1.RunWorkerAsync(); } if (aktualnyStanWykresu == "statyczny" && licznikResetu < 2) { backgroundWorker1.RunWorkerAsync(); licznikResetu++; backgroundWorker1.CancelAsync(); } if (aktualnyStanWykresu == "statyczny" && licznikResetu == 2) { backgroundWorker1.CancelAsync(); } } } obraz = mImage; }
public List <Rect> GetBlobs() { List <Rect> result = new List <Rect>(); foreach (Rectangle rectangle in counter.GetObjectRectangles()) { result.Add(TransformCoordinates(rectangle)); } return(result); }
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image) { Bitmap objectsImage = null; Bitmap mImage = null; mImage = (Bitmap)image.Clone(); filter.CenterColor = Color.FromArgb(color.ToArgb()); filter.Radius = (short)range; objectsImage = image; filter.ApplyInPlace(objectsImage); BitmapData objectsData = objectsImage.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); objectsImage.UnlockBits(objectsData); blobCounter.ProcessImage(grayImage); Rectangle[] rects = blobCounter.GetObjectRectangles(); if (rects.Length > 0) { foreach (Rectangle objectRect in rects) { Graphics g = Graphics.FromImage(mImage); using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); } } image = mImage; }
/// <summary> /// Merges blobs that are close to each other. /// </summary> /// <param name="value">The blobs found after running connected componenets algorithm.</param> /// <returns></returns> // private ArrayList<ExtendedBlob> mergeBlobs(ArrayList<ExtendedBlob> value) // { // /* // * Using a very simple methology of merging. // * Search all blobs that in close proximity of x pixels. // */ // ICollection<ExtendedBlob> intermediateValues = // new ArrayList<ExtendedBlob>(value.Count); // int x = 10; // ExtendedBlob closeToMe = value.RemoveAt(0); // while(!value.IsEmpty) // { // for (int i = 0; i < value.Count; i++) // { // Rectangle mergeRectangle = closeToMe.Rectangle; // mergeRectangle.Width += x; // mergeRectangle.Height += x; // if (mergeRectangle.IntersectsWith(value[i].Rectangle)) // { // // closeToMe // intermediateValues.Add(value[i]); // } // } // } // // } /// <summary> /// Runs the conected components algorithm. /// </summary> /// <param name="image">The image on which to run the algorithms.</param> /// <returns></returns> private List <ExtendedBlob> runConectedComponentsAlgorithm(Bitmap image) { blobCounter = new BlobCounter(image); Blob[] blobs = blobCounter.GetObjects(image); Rectangle[] rects = blobCounter.GetObjectRectangles(); List <ExtendedBlob> returnValue = new List <ExtendedBlob>(blobs.Length); for (int i = 0; i < blobs.Length; i++) { // Use adapter method and convert blobs to extended blobs. returnValue.Add(new ExtendedBlob(blobs[i], null, Unknown.GetInstance(), rects[i])); } return(returnValue); }
public static void ObjectDetection(ref Bitmap image) { Bitmap objectsImage = null; Bitmap mImage = (Bitmap)image.Clone(); eulideanColorFilter.CenterColor = Color.FromArgb(FilterColor.ToArgb()); eulideanColorFilter.Radius = (short)Range; objectsImage = image; eulideanColorFilter.ApplyInPlace(objectsImage); // lock image for further processing BitmapData objectsData = objectsImage.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); // grayscaling UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); // unlock image objectsImage.UnlockBits(objectsData); // locate blobs BlobCounterFilter.ProcessImage(grayImage); Rectangle[] rects = BlobCounterFilter.GetObjectRectangles(); if (rects.Length > 0) { foreach (Rectangle objectRect in rects) { // draw rectangle around derected object Graphics g = Graphics.FromImage(mImage); using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); } } image = mImage; }
// 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(); } }
// Process new frame public void ProcessFrame(ref Bitmap image) { if (backgroundFrame == null) { // create initial backgroung image backgroundFrame = processingFilter1.Apply(image); // 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; Bitmap tmp = moveTowardsFilter.Apply(backgroundFrame); // dispose old background backgroundFrame.Dispose(); backgroundFrame = tmp; } // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = backgroundFrame; // apply the the second filters sequence Bitmap tmpImage2 = processingFilter2.Apply(tmpImage); tmpImage.Dispose(); // get object rectangles Rectangle[] rects = BlobCounter.GetObjectRectangles(tmpImage2); tmpImage2.Dispose(); // 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++; } } } g.Dispose(); }