public System.Drawing.Rectangle DetectObstacle(AForge.Imaging.Filters.HSLFiltering filter, Bitmap image, Vector2 minRect, ref Bitmap colourBitmap) { Bitmap filtered = filter.Apply(image); short[,] structuringElement = new short[,] { { 0, 1, 0 }, { 1, 1, 1 }, { 0, 1, 0 } }; filtered = Threshold(filtered, 1); AForge.Imaging.Filters.Opening openingFilter = new AForge.Imaging.Filters.Opening(structuringElement); //filtered = openingFilter.Apply(filtered); colourBitmap = filtered; AForge.Imaging.BlobCounter blobs = new AForge.Imaging.BlobCounter(filtered); System.Drawing.Rectangle[] rectangles = blobs.GetObjectsRectangles(); int size = 0; int chosen = 0; int newSize = 0; for (int i = 0; i < rectangles.Length; i++) { newSize = rectangles[i].Height * rectangles[i].Width; if (size < newSize) { chosen = i; size = newSize; } } if (rectangles.Length != 0) { if (rectangles[chosen].Width > minRect.X && rectangles[chosen].Height > minRect.Y) { return rectangles[chosen]; } } return new System.Drawing.Rectangle(0, 0, 0, 0); }
/// <summary> /// Look at the input image and find a suitable blob. /// </summary> /// <param name="filter">HSL filter to use for image segmentation.</param> /// <param name="image">Input image to segment.</param> /// <param name="minRect">Minimum size of output blob.</param> /// <param name="colourBitmap">Bitmap to write binary image to.</param> /// <param name="maxSize">Maximum size of output blob (default value will allow any size).</param> /// <returns>A rectangle array of all suitable blobs, or an empty array if no suitable blobs are found.</returns> protected System.Drawing.Rectangle[] DetectObstacle(AForge.Imaging.Filters.HSLFiltering filter, Bitmap image, System.Drawing.Point minRect, out Bitmap colourBitmap, System.Drawing.Point maxSize = default(System.Drawing.Point)) { Bitmap filtered; filtered = filter.Apply(image); //short[,] structuringElement = new short[,] { { 0, 1, 0 }, { 1, 1, 1 }, { 0, 1, 0 } }; filtered = BinaryImage(filtered); //AForge.Imaging.Filters.Opening openingFilter = new AForge.Imaging.Filters.Opening(structuringElement); //filtered = openingFilter.Apply(filtered); colourBitmap = filtered; AForge.Imaging.BlobCounter blobs = new AForge.Imaging.BlobCounter(); blobs.MinWidth = (int)minRect.X; blobs.MinHeight = (int)minRect.Y; if (!maxSize.IsEmpty) { blobs.MaxWidth = maxSize.X; blobs.MaxHeight = maxSize.Y; } blobs.FilterBlobs = true; blobs.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; blobs.ProcessImage(filtered); System.Drawing.Rectangle[] rectangles = blobs.GetObjectsRectangles(); return rectangles; }