/// <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); }
//Filter to find red private Bitmap redF(Bitmap image) { //Create Filter and set value ranges AForge.Imaging.Filters.HSLFiltering filter = new AForge.Imaging.Filters.HSLFiltering(); filter.Hue = new AForge.IntRange(350, 25); filter.Saturation = new AForge.Range(0.5f, 1); filter.Luminance = new AForge.Range(0.15f, 1); filter.ApplyInPlace(image); //Create a blob counter to search through the image AForge.Imaging.BlobCounter extractor = new AForge.Imaging.BlobCounter(); extractor.MinHeight = 5; extractor.MinWidth = 5; extractor.FilterBlobs = true; extractor.ProcessImage(image); //Gets the rectangles so that we can draw around them Rectangle[] rects = extractor.GetObjectsRectangles(); if (rects.Length > 0) { Rectangle objectRect = rects[0]; Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 2)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); //Get the direction and change state. objDirectionRed(objectRect, image.Width); currentState = RoboState.FOLLOW_RED; } return(image); }
public void nesnebul(Bitmap image, int index, Bitmap imageson) { AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(); blobCounter.MinWidth = 3; blobCounter.MinHeight = 3; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; blobCounter.ProcessImage(image); Rectangle[] rects = blobCounter.GetObjectsRectangles(); foreach (Rectangle recs in rects) { if (rects.Length > 0) { Rectangle objectRect = rects[0]; Graphics g = Graphics.FromImage(imageson); using (Pen pen = new Pen(Color.FromArgb(55, 160, 81), 5)) { g.DrawEllipse(pen, objectRect); } g.Dispose(); }//end if }//end foreach imageson.Save("C:\\Users\\Halil\\Desktop\\newframes\\image" + index + ".jpg"); }
private Bitmap blueF(Bitmap image) { //Create Filter and set value ranges AForge.Imaging.Filters.HSLFiltering filter = new AForge.Imaging.Filters.HSLFiltering(); filter.Hue = new AForge.IntRange(200, 240); filter.Saturation = new AForge.Range(0.3f, 1); filter.Luminance = new AForge.Range(0.1f, 1); filter.ApplyInPlace(image); AForge.Imaging.BlobCounter extractor = new AForge.Imaging.BlobCounter(); extractor.MinHeight = 5; extractor.MinWidth = 5; extractor.FilterBlobs = true; extractor.ProcessImage(image); //Create a blob counter to search through the image Rectangle[] rects = extractor.GetObjectsRectangles(); if (rects.Length > 0) { Rectangle objectRect = rects[0]; Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 2)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); //Get the direction and change state. if (currentState != RoboState.FOLLOW_RED && currentState != RoboState.FOLLOW_GREEN) { currentState = RoboState.SEARCH; } } return(image); }
private void FaceDetect_Click(object sender, EventArgs e) { if (!sw.IsRunning) { sw.Reset(); sw.Start(); } Process.Image = Image.FromFile(filepath, true); if (bvis == false) { bvis = true; } else { bvis = false; } GridOn.GridPaint(bvis, (Bitmap)Process.Image);//绘制网格线 label3.Text = "网络线划分"; try { Bitmap gray_img = Knowledge.ToGray(originalimg); //将原图转化为灰度图 Bitmap BW = Knowledge.Thresholding(gray_img); //利用大津法进行二值化处理 Bitmap detectimg = Knowledge.FaceDetection(BW); //最后利用算法进行检测 AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(detectimg); Rectangle[] rects = blobCounter.GetObjectsRectangles(); Rectangle[] outputrects = new Rectangle[rects.Count()]; ////object count int minx = 100; int tmp = 0; int rectsCount = rects.Count(); List <decimal> test = new List <decimal>(); for (int c = 0; c < rectsCount; c++) { int p = rects[c].Width * rects[c].Height; decimal bl = (decimal)rects[c].Height / (decimal)rects[c].Width; int maxx = (gray_img.Width * gray_img.Height) / 2; if (p > minx && (double)bl < 1.8 && (double)bl > 0.8 && p < maxx) { test.Add(bl); outputrects[tmp++] = rects[c]; } } RectanglesMarker marker = new RectanglesMarker(outputrects, Color.Red); this.Detected.Image = marker.Apply((Bitmap)Image.FromFile(filepath, true)); label4.Text = "检测结果"; ShowMsg("检测结束!"); } catch (ArgumentException) { MessageBox.Show("Invalid usage!"); } if (sw.IsRunning) { sw.Stop(); string str = sw.Elapsed.ToString(); timeElpse.Text = string.Empty; timeElpse.Text = "运行时间:" + str; } }
private Bitmap DisplayAndTrackDrawingTool(Bitmap image) { //grayscale the image AForge.Imaging.Filters.Grayscale grayscaling = new AForge.Imaging.Filters.Grayscale( 0.2125, 0.7154, 0.0721); image = grayscaling.Apply(image); //mirror the image AForge.Imaging.Filters.Mirror mirrImg = new AForge.Imaging.Filters.Mirror(false, true); mirrImg.ApplyInPlace(image); //get biggest blob AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(image); blobCounter.ProcessImage(image); Rectangle[] rectsInImage = blobCounter.GetObjectsRectangles(); this.txtBoxInfo.Text = rectsInImage.Length + " blobs found."; AForge.Imaging.Filters.ExtractBiggestBlob biggestBlob = new AForge.Imaging.Filters.ExtractBiggestBlob(); Bitmap bigstBlobImage = biggestBlob.Apply(image); IntPoint posBiggBlob = biggestBlob.BlobPosition; this.txtBoxInfo.Text = "Biggest Blob at: " + posBiggBlob.ToString(); //SetCursor(posBiggBlob); GenerateGraphics(posBiggBlob); return(image); }
private void ProcessBlobs(Bitmap image) { //erode unwanted pixels. Note - erosion is time consuming //AForge.Imaging.Filters.Erosion3x3 erode = new AForge.Imaging.Filters.Erosion3x3(); //erode.ApplyInPlace(image); AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(image); blobCounter.ProcessImage(image); Rectangle[] rectsInImage = blobCounter.GetObjectsRectangles(); this.lblInfotext.Text = rectsInImage.Length + " blobs found."; /* * foreach (Rectangle rect in rectsInImage) * { * //Use this piece of code to filter out blobs based on size. Very heavy * //piece of code! Use caution! * bf.ApplyInPlace(image); * } * */ //get the biggest blob AForge.Imaging.Filters.ExtractBiggestBlob biggestBlob = new AForge.Imaging.Filters.ExtractBiggestBlob(); Bitmap bigstBlobImage = biggestBlob.Apply(image); IntPoint posBiggBlob = biggestBlob.BlobPosition; this.lblInfotext.Text += " Biggest blob's coordinates: " + posBiggBlob.ToString(); //pnlMagicCursor.Location = new Point(posBiggBlob.X, posBiggBlob.Y); pnlMagicCursor.Location = new System.Drawing.Point(posBiggBlob.X, posBiggBlob.Y); GetBlobsPanel(posBiggBlob); }
public void nesnebul(Bitmap image, int index, Bitmap imageson) { AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(); blobCounter.MinWidth = 3; blobCounter.MinHeight = 3; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; blobCounter.ProcessImage(image); Rectangle[] rects = blobCounter.GetObjectsRectangles(); foreach (Rectangle recs in rects) { if (rects.Length > 0) { Rectangle objectRect = rects[0]; Graphics g = Graphics.FromImage(imageson); using (Pen pen = new Pen(Color.FromArgb(55, 160, 81), 5)) { g.DrawEllipse(pen, objectRect); } g.Dispose(); } //end if } //end foreach imageson.Save("C:\\Users\\Halil\\Desktop\\newframes\\image" + index + ".jpg"); } //end nesnebul
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image) { i++; Bitmap objectsImage = null; AForge.Imaging.Filters.EuclideanColorFiltering filter = new AForge.Imaging.Filters.EuclideanColorFiltering(); // set center colol and radius AForge.Imaging.RGB f = new AForge.Imaging.RGB(Color.Red); filter.CenterColor = f;// AForge.Imaging.RGB..Blue; filter.Radius = (short)100; // apply the filter objectsImage = image; filter.ApplyInPlace(image); // lock image for further processing System.Drawing.Imaging.BitmapData objectsData = objectsImage.LockBits(new Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat); // AForge.Imaging.Filters.Grayscale. // grayscaling AForge.Imaging.UnmanagedImage grayImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(new AForge.Imaging.UnmanagedImage(objectsData)); // unlock image objectsImage.UnlockBits(objectsData); AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(); // locate blobs // blobCounter.MinHeight = 100; // blobCounter.MinWidth = 60; blobCounter.ProcessImage(grayImage); blobCounter.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; Rectangle[] rects = blobCounter.GetObjectsRectangles(); if (rects.Length > 0) { Rectangle objectRect = rects[0]; // draw rectangle around derected object Graphics g = Graphics.FromImage(image); s = s + "\n" + i + " - top = " + objectRect.Top + " bottom " + objectRect.Bottom + " left " + objectRect.Left + " right " + objectRect.Right + " " + objectRect.X + " " + objectRect.Location.X + " " + objectRect.Location.Y; using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); int objectX = objectRect.X + objectRect.Width / 2 - image.Width / 2; int objectY = image.Height / 2 - (objectRect.Y + objectRect.Height / 2); // System.Console.Out. // label1.Text = label1.Text+objectRect.X; // ParameterizedThreadStart t = new ParameterizedThreadStart(p); // Thread aa = new Thread(t); // aa.Start(rects[0]); } Graphics g1 = Graphics.FromImage(image); Pen pen1 = new Pen(Color.FromArgb(160, 255, 160), 3); g1.DrawLine(pen1, image.Width / 2, 0, image.Width / 2, image.Width); g1.DrawLine(pen1, image.Width, image.Height / 2, 0, image.Height / 2); g1.Dispose(); }
/// <returns>Extract Blobs from image</returns> public static AForge.Imaging.Blob[] ExtractBlobs(this Bitmap image) { if (image.PixelFormat != PixelFormat.Format8bppIndexed) { throw new NotSupportedException("Blob extractor can be applied to binary 8bpp images only"); } AForge.Imaging.BlobCounter bc = new AForge.Imaging.BlobCounter(image); return(bc.GetObjects(image, true)); }
//functions public void process() { while (true) { FeatureExtracting.Notifier.WaitOne(); foreach (Bitmap image in queue.GetConsumingEnumerable()) { AForge.Imaging.BlobCounter bc = new AForge.Imaging.BlobCounter(image); Rectangle[] rects = bc.GetObjectsRectangles(); Rectangle biggest = new Rectangle(0, 0, 0, 0); Graphics g = Graphics.FromImage(image); double ratio = 0; foreach (Rectangle r in rects) { ratio = (r.Height + 1.0) / (r.Width + 1.0); if (biggest.Width * biggest.Height < r.Width * r.Height) { //check ratio if ((ratio < 1.1) && (ratio > 0.45)) { if (r.Width * r.Height > 200) { biggest = r; } } } } int imageCenter = (biggest.Width / 2) + biggest.X; int screenCenter = image.Width / 2; g.DrawRectangle(new Pen(Color.FromArgb(255, 0, 0)), biggest); string drawString = biggest.Height + " <-Height Width-> " + biggest.Width + "\nratio = " + ratio + "\n Image Center = " + imageCenter + "\nScreen Center = " + screenCenter; System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 11); System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White); float x = 10.0F; float y = 10.0F; System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(); g.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat); drawFont.Dispose(); drawBrush.Dispose(); Stats a = new Stats(); a.blobCount = 0; ActionPlanning.queue.Add(a); ActionPlanning.Notifier.Set(); #if DEBUG Program.mainForm.VideoViewer.Image = image; #endif } } }
// //////////////////////////////ObjectPanoramaToSTL private void ObjectPanoramaToSTL() { // create instance of blob counter AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(); // process input image blobCounter.ProcessImage((Bitmap)tempImages[3]); // get information about detected objects AForge.Imaging.Blob[] blobs = blobCounter.GetObjectsInformation(); // apply the filter // filter.ApplyInPlace((Bitmap)filteredImages[3]); imgSTL = filteredImages[3]; }
public Camera() { grayFilter = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721); scaleFilter = new AForge.Imaging.Filters.ResizeBilinear(300, 300); threshldFilter = new AForge.Imaging.Filters.BradleyLocalThresholding(); InvertFilter = new AForge.Imaging.Filters.Invert(); Blober = new AForge.Imaging.BlobCounter(); original = new Bitmap(300, 300); g = Graphics.FromImage(original); Blober.FilterBlobs = true; Blober.MinWidth = 5; Blober.MinHeight = 5; Blober.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; }
private int GetOptimizedThreshold(int count, Bitmap image, int minSize, int maxSize) { var blobCounter = new AForge.Imaging.BlobCounter(); AForge.Imaging.Blob[] blobs; int thresholdMax = 255; int thresholdMin = 0; blobCounter.FilterBlobs = true; blobCounter.MaxHeight = maxSize; blobCounter.MaxWidth = maxSize; blobCounter.MinHeight = minSize; blobCounter.MinWidth = minSize; for (var i = 255; i >= 0; i--) { if (i % 10 == 0) { DetectShapesProgress?.Invoke(0.5f / 255f * (255 - i)); } blobCounter.BackgroundThreshold = Color.FromArgb(255, i, i, i); blobCounter.ProcessImage(image); blobs = blobCounter.GetObjectsInformation(); if (blobs.Length == count) { thresholdMax = i; break; } } DetectShapesProgress?.Invoke(0.5f); for (var i = 0; i <= 255; i++) { if (i % 10 == 0) { DetectShapesProgress?.Invoke(0.5f + 0.5f / 255f * i); } blobCounter.BackgroundThreshold = Color.FromArgb(255, i, i, i); blobCounter.ProcessImage(image); blobs = blobCounter.GetObjectsInformation(); if (blobs.Length == count) { thresholdMin = i; break; } } DetectShapesProgress?.Invoke(1f); var thr = (thresholdMax + thresholdMin) / 2; return(thr); }
private List <Shape> DetectShapes(int count, Bitmap image, int minSize, int maxSize) { var blobCounter = new AForge.Imaging.BlobCounter(); AForge.Imaging.Blob[] blobs; blobCounter.FilterBlobs = true; blobCounter.MaxHeight = maxSize; blobCounter.MaxWidth = maxSize; blobCounter.MinHeight = minSize; blobCounter.MinWidth = minSize; var threshold = Recognition.GetThreshold(image); blobCounter.BackgroundThreshold = Color.FromArgb(255, threshold, threshold, threshold); blobCounter.ProcessImage(image); blobs = blobCounter.GetObjectsInformation(); if (blobs.Length == count) { DetectShapesProgress?.Invoke(1f); } else { threshold = GetOptimizedThreshold(count, image, minSize, maxSize); blobCounter.BackgroundThreshold = Color.FromArgb(255, threshold, threshold, threshold); blobCounter.ProcessImage(image); blobs = blobCounter.GetObjectsInformation(); } var shapes = new List <Shape>(); foreach (var blob in blobs) { var edgePoints = blobCounter.GetBlobsEdgePoints(blob); // FindQuadrilateralCorners: // The first point in the list is the point with lowest X coordinate // (and with lowest Y if there are several points with the same X value). // The corners are provided in counter clockwise order var corners = PointsCloud.FindQuadrilateralCorners(edgePoints); shapes.Add(new Shape(corners.ToArray(), blob)); } return(shapes); }
private void skindetect_Click(object sender, EventArgs e) { sw.Reset(); sw.Start(); Bitmap img = (Bitmap)Image.FromFile(filepath, true); //将原图进行导入并进行格式转化 Bitmap normalizedImg = YCbCr.Lightingconpensate(img); Bitmap skin = YCbCr.SkinDetect1(normalizedImg); //肤色检测 Bitmap BWskin = Knowledge.Thresholding(skin); //图片二值化 Bitmap gray_img = Knowledge.ToGray(img); AForge.Imaging.SusanCornersDetector scd = new AForge.Imaging.SusanCornersDetector(); //susan检测子 //检测人眼部分 AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(BWskin); //对图片进行检测联通区域 Rectangle[] rects = blobCounter.GetObjectsRectangles(); Rectangle[] outputrects = new Rectangle[rects.Count()]; ////object count int minx = 150; int tmp = 0; int rectsCount = rects.Count(); List <decimal> test = new List <decimal>(); for (int c = 0; c < rectsCount; c++) { int p = rects[c].Width * rects[c].Height; decimal bl = (decimal)rects[c].Height / (decimal)rects[c].Width; int maxx = (img.Width * img.Height) / 2; if (p > minx && (double)bl < 1.8 && (double)bl > 0.9 && p < maxx) { test.Add(bl); outputrects[tmp++] = rects[c]; } } RectanglesMarker marker = new RectanglesMarker(outputrects, Color.Red); Process2.Image = img; Detected.Image = marker.Apply((Bitmap)Image.FromFile(filepath, true)); sw.Stop(); string str = sw.Elapsed.ToString(); timeElpsed.Text = string.Empty; timeElpsed.Text = "运行时间:" + str; }
/// <summary> /// Apply rectangle to image /// </summary> /// <param name="image">Bitmap, any suitable image</param> /// <returns>Rectangle</returns> public Rectangle DrawRectangle(Bitmap image) { //Instantiate new blob counter AForge.Imaging.BlobCounter detectBlob = new AForge.Imaging.BlobCounter(); //Get object by size detectBlob.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; //Apply BlobCounter detectBlob.ProcessImage(image); //objects in an array.0 Rectangle[] rects = detectBlob.GetObjectsRectangles(); //Create graphics from image Graphics g = Graphics.FromImage(image); try { //Draw rectangle around largest rectangle (default colour is red) g.DrawRectangle(new Pen(Color.Red), rects[0]); //Optional to save the result image to the startup directory //string startupPath = System.IO.Directory.GetCurrentDirectory(); //image.Save(startupPath + @"\nameyourfile.jpg"); //Return the rantngla return rects[0]; } catch (IndexOutOfRangeException e) { //If no blobs return empty rectangle Rectangle rrr = new Rectangle(0, 0, 0, 0); return rrr; } }
void theCamera_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { Bitmap theFrame = eventArgs.Frame; AForge.Imaging.Filters.ColorFiltering colorFilter = new AForge.Imaging.Filters.ColorFiltering(new AForge.IntRange(200, 255), new AForge.IntRange(200, 255), new AForge.IntRange(200, 255)); colorFilter.ApplyInPlace(theFrame); AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(theFrame); var ledLOCs = blobCounter.GetObjectsRectangles(); string ledpos = "not there"; if (ledLOCs.Length > 0) { //should probably find out which one is actually the LED... later though. var pos = getLEDLocCenter(ledLOCs[0]); var centerPos = new AForge.Math.Vector3(theFrame.Width / 2, theFrame.Height / 2, 0); var ledVector = pos - centerPos; ledVector.Normalize(); var ledAngle = Math.Acos(ledVector.X); if (ledVector.Y < 0) { ledAngle = -ledAngle; } double deltaLEDAngle = 0; if (ledAngleInit) { deltaLEDAngle = ledAngle - lastLEDAngle; if (deltaLEDAngle > Math.PI * 2) { deltaLEDAngle -= Math.PI * 2; } if (deltaLEDAngle < -Math.PI * 2) { deltaLEDAngle += Math.PI * 2; } lastLEDAngle = ledAngle; } else { ledAngleInit = true; lastLEDAngle = ledAngle; } deltaLEDAngle = ledAngle * 180 / Math.PI; bool canTransmit = false; vrconnectMutex.WaitOne(); if (VRapp != null) { canTransmit = true; } vrconnectMutex.ReleaseMutex(); if (canTransmit) { byte [] deltaAngleBytes = BitConverter.GetBytes(deltaLEDAngle); //note: in degrees per frame try { if (VRapp.Connected) { VRapp.GetStream().Write(deltaAngleBytes, 0, deltaAngleBytes.Length); } } catch { VRapp = null; canTransmit = false; } } double displayLEDAngle = deltaLEDAngle; ledpos = displayLEDAngle.ToString(); var triVec = ledPoints(ledLOCs); if (triVec != new Vector4(0, 0, 0, 0)) { ledpos = triVec.ToString(); Graphics g = Graphics.FromImage(theFrame); g.DrawLine(Pens.Red, triVec.X, triVec.Y, triVec.Z, triVec.W); g.Flush(); g.Dispose(); } //if we're connected to the VR app, this is where we send the delta angle. } updateTheFrame(theFrame, ledpos); }
/// <summary> /// Обработка одного сэмпла /// </summary> /// <param name="index"></param> private int processSample(int r, int c) { /// Инвертируем изображение AForge.Imaging.Filters.Invert InvertFilter = new AForge.Imaging.Filters.Invert(); InvertFilter.ApplyInPlace(arrayPics[r, c]); /// Создаём BlobCounter, выдёргиваем самый большой кусок, масштабируем, пересечение и сохраняем /// изображение в эксклюзивном использовании AForge.Imaging.BlobCounterBase bc = new AForge.Imaging.BlobCounter(); bc.FilterBlobs = true; bc.MinWidth = 3; bc.MinHeight = 3; // Упорядочиваем по размеру bc.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; // Обрабатываем картинку bc.ProcessImage(arrayPics[r, c]); Rectangle[] rects = bc.GetObjectsRectangles(); if (rects.Length == 0) { finalPics[r, c] = AForge.Imaging.UnmanagedImage.FromManagedImage(new Bitmap(100, 100)); return(0); } // К сожалению, код с использованием подсчёта blob'ов не работает, поэтому просто высчитываем максимальное покрытие // для всех блобов - для нескольких цифр, к примеру, 16, можем получить две области - отдельно для 1, и отдельно для 6. // Строим оболочку, включающую все блоки. Решение плохое, требуется доработка int lx = arrayPics[r, c].Width; int ly = arrayPics[r, c].Height; int rx = 0; int ry = 0; for (int i = 0; i < rects.Length; ++i) { if (lx > rects[i].X) { lx = rects[i].X; } if (ly > rects[i].Y) { ly = rects[i].Y; } if (rx < rects[i].X + rects[i].Width) { rx = rects[i].X + rects[i].Width; } if (ry < rects[i].Y + rects[i].Height) { ry = rects[i].Y + rects[i].Height; } } // Обрезаем края, оставляя только центральные блобчики AForge.Imaging.Filters.Crop cropFilter = new AForge.Imaging.Filters.Crop(new Rectangle(lx, ly, rx - lx, ry - ly)); finalPics[r, c] = cropFilter.Apply(arrayPics[r, c]); // Масштабируем до 100x100 AForge.Imaging.Filters.ResizeBilinear scaleFilter = new AForge.Imaging.Filters.ResizeBilinear(100, 100); finalPics[r, c] = scaleFilter.Apply(finalPics[r, c]); // Ну и распознаём currentDeskState[r * 4 + c] = patternMatch(r, c); return(0); }
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> /// Обработка одного сэмпла /// </summary> /// <param name="index"></param> public static string processSample(ref Imaging.UnmanagedImage unmanaged) { string rez = "Обработка"; /// Инвертируем изображение AForge.Imaging.Filters.Invert InvertFilter = new AForge.Imaging.Filters.Invert(); InvertFilter.ApplyInPlace(unmanaged); /// Создаём BlobCounter, выдёргиваем самый большой кусок, масштабируем, пересечение и сохраняем /// изображение в эксклюзивном использовании AForge.Imaging.BlobCounterBase bc = new AForge.Imaging.BlobCounter(); bc.FilterBlobs = true; bc.MinWidth = 10; bc.MinHeight = 10; // Упорядочиваем по размеру bc.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size; // Обрабатываем картинку bc.ProcessImage(unmanaged); Rectangle[] rects = bc.GetObjectsRectangles(); rez = "Насчитали " + rects.Length.ToString() + " прямоугольников!"; //if (rects.Length == 0) //{ // finalPics[r, c] = AForge.Imaging.UnmanagedImage.FromManagedImage(new Bitmap(100, 100)); // return 0; //} // К сожалению, код с использованием подсчёта blob'ов не работает, поэтому просто высчитываем максимальное покрытие // для всех блобов - для нескольких цифр, к примеру, 16, можем получить две области - отдельно для 1, и отдельно для 6. // Строим оболочку, включающую все блоки. Решение плохое, требуется доработка int lx = unmanaged.Width; int ly = unmanaged.Height; int rx = 0; int ry = 0; for (int i = 0; i < rects.Length; ++i) { if (lx > rects[i].X) { lx = rects[i].X; } if (ly > rects[i].Y) { ly = rects[i].Y; } if (rx < rects[i].X + rects[i].Width) { rx = rects[i].X + rects[i].Width; } if (ry < rects[i].Y + rects[i].Height) { ry = rects[i].Y + rects[i].Height; } } // Обрезаем края, оставляя только центральные блобчики AForge.Imaging.Filters.Crop cropFilter = new AForge.Imaging.Filters.Crop(new Rectangle(lx, ly, rx - lx, ry - ly)); unmanaged = cropFilter.Apply(unmanaged); // Масштабируем до 100x100 AForge.Imaging.Filters.ResizeBilinear scaleFilter = new AForge.Imaging.Filters.ResizeBilinear(100, 100); unmanaged = scaleFilter.Apply(unmanaged); return(rez); }
// //////////////////////////////ObjectPanoramaToSTL private void ObjectPanoramaToSTL() { // create instance of blob counter AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(); // process input image blobCounter.ProcessImage((Bitmap)tempImages[3]); // get information about detected objects AForge.Imaging.Blob[] blobs = blobCounter.GetObjectsInformation(); // apply the filter // filter.ApplyInPlace((Bitmap)filteredImages[3]); imgSTL = filteredImages[3]; }
/// <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; }