public void FindDefectsTest() { Bitmap bmp = Properties.Resources.hand; Bitmap gray = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bmp); BlobCounter bc = new BlobCounter(gray); bc.ObjectsOrder = ObjectsOrder.Size; Blob[] blobs = bc.GetObjectsInformation(); bc.ExtractBlobsImage(bmp, blobs[0], true); Bitmap blob = blobs[0].Image.ToManagedImage(); BorderFollowing bf = new BorderFollowing(); List<IntPoint> contour = bf.FindContour(blob); GrahamConvexHull graham = new GrahamConvexHull(); List<IntPoint> hull = graham.FindHull(contour); ConvexHullDefects hullDefects = new ConvexHullDefects(10); List<ConvexityDefect> defects = hullDefects.FindDefects(contour, hull); /* PointsMarker marker = new PointsMarker(hull, Color.Green, 10); marker.ApplyInPlace(blob); ImageBox.Show(blob); */ Assert.AreEqual(2, defects.Count); Assert.AreEqual(new IntPoint(130, 10), contour[defects[0].Start]); Assert.AreEqual(new IntPoint(93, 109), contour[defects[0].Point]); Assert.AreEqual(new IntPoint(64, 9), contour[defects[0].End]); Assert.AreEqual(99.549179077148438, defects[0].Depth, 1e-5); Assert.IsFalse(double.IsNaN(defects[0].Depth)); // Assert.AreEqual(9912.9531239366424, defects[0].Area); Assert.AreEqual(new IntPoint(49, 18), contour[defects[1].Start]); Assert.AreEqual(new IntPoint(61, 106), contour[defects[1].Point]); Assert.AreEqual(new IntPoint(18, 127), contour[defects[1].End]); Assert.AreEqual(35.615153852366504, defects[1].Depth, 1e-5); Assert.IsFalse(double.IsNaN(defects[1].Depth)); // Assert.AreEqual(2293.7535682510002, defects[1].Area); }
public void FindContourTest() { Bitmap bmp = Properties.Resources.sample_black; Bitmap gray = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bmp); BlobCounter bc = new BlobCounter(gray); bc.ObjectsOrder = ObjectsOrder.Size; Blob[] blobs = bc.GetObjectsInformation(); bc.ExtractBlobsImage(bmp, blobs[0], true); List<IntPoint> expected = bc.GetBlobsEdgePoints(blobs[0]); Bitmap blob = blobs[0].Image.ToManagedImage(); BorderFollowing bf = new BorderFollowing(); List<IntPoint> actual = bf.FindContour(blob); Assert.AreEqual(expected.Count, actual.Count); foreach (IntPoint point in expected) Assert.IsTrue(actual.Contains(point)); foreach (IntPoint point in actual) Assert.IsTrue(expected.Contains(point)); IntPoint prev = actual[0]; for (int i = 1; i < actual.Count; i++) { IntPoint curr = actual[i]; Assert.IsTrue(System.Math.Abs(prev.X - curr.X) <= 1 && System.Math.Abs(prev.Y - curr.Y) <= 1); prev = curr; } IntPoint first = actual[0]; IntPoint last = actual[actual.Count - 1]; Assert.IsTrue(System.Math.Abs(first.X - last.X) <= 1 && System.Math.Abs(first.Y - last.Y) <= 1); }
/// <summary> /// Given the Window UI Screenshot, tries to detect all the buttons in it and returns all of them. /// </summary> /// <param name="res">Where to put candidate controls</param> /// <param name="bitmap">Bitmap containing the pixels of the UI to analyze</param> /// <param name="shouldInvert">If true, the bitmap will be inverted before analysis</param> /// <param name="minWidth">Minimum width of patterns to recognize</param> /// <param name="minHeight">Minimum height of patterns to recognize</param> /// <returns>void</returns> private void ScanWithVisualRecognition(ref CandidateSet res, Bitmap original, bool shouldInvert, int minWidth, int minHeight, Window w, IRankingPolicy policy) { // Setup the Blob counter BlobCounter blobCounter = new BlobCounter(); blobCounter.BackgroundThreshold = Color.FromArgb(10, 10, 10); blobCounter.FilterBlobs = true; blobCounter.CoupledSizeFiltering = false; blobCounter.MinHeight = minHeight; blobCounter.MinWidth = minWidth; List <Blob> blobs = new List <Blob>(); // Button scanning // Apply the grayscale. This is needed for AForge's filters using (Bitmap grey_scaled = Grayscale.CommonAlgorithms.BT709.Apply(original)) { // Invert the image if requested if (shouldInvert) { Invert invert = new Invert(); invert.ApplyInPlace(grey_scaled); } //grey_scaled.Save("C:\\users\\alberto geniola\\desktop\\dbg\\gray_scaled_" + toinvert + ".bmp"); using (Bitmap t1 = new Threshold(64).Apply(grey_scaled)) { using (var tmp = new Bitmap(t1.Width, t1.Height)) { using (Graphics g = Graphics.FromImage(tmp)) { g.DrawImage(t1, 0, 0); } // The blob counter will analyze the bitmap looking for shapes blobCounter.ProcessImage(tmp); var tmparr = blobCounter.GetObjectsInformation(); blobs.AddRange(tmparr); } } using (Bitmap t2 = new SISThreshold().Apply(grey_scaled)) { using (var tmp = new Bitmap(t2.Width, t2.Height)) { using (Graphics g = Graphics.FromImage(tmp)) { g.DrawImage(t2, 0, 0); } //tmp.Save("C:\\users\\alberto geniola\\desktop\\dbg\\t2_" + toinvert + ".bmp"); // The blob counter will analyze the bitmap looking for shapes blobCounter.ProcessImage(tmp); var tmparr = blobCounter.GetObjectsInformation(); blobs.AddRange(tmparr); } } } // Let's analyze every single shape for (int i = 0, n = blobs.Count; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); if (edgePoints.Count > 1) { IntPoint p0, p1; PointsCloud.GetBoundingRectangle(edgePoints, out p0, out p1); var r = new Rectangle(p0.X, p0.Y, p1.X - p0.X, p1.Y - p0.Y); // Skip any shape representing the border of the whole window ( +10px padding) if (r.Width >= (original.Width - 10)) { continue; } // This is most-likely a button! // Crop the image and pass it to the OCR engine for text recognition using (Bitmap button = new Bitmap(r.Width, r.Height)) { // Scan the original shape String txt = null; using (var g1 = Graphics.FromImage(button)) { g1.DrawImage(original, 0, 0, r, GraphicsUnit.Pixel); } // Process OCR on that image txt = scanButton(button); if (String.IsNullOrEmpty(txt)) { using (Bitmap tmp = Grayscale.CommonAlgorithms.BT709.Apply(button)) { if (shouldInvert) { new Invert().ApplyInPlace(tmp); } new SISThreshold().ApplyInPlace(tmp); txt = scanButton(tmp); } } // If still nothing is found, repeat the analysis with the second version of the filter if (String.IsNullOrEmpty(txt)) { using (Bitmap tmp = Grayscale.CommonAlgorithms.BT709.Apply(button)) { if (shouldInvert) { new Invert().ApplyInPlace(tmp); } new Threshold(64).ApplyInPlace(tmp); txt = scanButton(tmp); } } // At this point we should have a result. Add it to list if it does not overlap any UIAutomated element UIControlCandidate t = new UIControlCandidate(); t.PositionWindowRelative = r; var winLoc = w.WindowLocation; t.PositionScreenRelative = new Rectangle(r.X + winLoc.X, r.Y + winLoc.Y, r.Width, r.Height); t.Text = txt; t.Score = policy.RankElement(t); // If the item falls into the same area of a UI element, ignore it. bool overlaps = false; foreach (var el in res) { if (el.AutoElementRef != null && el.PositionScreenRelative.IntersectsWith(t.PositionScreenRelative)) { overlaps = true; break; } } if (!overlaps) { res.Add(t); } } } } }
/// <summary> /// Apply filter to an image. /// </summary> /// /// <param name="imageData">Source image to get biggest blob from.</param> /// /// <returns>Returns image of the biggest blob.</returns> /// /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception> /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the original image.</exception> /// <exception cref="InvalidImagePropertiesException">Source and original images must have the same size.</exception> /// <exception cref="ArgumentException">The source image does not contain any blobs.</exception> /// public Bitmap Apply(BitmapData imageData) { // check pixel format of the source image if (!FormatTranslations.ContainsKey(imageData.PixelFormat)) { throw new UnsupportedImageFormatException("Source pixel format is not supported by the filter."); } // locate blobs in the source image BlobCounter blobCounter = new BlobCounter(imageData); // get information about blobs Blob[] blobs = blobCounter.GetObjectsInformation( ); // find the biggest blob int maxSize = 0; Blob biggestBlob = null; for (int i = 0, n = blobs.Length; i < n; i++) { int size = blobs[i].Rectangle.Width * blobs[i].Rectangle.Height; if (size > maxSize) { maxSize = size; biggestBlob = blobs[i]; } } // check if any blob was found if (biggestBlob == null) { throw new ArgumentException("The source image does not contain any blobs."); } blobPosition = new IntPoint(biggestBlob.Rectangle.Left, biggestBlob.Rectangle.Top); // extract biggest blob's image if (originalImage == null) { blobCounter.ExtractBlobsImage(new UnmanagedImage(imageData), biggestBlob, false); } else { // check original image's format if ( (originalImage.PixelFormat != PixelFormat.Format24bppRgb) && (originalImage.PixelFormat != PixelFormat.Format32bppArgb) && (originalImage.PixelFormat != PixelFormat.Format32bppRgb) && (originalImage.PixelFormat != PixelFormat.Format32bppPArgb) && (originalImage.PixelFormat != PixelFormat.Format8bppIndexed) ) { throw new UnsupportedImageFormatException("Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only."); } // check its size if ((originalImage.Width != imageData.Width) || (originalImage.Height != imageData.Height)) { throw new InvalidImagePropertiesException("Original image must have the same size as passed source image."); } blobCounter.ExtractBlobsImage(originalImage, biggestBlob, false); } Bitmap managedImage = biggestBlob.Image.ToManagedImage( ); // dispose unmanaged image of the biggest blob biggestBlob.Image.Dispose( ); return(managedImage); }
/// <summary> /// Detects and recognizes cards from source image /// </summary> /// <param name="source">Source image to be scanned</param> /// <returns>Recognized Cards</returns> public CardCollection Recognize(Bitmap source) { var height = ((double)source.Height / (double)source.Width * 640d); ResizeBilinear ImageResizer = new ResizeBilinear(640, (int)height); source = ImageResizer.Apply(source); CardCollection collection = new CardCollection(); //Collection that will hold cards Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image FiltersSequence seq = new FiltersSequence(); seq.Add(Grayscale.CommonAlgorithms.BT709); //First add grayScaling filter seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter temp = seq.Apply(source); // Apply filters on source image //Extract blobs from image whose size width and height larger than 150 BlobCounter extractor = new BlobCounter(); extractor.FilterBlobs = true; extractor.MinWidth = extractor.MinHeight = 150; extractor.MaxWidth = extractor.MaxHeight = 350; extractor.ProcessImage(temp); //Will be used transform(extract) cards on source image QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation(); //Will be used resize(scaling) cards ResizeBilinear resizer = new ResizeBilinear(CardWidth, CardHeight); var Blobs = extractor.GetObjectsInformation(); foreach (Blob blob in Blobs) { //Get Edge points of card List <IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob); //Calculate/Find corners of card on source image from edge points List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints); quadTransformer.SourceQuadrilateral = corners; //Set corners for transforming card quadTransformer.AutomaticSizeCalculaton = true; Bitmap cardImg = quadTransformer.Apply(source); //Extract(transform) card image if (cardImg.Width > cardImg.Height) //If card is positioned horizontally { cardImg.RotateFlip(RotateFlipType.Rotate90FlipNone); //Rotate } cardImg = resizer.Apply(cardImg); //Normalize card size Card card = new Card(cardImg, corners.ToArray()); //Create Card Object char color; try { color = ScanColor(card.GetTopLeftPart()); //Scan color } catch (Exception ex) { continue; } bool faceCard = IsFaceCard(cardImg); //Determine type of card(face or not) if (!faceCard) { card.Suit = ScanSuit(cardImg, color); //Scan Suit of non-face card card.Rank = ScanRank(cardImg); //Scan Rank of non-face card } else { Bitmap topLeft = card.GetTopLeftPart(); seq.Clear(); seq.Add(Grayscale.CommonAlgorithms.BT709); seq.Add(new BradleyLocalThresholding()); topLeft = seq.Apply(topLeft); BlobsFiltering bFilter = new BlobsFiltering(5, 5, 150, 150); bFilter.ApplyInPlace(topLeft); //Filter blobs that can not be a suit // topLeft.Save("topleft.bmp", ImageFormat.Bmp); card.Suit = ScanFaceSuit(topLeft, color); //Scan suit of face card card.Rank = ScanFaceRank(topLeft); //Scan rank of face card } collection.Add(card); //Add card to collection } return(collection); }
private void FindObject(Bitmap im) { BlobCounter blobCounter = new BlobCounter(); blobCounter.MinWidth = 5; blobCounter.MinHeight = 5; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; BitmapData objectsData = im.LockBits(new Rectangle(0, 0, im.Width, im.Height), ImageLockMode.ReadOnly, im.PixelFormat); Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721); UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); im.UnlockBits(objectsData); blobCounter.ProcessImage(im); Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs = blobCounter.GetObjectsInformation(); last.Image = im; String state = "null"; foreach (Rectangle recs in rects) { if (rects.Length > 0) { Rectangle objectRect = rects[0]; Graphics g = camPic.CreateGraphics(); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2)) { g.DrawRectangle(pen, objectRect); } int objectX = objectRect.X + (objectRect.Width / 2); int objectY = objectRect.Y + (objectRect.Height / 2); g.Dispose(); state = getState(objectRect); this.Invoke((MethodInvoker) delegate { if (state != "null" && cur != state) { cur = state; sendData(state); } }); } } if (rects.Length <= 0 && cur != "begin") { cur = "begin"; sendData("null"); // if no rectangle, null } }
public void nesnebul(Bitmap image) { #region BlobCounter blobCounter = new BlobCounter(); blobCounter.MinWidth = 5; blobCounter.MinHeight = 5; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; //Grayscale griFiltre = new Grayscale(0.2125, 0.7154, 0.0721); //Grayscale griFiltre = new Grayscale(0.2, 0.2, 0.2); //Bitmap griImage = griFiltre.Apply(image); BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); // grayscaling Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721); UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); // unlock image image.UnlockBits(objectsData); blobCounter.ProcessImage(image); Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs = blobCounter.GetObjectsInformation(); pictureBox2.Image = image; #endregion //Multi tracking Çoklu cisim Takibi------- for (int i = 0; rects.Length > i; i++) { Rectangle objectRect = rects[i]; //List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); //Graphics g = Graphics.FromImage(image); Graphics g = pictureBox1.CreateGraphics(); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2)) { g.DrawRectangle(pen, objectRect); g.DrawString((i + 1).ToString(), new Font("Arial", 12), Brushes.Red, objectRect); //g.DrawLines(pen,ToPointsArray(edgePoints)); } //Cizdirilen Dikdörtgenin Koordinatlari aliniyor. int objectX = objectRect.X + (objectRect.Width / 2); int objectY = objectRect.Y + (objectRect.Height / 2); // g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1)); g.Dispose(); } //TODO: Nesneler arası kapalı çevrim draww if (rects.Length > 1) { for (int j = 0; j < rects.Length - 1; j++) { int ilkx = (rects[j].Right + rects[j].Left) / 2; int ilky = (rects[j].Top + rects[j].Bottom) / 2; int ikix = (rects[j + 1].Right + rects[j + 1].Left) / 2; int ikiy = (rects[j + 1].Top + rects[j + 1].Bottom) / 2; Graphics g = pictureBox2.CreateGraphics(); //g.DrawLine(Pens.Red, rects[j].Location, rects[j + 1].Location); //g.DrawLine(Pens.Blue, rects[0].Location, rects[rects.Length - 1].Location); g.DrawLine(Pens.Red, ilkx, ilky, ikix, ikiy); } int ilkX = rects[0].X + (rects[0].Width / 2); int ilkY = rects[0].Y + (rects[0].Height / 2); int ikiX = rects[1].X + (rects[1].Width / 2); int ikiY = rects[1].X + (rects[1].Height / 2); int uzaklik = System.Math.Abs(ilkX - ikiX); if (uzaklik < 120 && sayma == true) { xoop++; sayma = false; } if (uzaklik > 120) { sayma = true; } else { sayma = false; } this.Invoke((MethodInvoker) delegate { richTextBox1.Text = xoop.ToString() + "\n" + richTextBox1.Text + "\n";; }); } }
private void detectQuads(Bitmap bitmap) { // Greyscale filteredBitmap = Grayscale.CommonAlgorithms.BT709.Apply(bitmap); // edge filter SobelEdgeDetector edgeFilter = new SobelEdgeDetector(); edgeFilter.ApplyInPlace(filteredBitmap); // Threshhold filter Threshold threshholdFilter = new Threshold(190); threshholdFilter.ApplyInPlace(filteredBitmap); BitmapData bitmapData = filteredBitmap.LockBits( new Rectangle(0, 0, filteredBitmap.Width, filteredBitmap.Height), ImageLockMode.ReadWrite, filteredBitmap.PixelFormat); BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = 125; blobCounter.MinWidth = 125; blobCounter.ProcessImage(bitmapData); Blob[] blobs = blobCounter.GetObjectsInformation(); filteredBitmap.UnlockBits(bitmapData); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); Bitmap bm = new Bitmap(filteredBitmap.Width, filteredBitmap.Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bm); g.DrawImage(filteredBitmap, 0, 0); Pen pen = new Pen(Color.Red, 5); List <IntPoint> cardPositions = new List <IntPoint>(); // Loop through detected shapes for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); List <IntPoint> corners; bool sameCard = false; // is triangle or quadrilateral if (shapeChecker.IsConvexPolygon(edgePoints, out corners)) { // get sub-type PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners); // Only return 4 corner rectanges if ((subType == PolygonSubType.Parallelogram || subType == PolygonSubType.Rectangle) && corners.Count == 4) { // Check if its sideways, if so rearrange the corners so it's veritcal rearrangeCorners(corners); // Prevent it from detecting the same card twice foreach (IntPoint point in cardPositions) { if (corners[0].DistanceTo(point) < 40) { sameCard = true; } } if (sameCard) { continue; } // Hack to prevent it from detecting smaller sections of the card instead of the whole card if (GetArea(corners) < 20000) { continue; } cardPositions.Add(corners[0]); g.DrawPolygon(pen, ToPointsArray(corners)); // Extract the card bitmap QuadrilateralTransformation transformFilter = new QuadrilateralTransformation(corners, 211, 298); cardBitmap = transformFilter.Apply(cameraBitmap); List <IntPoint> artCorners = new List <IntPoint>(); artCorners.Add(new IntPoint(14, 35)); artCorners.Add(new IntPoint(193, 35)); artCorners.Add(new IntPoint(193, 168)); artCorners.Add(new IntPoint(14, 168)); // Extract the art bitmap QuadrilateralTransformation cartArtFilter = new QuadrilateralTransformation(artCorners, 183, 133); cardArtBitmap = cartArtFilter.Apply(cardBitmap); MagicCard card = new MagicCard(); card.corners = corners; card.cardBitmap = cardBitmap; card.cardArtBitmap = cardArtBitmap; card.area = GetArea(corners); magicCards.Add(card); } } } pen.Dispose(); g.Dispose(); filteredBitmap = bm; }
private void button2_Click(object sender, EventArgs e) { Bitmap bmp = new Bitmap(pictureBox1.Image); Bitmap bmpsobe; Bitmap bmpmedian; Bitmap otsu; if (pictureBox1.Image == null) { MessageBox.Show("Lütfen önce bir resim Seçin"); } else { progressBar1.Visible = true; int i, j; Color ort;//Color sınıfından bir renk nesne tanımlıyoruz. //int r,g,b; progressBar1.Maximum = bmp.Width * bmp.Height; //İşlem çubuğunun maksimim olduğu yer for döngüsünün sonundaki piksel değerine erişmemiz durumundadır. for (i = 0; i <= bmp.Width - 1; i++) //dikey olarak görüntümüzü tarıyoruz. { for (j = 0; j <= bmp.Height - 1; j++) //yatay olarak görüntümüzü tarıyoruz. { ort = bmp.GetPixel(i, j); ort = Color.FromArgb((byte)((ort.R + ort.G + ort.B) / 3), (byte)((ort.R + ort.G + ort.B) / 3), (byte)((ort.R + ort.G + ort.B) / 3)); bmp.SetPixel(i, j, ort); if ((i % 10) == 0)//her on satırda bir göstergeyi güncelle { progressBar1.Value = i * bmp.Height + j; Application.DoEvents(); } } } } //////////////////////// Median ////////////// bmpmedian = ExtBitmap.MedianFilter(bmp, 3); /////////////////////// SOBEL /////////////// bmpsobe = ExtBitmap.Sobel3x3Filter(bmpmedian, true); Bitmap bmpsobe1 = (Bitmap)bmpsobe.Clone(); /////////// otsu int x, y; int genislik = bmpsobe1.Width; int yukseklik = bmpsobe1.Height; byte[] pixeller = new byte[(int)genislik * yukseklik]; Bitmap resim = (Bitmap)bmpsobe1.Clone(); for (y = 0; y < yukseklik; y++) { for (x = 0; x < genislik; x++) { // Pixelleri kütüphanenin işleyebileceği tek boyutlu bir diziye atıyoruz. // Gri seviyede tüm ana renkler eşit olduğu için sadece kırmızıyı okumak gri seviye için yeterli. pixeller[y * genislik + x] = resim.GetPixel(x, y).R; } } byte esikDeger = 0; OtsuEsikleme(ref pixeller[0], ref esikDeger, genislik, yukseklik); int renkk; for (y = 0; y < yukseklik; y++) { for (x = 0; x < genislik; x++) { renkk = pixeller[y * genislik + x]; // gri resim.SetPixel(x, y, Color.FromArgb(renkk, renkk, renkk)); // Gri seviyeyi argb moduna dönüştürüp resme aktarıyoruz. } } otsu = (Bitmap)resim.Clone(); Bitmap bmperosion; Bitmap bmpdilation; Bitmap bmpclosing; bmperosion = ExtBitmap.DilateAndErodeFilter(otsu, 3, akıllıgecissistemleri.ExtBitmap.MorphologyType.Erosion, true, true, true); Bitmap bmperosionn = (Bitmap)bmperosion.Clone(); bmpdilation = ExtBitmap.DilateAndErodeFilter(bmperosionn, 7, akıllıgecissistemleri.ExtBitmap.MorphologyType.Dilation, true, true, true); Bitmap bmpdilationn = (Bitmap)bmpdilation.Clone(); Bitmap one = (Bitmap)bmpdilationn.Clone(); BlobsFiltering filter0 = new BlobsFiltering(); // filtre ayarlaması filter0.CoupledSizeFiltering = true; filter0.MinWidth = 70; filter0.MinHeight = 40; // filtre uygula filter0.ApplyInPlace(one); Bitmap one2 = (Bitmap)one.Clone(); bmpclosing = ExtBitmap.CloseMorphologyFilter(one2, 15, true, true, true); Bitmap bmperosio = (Bitmap)bmpclosing.Clone(); Bitmap bmperosionson = ExtBitmap.DilateAndErodeFilter(bmperosio, 9, akıllıgecissistemleri.ExtBitmap.MorphologyType.Erosion, true, true, true); Bitmap blobson = (Bitmap)bmperosionson.Clone(); BlobsFiltering filterson = new BlobsFiltering(); // filtre ayarla filterson.CoupledSizeFiltering = true; filterson.MinWidth = 50; filterson.MinHeight = 200; // filtre uygula filterson.ApplyInPlace(blobson); Bitmap rect = (Bitmap)blobson.Clone(); Bitmap one1 = (Bitmap)rect.Clone(); ConnectedComponentsLabeling filter = new ConnectedComponentsLabeling(); // filtre uygula Bitmap newImage = filter.Apply(one1); Bitmap newImage1 = (Bitmap)newImage.Clone(); // obje sayısını kontrol et int objectCount = filter.ObjectCount; BlobCounter blobCounter = new BlobCounter(); blobCounter.ProcessImage(rect); Blob[] blobs = blobCounter.GetObjectsInformation(); // Görüntüye çizmek için Graphic nesnesi ve bir kalem oluştur Graphics g = Graphics.FromImage(rect); Pen bluePen = new Pen(Color.Blue, 2); // her nesne kpntrol edilir ve etrafında daire çizilir. for (int i = 0, n = blobs.Length; i < n; i++) { /* * x1=0 * x2=1 * y1=2 * y2=3 */ List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints); int[] sinirlar = sinir(corners); sinirlar[0] = sinirlar[0] - 2; sinirlar[1] = sinirlar[1] + 2; sinirlar[2] = sinirlar[2] - 2; sinirlar[3] = sinirlar[3] + 2; int en = sinirlar[1] - sinirlar[0]; int boy = sinirlar[3] - sinirlar[2]; float ort = (float)en / (float)boy; List <IntPoint> ucnoktalar = new List <IntPoint>(); ucnoktalar.Add(new IntPoint(sinirlar[0], sinirlar[2])); ucnoktalar.Add(new IntPoint(sinirlar[1], sinirlar[2])); ucnoktalar.Add(new IntPoint(sinirlar[1], sinirlar[3])); ucnoktalar.Add(new IntPoint(sinirlar[0], sinirlar[3])); g.DrawPolygon(bluePen, ToPointsArray(ucnoktalar)); g.DrawString("Plaka kordinatlari : (x,y): (" + sinirlar[0].ToString() + "," + sinirlar[2].ToString() + ")\n en, boy,ort: " + (sinirlar[1] - sinirlar[0]).ToString() + ", " + (sinirlar[3] - sinirlar[2]).ToString() + "," + ort.ToString() + " blob sayisi:" + blobs.Length.ToString(), new Font("Arial", 8), Brushes.White, new System.Drawing.Point(sinirlar[0], sinirlar[3] + 4)); } bluePen.Dispose(); g.Dispose(); Bitmap rect1 = (Bitmap)pictureBox1.Image.Clone(); Graphics g1 = Graphics.FromImage(rect1); Pen bluePen2 = new Pen(Color.Red, 2); // her nesne kpntrol edilir ve etrafında daire çizilir. List <Blob> bloplar = new List <Blob>(); for (int i = 0, n = blobs.Length; i < n; i++) { /* x1,y1--------x2,y1 * x1=0 | | * x2=1 | | * y1=2 x1,y2--------x2,y2 * y2=3 */ List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints); int[] sinirlar = sinir(corners); sinirlar[0] = sinirlar[0] - 5; sinirlar[1] = sinirlar[1]; sinirlar[2] = sinirlar[2] - 5; sinirlar[3] = sinirlar[3] + 5; int en = sinirlar[1] - sinirlar[0]; int boy = sinirlar[3] - sinirlar[2]; float ort = (float)en / (float)boy; if (ort >= 3 && ort <= 5.7) { g1.DrawLines(bluePen2, new System.Drawing.Point[] { new System.Drawing.Point(sinirlar[0], sinirlar[2]), new System.Drawing.Point(sinirlar[1], sinirlar[2]), new System.Drawing.Point(sinirlar[1], sinirlar[3]), new System.Drawing.Point(sinirlar[0], sinirlar[3]), new System.Drawing.Point(sinirlar[0], sinirlar[2]) }); g1.DrawString("Plaka kordinatlari : (x,y): (" + sinirlar[0].ToString() + "," + sinirlar[2].ToString() + ")\n en, boy,ort: " + (sinirlar[1] - sinirlar[0]).ToString() + ", " + (sinirlar[3] - sinirlar[2]).ToString() + "," + ort.ToString() + " blob sayisi:" + blobs.Length.ToString(), new Font("Arial", 8), Brushes.White, new System.Drawing.Point(sinirlar[0], sinirlar[3] + 4)); } } bluePen2.Dispose(); g1.Dispose(); Bitmap bn = null; Bitmap kes1 = (Bitmap)rect1.Clone(); Graphics g2 = Graphics.FromImage(kes1); Pen bluePen3 = new Pen(Color.Red, 2); // her nesne kpntrol edilir ve etrafında daire çizilir. for (int i = 0, n = blobs.Length; i < n; i++) { /* x1,y1--------x2,y1 * x1=0 | | * x2=1 | | * y1=2 x1,y2--------x2,y2 * y2=3 */ List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints); int[] sinirlar = sinir(corners); sinirlar[0] = sinirlar[0] - 5; sinirlar[1] = sinirlar[1]; sinirlar[2] = sinirlar[2] - 5; sinirlar[3] = sinirlar[3] + 5; int en = sinirlar[1] - sinirlar[0]; int boy = sinirlar[3] - sinirlar[2]; float ort = en / boy; if (ort >= 3 && ort <= 5.7) { g2.DrawLines(bluePen3, new System.Drawing.Point[] { new System.Drawing.Point(sinirlar[0], sinirlar[2]), new System.Drawing.Point(sinirlar[1], sinirlar[2]), new System.Drawing.Point(sinirlar[1], sinirlar[3]), new System.Drawing.Point(sinirlar[0], sinirlar[3]), new System.Drawing.Point(sinirlar[0], sinirlar[2]) }); for (int ii = 0; ii < kes1.Width; ii++) { for (int ji = 0; ji < kes1.Height; ji++) { if ((ii >= sinirlar[0] && ii <= sinirlar[1]) && (ji >= sinirlar[2]) && ji <= sinirlar[3]) { continue; } else { kes1.SetPixel(ii, ji, Color.Black); } } } bn = new Bitmap(en, boy); Graphics g3 = Graphics.FromImage(bn); g3.DrawImage(kes1, -sinirlar[0], -sinirlar[2]); pictureBox2.Image = bn; } } bluePen3.Dispose(); g2.Dispose(); //form(bmp, "Grayscala"); //form(bmpmedian, "Median"); //form(bmpsobe, "sobel"); //form(otsu, "otsu"); //form(bmperosion, "erosion"); //form(bmpdilationn, "dilation"); //form(one, "one"); //form(bmpclosing, "closing"); //form(bmperosionson, "son"); //form(blobson, "blobson"); //form(newImage, "one1"); //form(rect, "rect"); //form(rect1, "rect1"); //form(kes1, "kes1"); //form(bn, "bn"); }
private ImageSource ProcessImageSourceForBitmap(Bitmap bmp) { BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); EuclideanColorFiltering filter = new EuclideanColorFiltering(); filter.CenterColor = new RGB(byte.Parse(redTextBox.Text), byte.Parse(greenTextBox.Text), byte.Parse(blueTextBox.Text)); filter.Radius = short.Parse(radiusTextBox.Text); filter.ApplyInPlace(bitmapData); BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = 15; blobCounter.MinWidth = 15; blobCounter.MaxHeight = bitmapData.Width - 1; blobCounter.MaxWidth = bitmapData.Width - 1; blobCounter.ProcessImage(bitmapData); Blob[] blobs = blobCounter.GetObjectsInformation(); bmp.UnlockBits(bitmapData); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); Graphics g = Graphics.FromImage(bmp); System.Drawing.Pen redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5); System.Drawing.Pen yellowPen = new System.Drawing.Pen(System.Drawing.Color.Yellow, 5); System.Drawing.Pen greenPen = new System.Drawing.Pen(System.Drawing.Color.Green, 5); System.Drawing.Pen bluePen = new System.Drawing.Pen(System.Drawing.Color.Blue, 5); for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); AForge.Point center; float radius; if (shapeChecker.IsCircle(edgePoints, out center, out radius)) { g.DrawEllipse(yellowPen, (float)(center.X - radius), (float)(center.Y - radius), (float)(radius * 2), (float)(radius * 2)); } else { List <IntPoint> corners; if (shapeChecker.IsQuadrilateral(edgePoints, out corners)) { if (shapeChecker.CheckPolygonSubType(corners) == PolygonSubType.Rectangle) { g.DrawPolygon(greenPen, ToPointsArray(corners)); } else { g.DrawPolygon(bluePen, ToPointsArray(corners)); } } else { corners = PointsCloud.FindQuadrilateralCorners(edgePoints); g.DrawPolygon(redPen, ToPointsArray(corners)); } } } redPen.Dispose(); greenPen.Dispose(); bluePen.Dispose(); yellowPen.Dispose(); g.Dispose(); var handle = bmp.GetHbitmap(); try { return(Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } finally { DeleteObject(handle); } }
private ImageSource ProcessRectangeImageSourceForBitmap(Bitmap bmp) { BitmapData bitmapData = bmp.LockBits(new Rectangle((bmp.Width / 2) - (FilterWidth / 2), (bmp.Height / 2) - (FilterHeight / 2), FilterWidth, FilterHeight), ImageLockMode.ReadWrite, bmp.PixelFormat); EuclideanColorFiltering filter = new EuclideanColorFiltering(new RGB(byte.Parse(redTextBox.Text), byte.Parse(greenTextBox.Text), byte.Parse(blueTextBox.Text)), short.Parse(radiusTextBox.Text)); filter.ApplyInPlace(bitmapData); if (StartDetecting) { BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = 2; blobCounter.MinWidth = 2; blobCounter.MaxHeight = bitmapData.Height - 1; blobCounter.MaxWidth = bitmapData.Width - 1; blobCounter.ProcessImage(bitmapData); bmp.UnlockBits(bitmapData); Blob[] blobs = blobCounter.GetObjectsInformation(); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); Graphics g = Graphics.FromImage(bmp); System.Drawing.Pen yellowPen = new System.Drawing.Pen(System.Drawing.Color.Yellow, 5); foreach (var corner in Corners) { g.DrawRectangle(yellowPen, corner.X + (bmp.Width / 2) - (FilterWidth / 2), corner.Y + (bmp.Height / 2) - (FilterHeight / 2), 1, 1); } for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); List <IntPoint> corners; if (shapeChecker.IsConvexPolygon(edgePoints, out corners)) { if (calibrated) { if (Corners.Count < MAX_CORNERS) { foreach (var corner in corners) { if (!Corners.Contains(corner)) { Corners.Add(corner); } } } else { testPassedLabel.Content = "Light was detected"; } } } } } var handle = bmp.GetHbitmap(); try { return(Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } finally { DeleteObject(handle); } }
/// <summary> /// Search for glyphs in the specified image and recognize them. /// </summary> /// /// <param name="image">Image to search glyphs in.</param> /// /// <returns>Return a list of found glyphs.</returns> /// /// <remarks><para>The method does processing of the specified image and searches for glyphs in it of /// the specified <see cref="GlyphSize">size</see>. In the case if <see cref="GlyphDatabase">glyphs' database</see> /// is set, it tries to find a matching glyph in it for each found glyph in the image. If matching is found, /// then <see cref="ExtractedGlyphData.RecognizedGlyph">RecognizedGlyph</see> and /// <see cref="ExtractedGlyphData.RecognizedQuadrilateral">RecognizedQuadrilateral</see> /// properties of <see cref="ExtractedGlyphData"/> are set correspondingly.</para></remarks> /// /// <exception cref="UnsupportedImageFormatException">Pixel format of the specified image is not supported. /// It must be 8 bpp indexed or 24/32 bpp color image.</exception> /// public List <ExtractedGlyphData> FindGlyphs(UnmanagedImage image) { List <ExtractedGlyphData> extractedGlyphs = new List <ExtractedGlyphData>( ); if ((image.PixelFormat != PixelFormat.Format8bppIndexed) && (!Grayscale.CommonAlgorithms.BT709.FormatTranslations.ContainsKey(image.PixelFormat))) { throw new UnsupportedImageFormatException("Pixel format of the specified image is not supported."); } // 1 - grayscaling UnmanagedImage grayImage = null; if (image.PixelFormat == PixelFormat.Format8bppIndexed) { grayImage = image; } else { grayImage = UnmanagedImage.Create(image.Width, image.Height, PixelFormat.Format8bppIndexed); Grayscale.CommonAlgorithms.BT709.Apply(image, grayImage); } // 2 - Edge detection UnmanagedImage edgesImage = edgeDetector.Apply(grayImage); // 3 - Threshold edges thresholdFilter.ApplyInPlace(edgesImage); // 4 - Blob Counter blobCounter.ProcessImage(edgesImage); Blob[] blobs = blobCounter.GetObjectsInformation( ); // 5 - check each blob for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); List <IntPoint> corners = null; // does it look like a quadrilateral ? if (shapeChecker.IsQuadrilateral(edgePoints, out corners)) { // get edge points on the left and on the right side List <IntPoint> leftEdgePoints, rightEdgePoints; blobCounter.GetBlobsLeftAndRightEdges(blobs[i], out leftEdgePoints, out rightEdgePoints); // calculate average difference between pixel values from outside of the shape and from inside float diff = CalculateAverageEdgesBrightnessDifference( leftEdgePoints, rightEdgePoints, grayImage); // check average difference, which tells how much outside is lighter than inside on the average if (diff > 20) { // perform glyph recognition ExtractedGlyphData glyphData = RecognizeGlyph(grayImage, corners); if (glyphData != null) { extractedGlyphs.Add(glyphData); if (extractedGlyphs.Count >= maxNumberOfGlyphsToSearch) { break; } } } } } // dispose resources if (image.PixelFormat != PixelFormat.Format8bppIndexed) { grayImage.Dispose( ); } edgesImage.Dispose( ); return(extractedGlyphs); }
private void button2_Click(object sender, EventArgs e) { button2.Text = "处理中"; switch (comboBox4.SelectedIndex) { case 0: { Bitmap temp = (Bitmap)pictureBox1.Image; OilPainting filter3 = new OilPainting(10); // apply the filter filter3.ApplyInPlace(temp); this.pictureBox2.Image = ResizeBitmap(temp); break; } case 1: { Bitmap temp = (Bitmap)pictureBox1.Image; temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp); DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector(); temp = edgeDetector.Apply(temp); temp = new Threshold((int)numericUpDown1.Value).Apply(temp); //FillHoles filter2 = new FillHoles(); //filter2.MaxHoleHeight = MinHeight; //filter2.MaxHoleWidth = MaxWidth; //filter2.CoupledSizeFiltering = false; // apply the filter //temp = filter2.Apply(temp); //HorizontalRunLengthSmoothing hrls = new HorizontalRunLengthSmoothing(40); // apply the filter //hrls.ApplyInPlace(temp); /*AForge.Imaging.Filters.BlobsFiltering filter = new AForge.Imaging.Filters.BlobsFiltering(); * // 设置过滤条件(对象长、宽至少为70) * filter.CoupledSizeFiltering = true; * filter.MaxWidth = (int)numericUpDown3.Value; * filter.MaxHeight = (int)numericUpDown4.Value; * filter.MinWidth = (int)numericUpDown5.Value; * filter.MinHeight = (int)numericUpDown6.Value; * filter.ApplyInPlace(temp);*/ BlobCounter blobCounter = new BlobCounter(); blobCounter.MinHeight = 32; blobCounter.MinWidth = 32; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; // 4 - find all stand alone blobs blobCounter.ProcessImage(temp); Blob[] blobs = blobCounter.GetObjectsInformation(); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); List <IntPoint> corners = null; List <IntPoint> corners2 = null; for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); // does it look like a quadrilateral ? if (shapeChecker.IsQuadrilateral(edgePoints, out corners)) { // get edge points on the left and on the right side List <IntPoint> leftEdgePoints, rightEdgePoints; blobCounter.GetBlobsLeftAndRightEdges(blobs[i], out leftEdgePoints, out rightEdgePoints); listBox1.DataSource = leftEdgePoints; listBox2.DataSource = rightEdgePoints; } } //listBox1.DataSource = corners; //listBox2.DataSource = corners2; this.pictureBox1.Image = temp; break; } case 2: { Bitmap bt2 = new Bitmap(@"D:\TCL条码\截图01.bmp"); Bitmap bt1 = new Bitmap(@"D:\TCL条码\截图03.bmp"); //Bitmap bt1 = new Bitmap(pictureBox2.Image); ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.80f); //基于一定的相似性阈值获得匹配块 TemplateMatch[] matchings = tm.ProcessImage(bt1, bt2); BitmapData data = bt1.LockBits( new Rectangle(0, 0, bt1.Width, bt1.Height), ImageLockMode.ReadWrite, bt1.PixelFormat); foreach (TemplateMatch m in matchings) { Drawing.Rectangle(data, m.Rectangle, Color.Red); } bt1.UnlockBits(data); pictureBox2.Image = bt1; break; } case 3: { Bitmap bt2 = new Bitmap(@"D:\TCL条码\Canny算法.png"); AForge.Imaging.Filters.BlobsFiltering filter = new AForge.Imaging.Filters.BlobsFiltering(); // 设置过滤条件(对象长、宽至少为70) filter.CoupledSizeFiltering = true; filter.MaxWidth = (int)numericUpDown3.Value; filter.MaxHeight = (int)numericUpDown4.Value; filter.MinWidth = (int)numericUpDown5.Value; filter.MinHeight = (int)numericUpDown6.Value; filter.ApplyInPlace(bt2); pictureBox1.Image = bt2; byte[] RESULT = BitmapToBytes(bt2); break; } case 4: { Bitmap temp = (Bitmap)pictureBox1.Image; temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp); AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector(); filter.ApplyInPlace(temp); pictureBox2.Image = temp; break; } } button2.Text = "处理"; }
private Bitmap BlobDetection(Bitmap bitmap) { var clone = bitmap.Clone() as Bitmap; var grayscaledBitmap = Grayscale.CommonAlgorithms.BT709.Apply(clone); new Threshold(Threshold).ApplyInPlace(grayscaledBitmap); BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinWidth = 70; blobCounter.MinHeight = 70; blobCounter.ObjectsOrder = ObjectsOrder.Size; blobCounter.ProcessImage(grayscaledBitmap); Blob[] blobs = blobCounter.GetObjectsInformation(); Bitmap tmp = new Bitmap(grayscaledBitmap.Width, grayscaledBitmap.Height); Graphics g = Graphics.FromImage(tmp); g.DrawImage(grayscaledBitmap, new Rectangle(0, 0, tmp.Width, tmp.Height), 0, 0, tmp.Width, tmp.Height, GraphicsUnit.Pixel); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); List <IntPoint> edgePoints; List <IntPoint> c = null; float radius; AForge.Point center; for (int i = 0, n = blobs.Length; i < n; i++) { edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); int ipenWidth = 5; if (shapeChecker.IsQuadrilateral(edgePoints, out c)) { System.Drawing.Point[] coordinates = ToPointsArray(c); Pen p = new Pen(Color.Blue, ipenWidth); if (coordinates.Length == 4) { if (ShapeDetection) { Rectangle += 1; rTmp = Rectangle; } PaintPolygon(c, bitmap, p); } } if (shapeChecker.IsCircle(edgePoints, out center, out radius)) { Pen p = new Pen(Color.Red, ipenWidth); if (ShapeDetection) { Circle += 1; cTmp = Circle; } PaintEllipse(bitmap, p, (float)(center.X - radius), (float)(center.Y - radius), (float)(radius * 2), (float)(radius * 2)); } if (shapeChecker.IsTriangle(edgePoints, out c)) { System.Drawing.Point[] coordinates = ToPointsArray(c); Pen p = new Pen(Color.Green, ipenWidth); if (coordinates.Length == 3) { if (ShapeDetection) { Triangle += 1; tTmp = Triangle; } PaintPolygon(c, bitmap, p); } } } if (Renewal) { Circle = 0; Rectangle = 0; Triangle = 0; clickX = 0; clickY = 0; clickZ = 0; for (int j = 0, k = blobs.Length; j < k; j++) { edgePoints = blobCounter.GetBlobsEdgePoints(blobs[j]); if (shapeChecker.IsQuadrilateral(edgePoints, out c)) { System.Drawing.Point[] coordinates = ToPointsArray(c); if (coordinates.Length == 4) { Rectangle += 1; } } if (shapeChecker.IsTriangle(edgePoints, out c)) { System.Drawing.Point[] coordinates = ToPointsArray(c); if (coordinates.Length == 3) { Triangle += 1; } } if (shapeChecker.IsCircle(edgePoints, out center, out radius)) { Circle += 1; } } } Renewal = false; ShapeDetection = false; return(bitmap); }
private void ParseImage(int imageId) { DateTime startTime = DateTime.Now; labelDetection.Text = ""; string difImagePath = ""; if (imageId >= 0) { difImagePath = DifBoardImages[imageId]; } Bitmap orig; Bitmap dif; try { orig = (Bitmap)Bitmap.FromFile(OriginalBoardImage); if (imageId >= 0) { dif = (Bitmap)Bitmap.FromFile(difImagePath); } else { dif = (Bitmap)pictureBoxGenerated.Image; } } catch { // kill any exception due to missing files return; } pictureBoxOrig.Image = dif; Difference filter = new Difference(orig); dif = filter.Apply(dif); BlobCounter blobCounter = new BlobCounter(); blobCounter.ProcessImage(dif); Blob[] blobs = blobCounter.GetObjectsInformation(); // create Graphics object to draw on the image and a pen Graphics g = Graphics.FromImage(dif); Pen redPen = new Pen(Color.Red, 3); Pen bluePen = new Pen(Color.Blue, 1); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); // check each object and draw circle around objects, which // are recognized as circles for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); labelDetection.Text = string.Format("{0} points", edgePoints.Count); if (edgePoints.Count <= 1) { continue; } List <IntPoint> points = new List <IntPoint>(); if (shapeChecker.IsQuadrilateral(edgePoints)) { labelDetection.Text += ", quadrilateral"; } else if (shapeChecker.IsConvexPolygon(edgePoints, out points)) { labelDetection.Text += ", convex poligon"; } else if (shapeChecker.IsTriangle(edgePoints, out points)) { labelDetection.Text += ", triangle"; } Pen usePen = shapeChecker.IsQuadrilateral(edgePoints) ? redPen : bluePen; int centerX = edgePoints[0].X; int centerY = edgePoints[0].Y; int minX = centerX; int minY = centerY; int maxX = centerX; int maxY = centerY; for (int j = 0; j < edgePoints.Count - 1; j++) { centerX += edgePoints[j + 1].X; centerY += edgePoints[j + 1].Y; if (edgePoints[j + 1].X < minX) { minX = edgePoints[j + 1].X; } if (edgePoints[j + 1].Y < minY) { minY = edgePoints[j + 1].Y; } if (edgePoints[j + 1].X > maxX) { maxX = edgePoints[j + 1].X; } if (edgePoints[j + 1].Y > maxX) { maxX = edgePoints[j + 1].Y; } g.DrawLine(usePen, edgePoints[j].X, edgePoints[j].Y, edgePoints[j + 1].X, edgePoints[j + 1].Y); } g.DrawLine(usePen , edgePoints[0].X, edgePoints[0].Y , edgePoints[edgePoints.Count - 1].X, edgePoints[edgePoints.Count - 1].Y); labelDetectedPosition.Text = string.Format("{0}, {1}" , new object[] { (centerX / edgePoints.Count), (centerY / edgePoints.Count) //, maxX - minX, maxY - minY }); labelDetectionOffset.Text = string.Format("{0}, {1}" , Grigore.Position.X - (centerX / edgePoints.Count) , Grigore.Position.Y - (centerY / edgePoints.Count) ); } redPen.Dispose(); bluePen.Dispose(); g.Dispose(); pictureBoxRoboBoard.Image = dif; labelDetection.Text = string.Format("{1}, {0} sec", (DateTime.Now - startTime), labelDetection.Text); }
public void nesnebul(Bitmap image) { BlobCounter blobCounter = new BlobCounter(); blobCounter.MinWidth = 5; blobCounter.MinHeight = 5; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; //Grayscale griFiltre = new Grayscale(0.2125, 0.7154, 0.0721); //Grayscale griFiltre = new Grayscale(0.2, 0.2, 0.2); //Bitmap griImage = griFiltre.Apply(image); BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); // grayscaling Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721); UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); // unlock image image.UnlockBits(objectsData); blobCounter.ProcessImage(image); Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs = blobCounter.GetObjectsInformation(); pictureBox2.Image = image; if (rdiobtnTekCisimTakibi.Checked) { //Tekli cisim Takibi Single Tracking-------- foreach (Rectangle recs in rects) { if (rects.Length > 0) { Rectangle objectRect = rects[0]; //Graphics g = Graphics.FromImage(image); Graphics g = pictureBox1.CreateGraphics(); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2)) { g.DrawRectangle(pen, objectRect); } //Cizdirilen Dikdörtgenin Koordinatlari aliniyor. int objectX = objectRect.X + (objectRect.Width / 2); int objectY = objectRect.Y + (objectRect.Height / 2); // g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1)); g.Dispose(); if (objectX <= 106 && objectY <= 80) { serialPort1.Write("1"); } else if (objectX >= 106.6 && objectX <= 213 && objectY <= 80) { serialPort1.Write("2"); } else if (objectX <= 320 && objectX >= 213 && objectY <= 80) { serialPort1.Write("3"); } else if (objectX <= 106 && objectY >= 80 && objectY <= 160) { serialPort1.Write("4"); } else if (objectX >= 106 && objectX <= 213 && objectY >= 80 && objectY <= 160) { serialPort1.Write("5"); } else if (objectX >= 213 && objectX <= 320 && objectY >= 80 && objectY <= 160) { serialPort1.Write("6"); } else if (objectX <= 106 && objectY >= 160 && objectY <= 240) { serialPort1.Write("7"); } else if (objectX >= 106 && objectX <= 213 && objectY >= 160 && objectY <= 240) { serialPort1.Write("8"); } else if (objectX >= 213 && objectX <= 320 && objectY >= 160 && objectY <= 240) { serialPort1.Write("9"); } if (chkKoordinatiGoster.Checked) { this.Invoke((MethodInvoker) delegate { richTextBox1.Text = objectRect.Location.ToString() + "\n" + richTextBox1.Text + "\n";; }); } } } } if (rdiobtnCokCisimTakibi.Checked) { //Multi tracking Çoklu cisim Takibi------- for (int i = 0; rects.Length > i; i++) { Rectangle objectRect = rects[i]; //Graphics g = Graphics.FromImage(image); Graphics g = pictureBox1.CreateGraphics(); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2)) { g.DrawRectangle(pen, objectRect); g.DrawString((i + 1).ToString(), new Font("Arial", 12), Brushes.Red, objectRect); } //Cizdirilen Dikdörtgenin Koordinatlari aliniyor. int objectX = objectRect.X + (objectRect.Width / 2); int objectY = objectRect.Y + (objectRect.Height / 2); // g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1)); if (chkboxMesafeOlcer.Checked) { if (rects.Length > 1) { for (int j = 0; j < rects.Length - 1; j++) { int ilkx = (rects[j].Right + rects[j].Left) / 2; int ilky = (rects[j].Top + rects[j].Bottom) / 2; int ikix = (rects[j + 1].Right + rects[j + 1].Left) / 2; int ikiy = (rects[j + 1].Top + rects[j + 1].Bottom) / 2; g = pictureBox1.CreateGraphics(); //g.DrawLine(Pens.Red, rects[j].Location, rects[j + 1].Location); //g.DrawLine(Pens.Blue, rects[0].Location, rects[rects.Length - 1].Location); g.DrawLine(Pens.Red, ilkx, ilky, ikix, ikiy); } } if (rects.Length == 2) { Rectangle ilk = rects[0]; Rectangle iki = rects[1]; int ilkX = ilk.X + (ilk.Width / 2); int ilkY = ilk.Y + (ilk.Height / 2); int ikiX = iki.X + (iki.Width / 2); int ikiY = iki.Y + (iki.Height / 2); //1 pixel (X) = 0.0264583333333334 centimeter [cm] double formul = Math.Floor((Math.Sqrt((Math.Pow((ilkX - ikiX), 2)) + Math.Pow((ilkY - ikiY), 2))) * 0.0264); string uzaklikY = "Y-" + Convert.ToString(ilkX - ikiX); string uzaklikX = "X-" + Convert.ToString(ilkY - ikiY); string distance = uzaklikX + " " + uzaklikY; AForge.Imaging.Drawing.Line(objectsData, new IntPoint((int)ilkX, (int)ilkY), new IntPoint((int)ikiX, (int)ikiY), Color.Blue); this.Invoke((MethodInvoker) delegate { richTextBox2.Text = formul.ToString() + " cm\n" + richTextBox2.Text + " cm\n";; }); if (chkboxMesafeKordinati.Checked) { this.Invoke((MethodInvoker) delegate { richTextBox3.Text = distance.ToString() + "\n" + richTextBox3.Text + "\n";; }); } } } g.Dispose(); // this.Invoke((MethodInvoker)delegate //{ // richTextBox1.Text = objectRect.Location.ToString() + "\n" + richTextBox1.Text + "\n"; ; //}); } } if (rdiobtnGeoSekil.Checked) { SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); Graphics g = pictureBox1.CreateGraphics(); Pen yellowPen = new Pen(Color.Yellow, 2); // circles Pen redPen = new Pen(Color.Red, 2); // quadrilateral Pen brownPen = new Pen(Color.Brown, 2); // quadrilateral with known sub-type Pen greenPen = new Pen(Color.Green, 2); // known triangle Pen bluePen = new Pen(Color.Blue, 2); // triangle for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); AForge.Point center; float radius; // is circle ? if (shapeChecker.IsCircle(edgePoints, out center, out radius)) { g.DrawEllipse(yellowPen, (float)(center.X - radius), (float)(center.Y - radius), (float)(radius * 2), (float)(radius * 2)); } else { List <IntPoint> corners; // is triangle or quadrilateral if (shapeChecker.IsConvexPolygon(edgePoints, out corners)) { // get sub-type PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners); Pen pen; if (subType == PolygonSubType.Unknown) { pen = (corners.Count == 4) ? redPen : bluePen; } else { pen = (corners.Count == 4) ? brownPen : greenPen; } g.DrawPolygon(pen, ToPointsArray(corners)); } } } yellowPen.Dispose(); redPen.Dispose(); greenPen.Dispose(); bluePen.Dispose(); brownPen.Dispose(); g.Dispose(); } }
/// <summary> /// Apply filter to an image. /// </summary> /// /// <param name="imageData">Source image to get biggest blob from.</param> /// /// <returns>Returns image of the biggest blob.</returns> /// /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception> /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the original image.</exception> /// <exception cref="InvalidImagePropertiesException">Source and original images must have the same size.</exception> /// <exception cref="ArgumentException">The source image does not contain any blobs.</exception> /// public Bitmap Apply( BitmapData imageData ) { // check pixel format of the source image if ( !FormatTranslations.ContainsKey( imageData.PixelFormat ) ) throw new UnsupportedImageFormatException( "Source pixel format is not supported by the filter." ); // locate blobs in the source image BlobCounter blobCounter = new BlobCounter( imageData ); // get information about blobs Blob[] blobs = blobCounter.GetObjectsInformation( ); // find the biggest blob int maxSize = 0; Blob biggestBlob = null; for ( int i = 0, n = blobs.Length; i < n; i++ ) { int size = blobs[i].Rectangle.Width * blobs[i].Rectangle.Height; if ( size > maxSize ) { maxSize = size; biggestBlob = blobs[i]; } } // check if any blob was found if ( biggestBlob == null ) { throw new ArgumentException( "The source image does not contain any blobs." ); } blobPosition = new IntPoint( biggestBlob.Rectangle.Left, biggestBlob.Rectangle.Top ); // extract biggest blob's image if ( originalImage == null ) { blobCounter.ExtractBlobsImage( new UnmanagedImage( imageData ), biggestBlob, false ); } else { // check original image's format if ( ( originalImage.PixelFormat != PixelFormat.Format24bppRgb ) && ( originalImage.PixelFormat != PixelFormat.Format32bppArgb ) && ( originalImage.PixelFormat != PixelFormat.Format32bppRgb ) && ( originalImage.PixelFormat != PixelFormat.Format32bppPArgb ) && ( originalImage.PixelFormat != PixelFormat.Format8bppIndexed ) ) { throw new UnsupportedImageFormatException( "Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only." ); } // check its size if ( ( originalImage.Width != imageData.Width ) || ( originalImage.Height != imageData.Height ) ) { throw new InvalidImagePropertiesException( "Original image must have the same size as passed source image." ); } blobCounter.ExtractBlobsImage( originalImage, biggestBlob, false ); } Bitmap managedImage = biggestBlob.Image.ToManagedImage( ); // dispose unmanaged image of the biggest blob biggestBlob.Image.Dispose( ); return managedImage; }
// define ProcessIMG function public void ProcessIMG(Bitmap image) { BlobCounter blobCounter = new BlobCounter(); blobCounter.MinWidth = 5; blobCounter.MinHeight = 5; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; // Grayscale griFilter = new Grayscale(0.2125, 0.7154, 0.0721); //Grayscale griFilter = new Grayscale(0.2, 0.2, 0.2); //Bitmap griImage = griFilter.Apply(image); BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); //grayscaling Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721); UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); // unlock image image.UnlockBits(objectsData); blobCounter.ProcessImage(image); Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs = blobCounter.GetObjectsInformation(); // coordinates: foreach (Rectangle rc in rects) { if ((rects.Length > 0) && (rects[0].Height > 0)) { Rectangle target = rects[0]; //System.Diagnostics.Debug.WriteLine( // string.Format("Position: ({0}, {1}), Size: {2} x {3}", // rc.Left, rc.Top, rc.Width, rc.Height)); PointF drawPoin = new PointF(target.X, target.Y); objectX = target.X + target.Width / 2 - image.Width / 2; objectY = image.Height / 2 - (target.Y + target.Height / 2); // To decide in which direction the robot should steer. int poZ = 320 - objectX; Graphics graphic = Graphics.FromImage(image); // To apply Grapghics on video2, that is, pictureBox2. Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(Color.White, 3)) { graphic.DrawRectangle(pen, target); // with PointF drawPoin string Blobinformation = "X= " + objectX.ToString() + "\nY= " + objectY.ToString() + "\nSize=" + target.Size.ToString() + "\npoZ=" + poZ.ToString(); g.DrawString(Blobinformation, new Font("Arial", 19), new SolidBrush(Color.White), drawPoin); g.DrawString("Target Located !", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2)); // to demonstrate 'recognition' of target. g.Dispose(); } graphic.Dispose(); } } pictureBox3.Image = image; }
/// <summary> /// Apply template /// </summary> /// <param name="template"></param> /// <param name="image"></param> public OmrPageOutput ApplyTemplate(OmrTemplate template, ScannedImage image) { // Image ready for scan if (!image.IsReadyForScan) { if (!image.IsScannable) { image.Analyze(); } image.PrepareProcessing(); } // Page output var retVal = new OmrPageOutput { Id = image.TemplateName + DateTime.Now.ToString("yyyyMMddHHmmss"), TemplateId = image.TemplateName, Parameters = image.Parameters, StartTime = DateTime.Now, Template = template }; // Save directory for output images var saveDirectory = string.Empty; var parmStr = new StringBuilder(); if (SaveIntermediaryImages) { if (image.Parameters != null) { foreach (var pv in image.Parameters) { parmStr.AppendFormat("{0}.", pv); } } retVal.RefImages = new List <string> { string.Format("{0}-{1}-init.bmp", retVal.Id, parmStr), string.Format("{0}-{1}-tx.bmp", retVal.Id, parmStr), string.Format("{0}-{1}-fields.bmp", retVal.Id, parmStr), string.Format("{0}-{1}-gs.bmp", retVal.Id, parmStr), string.Format("{0}-{1}-bw.bmp", retVal.Id, parmStr), string.Format("{0}-{1}-inv.bmp", retVal.Id, parmStr) }; saveDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "imgproc"); if (!Directory.Exists(saveDirectory)) { Directory.CreateDirectory(saveDirectory); } image.Image.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-init.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr))); } // First, we want to get the image from the scanned image and translate it to the original // position in the template Bitmap bmp = null; try { bmp = new Bitmap((int)template.BottomRight.X, (int)template.BottomRight.Y, PixelFormat.Format24bppRgb); // Scale float width = template.TopRight.X - template.TopLeft.X, height = template.BottomLeft.Y - template.TopLeft.Y; // Translate to original using (var g = Graphics.FromImage(bmp)) { var bc = new ResizeBicubic((int)width, (int)height); g.DrawImage(bc.Apply((Bitmap)image.Image), template.TopLeft.X, template.TopLeft.Y); } if (SaveIntermediaryImages) { bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-tx.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr))); } // Now try to do hit from the template if (SaveIntermediaryImages) { using (var tbmp = bmp.Clone() as Bitmap) { using (var g = Graphics.FromImage(tbmp)) { foreach (var field in template.Fields) { g.DrawRectangle(Pens.Black, field.TopLeft.X, field.TopLeft.Y, field.TopRight.X - field.TopLeft.X, field.BottomLeft.Y - field.TopLeft.Y); g.DrawString(field.Id, SystemFonts.CaptionFont, Brushes.Black, field.TopLeft); } } tbmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-fields.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr))); } } // Now convert to Grayscale var grayFilter = new GrayscaleY(); var gray = grayFilter.Apply(bmp); bmp.Dispose(); bmp = gray; if (SaveIntermediaryImages) { bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-gs.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr))); } // Prepare answers var hitFields = new Dictionary <OmrQuestionField, OmrOutputData>(); var barScan = new BarcodeReader(); barScan.Options.UseCode39ExtendedMode = true; barScan.Options.UseCode39RelaxedExtendedMode = true; barScan.Options.TryHarder = true; barScan.TryInverted = true; barScan.Options.PureBarcode = false; barScan.AutoRotate = true; foreach (var itm in template.Fields.Where(o => o is OmrBarcodeField)) { var position = itm.TopLeft; var size = new SizeF(itm.TopRight.X - itm.TopLeft.X, itm.BottomLeft.Y - itm.TopLeft.Y); using ( var areaOfInterest = new Crop(new Rectangle((int)position.X, (int)position.Y, (int)size.Width, (int)size.Height)).Apply(bmp)) { // Scan the barcode var result = barScan.Decode(areaOfInterest); if (result != null) { hitFields.Add(itm, new OmrBarcodeData { BarcodeData = result.Text, Format = result.BarcodeFormat, Id = itm.Id, TopLeft = new PointF(result.ResultPoints[0].X + position.X, result.ResultPoints[0].Y + position.Y), BottomRight = new PointF(result.ResultPoints[1].X + position.X, result.ResultPoints[0].Y + position.Y + 10) }); } } } // Now binarize var binaryThreshold = new Threshold(template.ScanThreshold); binaryThreshold.ApplyInPlace(bmp); if (SaveIntermediaryImages) { bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-bw.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr))); } // Set return parameters var tAnalyzeFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); bmp.Save(tAnalyzeFile, ImageFormat.Jpeg); retVal.AnalyzedImage = tAnalyzeFile; retVal.BottomRight = new PointF(bmp.Width, bmp.Height); // Now Invert var invertFiter = new Invert(); invertFiter.ApplyInPlace(bmp); if (SaveIntermediaryImages) { bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-inv.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr))); } // Crop out areas of interest var areasOfInterest = new List <KeyValuePair <OmrQuestionField, Bitmap> >(); foreach (var itm in template.Fields.Where(o => o is OmrBubbleField)) { var position = itm.TopLeft; var size = new SizeF(itm.TopRight.X - itm.TopLeft.X, itm.BottomLeft.Y - itm.TopLeft.Y); areasOfInterest.Add(new KeyValuePair <OmrQuestionField, Bitmap>( itm, new Crop(new Rectangle((int)position.X, (int)position.Y, (int)size.Width, (int)size.Height)).Apply(bmp)) ); } // Queue analysis var wtp = new WaitThreadPool(); var syncLock = new object(); foreach (var itm in areasOfInterest) { wtp.QueueUserWorkItem(img => { var parm = itm; try { var areaOfInterest = parm.Value; var field = parm.Key; var blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; // Check for circles blobCounter.ProcessImage(areaOfInterest); var blobs = blobCounter.GetObjectsInformation(); var blob = blobs.FirstOrDefault(o => o.Area == blobs.Max(b => b.Area)); if (blob != null) { //var area = new AForge.Imaging.ImageStatistics(blob).PixelsCountWithoutBlack; if (blob.Area < 30) { return; } var bubbleField = field as OmrBubbleField; lock (syncLock) { hitFields.Add(field, new OmrBubbleData { Id = field.Id, Key = bubbleField.Question, Value = bubbleField.Value, TopLeft = new PointF(blob.Rectangle.X + field.TopLeft.X, blob.Rectangle.Y + field.TopLeft.Y), BottomRight = new PointF(blob.Rectangle.X + blob.Rectangle.Width + field.TopLeft.X, blob.Rectangle.Y + blob.Rectangle.Height + field.TopLeft.Y), BlobArea = blob.Area }); } } } catch (Exception e) { Trace.TraceError(e.ToString()); } finally { parm.Value.Dispose(); } }, itm); } wtp.WaitOne(); // Organize the response foreach (var res in hitFields) { if (string.IsNullOrEmpty(res.Key.AnswerRowGroup)) { AddAnswerToOutputCollection(retVal, res); } else { // Rows of data var rowGroup = retVal.Details.Find(o => o.Id == res.Key.AnswerRowGroup) as OmrRowData; if (rowGroup == null) { rowGroup = new OmrRowData { Id = res.Key.AnswerRowGroup }; retVal.Details.Add(rowGroup); } AddAnswerToOutputCollection(rowGroup, res); } } // Remove temporary images //foreach (var f in retVal.RefImages) // File.Delete(Path.Combine(saveDirectory, f)); // Outcome is success retVal.Outcome = OmrScanOutcome.Success; } catch (Exception e) { retVal.Outcome = OmrScanOutcome.Failure; retVal.ErrorMessage = e.Message; Trace.TraceError(e.ToString()); } finally { retVal.StopTime = DateTime.Now; bmp.Dispose(); } return(retVal); }
private void AugmentedMethod2() { UnmanagedImage image = UnmanagedImage.FromManagedImage(new Bitmap(picSource.Image)); // 1 - grayscaling UnmanagedImage grayImage = null; if (image.PixelFormat == PixelFormat.Format8bppIndexed) { grayImage = image; } else { grayImage = UnmanagedImage.Create(image.Width, image.Height, PixelFormat.Format8bppIndexed); Grayscale.CommonAlgorithms.BT709.Apply(image, grayImage); } // 2 - Edge detection DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector(); UnmanagedImage edgesImage = edgeDetector.Apply(grayImage); // 3 - Threshold edges Threshold thresholdFilter = new Threshold(40); thresholdFilter.ApplyInPlace(edgesImage); // create and configure blob counter BlobCounter blobCounter = new BlobCounter(); blobCounter.MinHeight = 32; blobCounter.MinWidth = 32; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; // 4 - find all stand alone blobs blobCounter.ProcessImage(edgesImage); Blob[] blobs = blobCounter.GetObjectsInformation(); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); int counter = 0; // 5 - check each blob for (int i = 0, n = blobs.Length; i < n; i++) { // get edge points on the left and on the right side List <IntPoint> leftEdgePoints, rightEdgePoints; blobCounter.GetBlobsLeftAndRightEdges(blobs[i], out leftEdgePoints, out rightEdgePoints); // calculate average difference between pixel values from outside of the // shape and from inside float diff = CalculateAverageEdgesBrightnessDifference( leftEdgePoints, rightEdgePoints, grayImage); // check average difference, which tells how much outside is lighter than // inside on the average if (diff >= 50) { ++counter; } txtOut.AppendText(diff + ","); /*List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); * List<IntPoint> corners = null; * * // does it look like a quadrilateral ? * if (shapeChecker.IsQuadrilateral(edgePoints, out corners)) * { ++counter; * }*/ } txtOut.AppendText(Environment.NewLine); lblCount.Text = counter.ToString(); picResult.Image = edgesImage.ToManagedImage(); }
public Bitmap DetectElipse(System.Drawing.Image imgOrigin) { Bitmap bmp = (Bitmap)imgOrigin; Bitmap bmpAux = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), PixelFormat.Format32bppRgb); BitmapData bmData = bmpAux.LockBits( new Rectangle(0, 0, bmpAux.Width, bmpAux.Height), ImageLockMode.ReadWrite, bmpAux.PixelFormat); //Turn background to black ColorFiltering cFilter = new ColorFiltering(); cFilter.Red = new IntRange(0, 60); cFilter.Green = new IntRange(0, 60); cFilter.Blue = new IntRange(0, 60); cFilter.FillOutsideRange = false; cFilter.ApplyInPlace(bmData); //cFilter.Apply(bmData); //Blob --> finding shapes BlobCounter _blob = new BlobCounter(); //Investigar a fondo _blob.FilterBlobs = true; _blob.MinHeight = 30; _blob.MinWidth = 30; //Process the image, with magic _blob.ProcessImage(bmData); //Found shapes Blob[] _blobs = _blob.GetObjectsInformation(); bmpAux.UnlockBits(bmData); //Check found shapes SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); shapeChecker.MinAcceptableDistortion = 0.7f; shapeChecker.RelativeDistortionLimit = 0.08f; //color shapes Graphics g = Graphics.FromImage(bmpAux); Pen yellowPen = new Pen(Color.Yellow, 2); if (_blobs.Length != 0) { _elipse = true; } for (int i = 0; i < _blobs.Length; i++) { //Obtains the edges of the shape List <IntPoint> edgePoints = _blob.GetBlobsEdgePoints(_blobs[i]); //Points to the center of the shape AForge.Point center; float radius; if (shapeChecker.IsCircle(edgePoints, out center, out radius)) { //Draw founded shape g.DrawEllipse(yellowPen, (float)(center.X - radius), (float)(center.Y - radius), (float)(radius * 2), (float)(radius * 2)); } } yellowPen.Dispose(); g.Dispose(); return(bmpAux); }
public void nesnebul(Bitmap image) { BlobCounter blobCounter = new BlobCounter(); blobCounter.MinWidth = 5; blobCounter.MinHeight = 5; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; //Grayscale griFiltre = new Grayscale(0.2125, 0.7154, 0.0721); //Grayscale griFiltre = new Grayscale(0.2, 0.2, 0.2); //Bitmap griImage = griFiltre.Apply(image); BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); // grayscaling Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721); UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); // unlock image image.UnlockBits(objectsData); blobCounter.ProcessImage(image); Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs = blobCounter.GetObjectsInformation(); pictureBox2.Image = image; if (rdiobtnSingleTracking.Checked) { foreach (Rectangle recs in rects) { if (rects.Length > 0) { Rectangle objectRect = rects[0]; //Graphics g = Graphics.FromImage(image); Graphics g = pictureBox1.CreateGraphics(); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2)) { g.DrawRectangle(pen, objectRect); } int objectX = objectRect.X + (objectRect.Width / 2); int objectY = objectRect.Y + (objectRect.Height / 2); // g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1)); g.Dispose(); if (chkKoordinatiGoster.Checked) { this.Invoke((MethodInvoker) delegate { richTextBox1.Text = objectRect.Location.ToString() + "\n" + richTextBox1.Text + "\n";; }); } } } } if (rdiobtnManyTracking.Checked) { FirstR = rects.FirstOrDefault(coordX => coordX.X == rects.Max(x => x.X)); Rectangle tmp = rects.FirstOrDefault(coordX => coordX.X == rects.Min(x => x.X)); SecondR = (tmp == FirstR) ? default(Rectangle) : tmp; for (int i = 0; rects.Length > i; i++) { List <Rectangle> rectsList = new List <Rectangle>(); rectsList.AddRange(rects); Rectangle objectRect = rects[i]; //Graphics g = Graphics.FromImage(image); Graphics g = pictureBox1.CreateGraphics(); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2)) { g.DrawRectangle(pen, objectRect); g.DrawString((i + 1).ToString(), new Font("Arial", 12), Brushes.Red, objectRect); } int objectX = objectRect.X + (objectRect.Width / 2); int objectY = objectRect.Y + (objectRect.Height / 2); // g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1)); if (chkboxDistance.Checked) { if (rects.Length > 1) { for (int j = 0; j < rects.Length - 1; j++) { int ilkx = (rects[j].Right + rects[j].Left) / 2; int ilky = (rects[j].Top + rects[j].Bottom) / 2; int ikix = (rects[j + 1].Right + rects[j + 1].Left) / 2; int ikiy = (rects[j + 1].Top + rects[j + 1].Bottom) / 2; g = pictureBox1.CreateGraphics(); //g.DrawLine(Pens.Red, rects[j].Location, rects[j + 1].Location); //g.DrawLine(Pens.Blue, rects[0].Location, rects[rects.Length - 1].Location); g.DrawLine(Pens.Red, ilkx, ilky, ikix, ikiy); } } if (rects.Length == 2) { Rectangle ilk = rects[0]; Rectangle iki = rects[1]; int ilkX = ilk.X + (ilk.Width / 2); int ilkY = ilk.Y + (ilk.Height / 2); int ikiX = iki.X + (iki.Width / 2); int ikiY = iki.Y + (iki.Height / 2); double formul = Math.Floor((Math.Sqrt((Math.Pow((ilkX - ikiX), 2)) + Math.Pow((ilkY - ikiY), 2))) * 0.0264); string uzaklikY = "Y-" + Convert.ToString(ilkX - ikiX); string uzaklikX = "X-" + Convert.ToString(ilkY - ikiY); string distance = uzaklikX + " " + uzaklikY; AForge.Imaging.Drawing.Line(objectsData, new IntPoint((int)ilkX, (int)ilkY), new IntPoint((int)ikiX, (int)ikiY), Color.Blue); this.Invoke((MethodInvoker) delegate { richTextBox2.Text = formul.ToString() + " cm\n" + richTextBox2.Text + " cm\n";; }); if (chkboxDistanceCoord.Checked) { this.Invoke((MethodInvoker) delegate { richTextBox3.Text = distance.ToString() + "\n" + richTextBox3.Text + "\n";; }); } } } g.Dispose(); // this.Invoke((MethodInvoker)delegate //{ // richTextBox1.Text = objectRect.Location.ToString() + "\n" + richTextBox1.Text + "\n"; ; //}); } } }
/// <summary> /// Scans dominant color on image and returns it. /// Crops rank part on image and analyzes suit part on image /// </summary> /// <param name="bmp">Bitmap to be scanned</param> /// <returns>Returns 'B' for black , 'R' for red</returns> private char ScanColor(Bitmap bmp) { System.Diagnostics.Debug.Flush(); System.Diagnostics.Debug.Print("I'm here"); char color = 'B'; //Crop rank part Crop crop = new Crop(new Rectangle(0, bmp.Height / 2, bmp.Width, bmp.Height / 2)); bmp = crop.Apply(bmp); Bitmap temp = commonSeq.Apply(bmp); //Apply filters //Find suit blob on image BlobCounter counter = new BlobCounter(); counter.ProcessImage(temp); Blob[] blobs = counter.GetObjectsInformation(); if (blobs.Length > 0) //If blobs found { Blob max = blobs[0]; //Find blob whose size is biggest foreach (Blob blob in blobs) { if (blob.Rectangle.Height > max.Rectangle.Height) { max = blob; } else if (blob.Rectangle.Height == max.Rectangle.Height) { max = blob.Rectangle.Width > max.Rectangle.Width ? blob : max; } } QuadrilateralTransformation trans = new QuadrilateralTransformation(); trans.SourceQuadrilateral = PointsCloud.FindQuadrilateralCorners(counter.GetBlobsEdgePoints(max)); bmp = trans.Apply(bmp); //Extract suit } //Lock Bits for processing BitmapData imageData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); int totalRed = 0; int totalBlack = 0; unsafe { //Count red and black pixels try { UnmanagedImage img = new UnmanagedImage(imageData); int height = img.Height; int width = img.Width; int pixelSize = (img.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4; byte *p = (byte *)img.ImageData.ToPointer(); // for each line for (int y = 0; y < height; y++) { // for each pixel for (int x = 0; x < width; x++, p += pixelSize) { int r = (int)p[RGB.R]; //Red pixel value int g = (int)p[RGB.G]; //Green pixel value int b = (int)p[RGB.B]; //Blue pixel value if (r > g + b) //If red component is bigger then total of green and blue component { totalRed++; //then its red } else if (r <= g + b && (r < 50 && g < 50)) //If all components less 70 { totalBlack++; //then its black } } } } finally { bmp.UnlockBits(imageData); //Unlock } } if (totalRed > totalBlack) //If red is dominant { color = 'R'; //Set color as Red } return(color); }
private void ProcessImage(Bitmap bitmap) { // lock image BitmapData bitmapData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // step 1 - turn background to black ColorFiltering colorFilter = new ColorFiltering(); colorFilter.Red = new IntRange(Properties.Settings.Default.camFilterRed1, Properties.Settings.Default.camFilterRed2); colorFilter.Green = new IntRange(Properties.Settings.Default.camFilterGreen1, Properties.Settings.Default.camFilterGreen2); colorFilter.Blue = new IntRange(Properties.Settings.Default.camFilterBlue1, Properties.Settings.Default.camFilterBlue2); colorFilter.FillOutsideRange = Properties.Settings.Default.camFilterOutside; colorFilter.ApplyInPlace(bitmapData); // step 2 - locating objects BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = (int)Properties.Settings.Default.camShapeSizeMin * (int)cameraZoom; blobCounter.MinWidth = (int)Properties.Settings.Default.camShapeSizeMin * (int)cameraZoom; blobCounter.MaxHeight = (int)Properties.Settings.Default.camShapeSizeMax * (int)cameraZoom; blobCounter.MaxWidth = (int)Properties.Settings.Default.camShapeSizeMax * (int)cameraZoom; blobCounter.ProcessImage(bitmapData); Blob[] blobs = blobCounter.GetObjectsInformation(); bitmap.UnlockBits(bitmapData); // step 3 - check objects' type and highlight SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); shapeChecker.MinAcceptableDistortion = (float)Properties.Settings.Default.camShapeDist; shapeChecker.RelativeDistortionLimit = (float)Properties.Settings.Default.camShapeDistMax; Graphics g = Graphics.FromImage(bitmap); Pen yellowPen = new Pen(Color.Yellow, 5); // circles Pen redPen = new Pen(Color.Red, 10); // circles Pen greenPen = new Pen(Color.Green, 5); // known triangle double lowestDistance = xmid; double distance; shapeFound = false; AForge.Point center; double shapeRadius = 1; for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); System.Single radius; // is circle ? // g.DrawPolygon(greenPen, ToPointsArray(edgePoints)); if (Properties.Settings.Default.camShapeCircle && shapeChecker.IsCircle(edgePoints, out center, out radius)) { shapeFound = true; distance = center.DistanceTo((AForge.Point)picCenter); g.DrawEllipse(yellowPen, (float)(shapeCenter.X - shapeRadius), (float)(shapeCenter.Y - shapeRadius), (float)(shapeRadius * 2), (float)(shapeRadius * 2)); if (lowestDistance > Math.Abs(distance)) { lowestDistance = Math.Abs(distance); shapeCenter = center; shapeRadius = radius; } } List <IntPoint> corners; if (Properties.Settings.Default.camShapeRect && shapeChecker.IsQuadrilateral(edgePoints, out corners)) //.IsConvexPolygon { IntPoint minxy, maxxy, centxy; shapeFound = true; PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners); g.DrawPolygon(yellowPen, ToPointsArray(corners)); PointsCloud.GetBoundingRectangle(corners, out minxy, out maxxy); centxy = (minxy + maxxy) / 2; distance = picCenter.DistanceTo(centxy);// PointsCloud.GetCenterOfGravity(corners)); if (lowestDistance > Math.Abs(distance)) { lowestDistance = Math.Abs(distance); shapeCenter = centxy; // PointsCloud.GetCenterOfGravity(corners); shapeRadius = maxxy.DistanceTo(minxy) / 2; // 50; } } } if (shapeFound) { g.DrawEllipse(redPen, (float)(shapeCenter.X - shapeRadius * 1.2), (float)(shapeCenter.Y - shapeRadius * 1.2), (float)(shapeRadius * 2.4), (float)(shapeRadius * 2.4)); } yellowPen.Dispose(); redPen.Dispose(); greenPen.Dispose(); g.Dispose(); pictureBoxVideo.BackgroundImage = bitmap; }
/// <summary> /// Performs the analysis on the image /// </summary> public void Analyze(bool thorough = true) { if (this.m_disposed) { throw new ObjectDisposedException("ImageProcessor"); } if (this.IsScannable) { return; } LuminanceSource source = new BitmapLuminanceSource(this.m_bitmap); var binarizer = new HybridBinarizer(source); var binBitmap = new BinaryBitmap(binarizer); // Try to extract the form data var barReader = new ZXing.BarcodeReader(); barReader.AutoRotate = true; barReader.Options.TryHarder = thorough; barReader.Options.PossibleFormats = new List <BarcodeFormat>() { BarcodeFormat.CODE_128 }; this.m_barcodeResults = barReader.DecodeMultiple(source); // Look for barcode markers if possible if (this.m_barcodeResults != null) { this.m_markerCodes = this.m_barcodeResults.Where(o => o.Text.StartsWith("OMR:")).ToArray(); } this.IsScannable = true; // Get the template data var markerCode = this.m_markerCodes == null ? null : this.m_markerCodes.FirstOrDefault(o => o.Text.StartsWith("OMR:TL") || o.Text.StartsWith("OMR:ID")); // Get the guiding points by circles GrayscaleY grayFilter = new GrayscaleY(); Threshold thresholdFilter = new Threshold(127); Invert invertFilter = new Invert(); using (Bitmap searchImage = invertFilter.Apply(thresholdFilter.Apply(grayFilter.Apply(this.m_bitmap)))) { // Blobs BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = 30; blobCounter.MinWidth = 30; // Check for circles blobCounter.ProcessImage(searchImage); Blob[] blobs = blobCounter.GetObjectsInformation(); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); List <AForge.Point> controlPoints = new List <AForge.Point>(); int currentCheck = 45; while (currentCheck-- > 20 && controlPoints.Count != 4) { controlPoints.Clear(); // Get the positions foreach (var blob in blobs) { AForge.Point center = new AForge.Point(); float radius = 0; if (shapeChecker.IsCircle(blobCounter.GetBlobsEdgePoints(blob), out center, out radius) && radius > currentCheck) { controlPoints.Add(center); } } } // Control points this.IsScannable &= controlPoints.Count == 4; if (!this.IsScannable) { return; } // Now set markers this.m_topLeft = controlPoints[0]; //new AForge.Point(this.m_bitmap.Width + 10, this.m_bitmap.Height + 10); this.m_topRight = controlPoints[1]; this.m_bottomLeft = controlPoints[2]; this.m_bottomRight = controlPoints[3]; // Find the right most bubble float rightMost = controlPoints.Select(o => o.X).Max(), leftMost = controlPoints.Select(o => o.X).Min(); // Organize those that are left/right AForge.Point[] lefties = controlPoints.Where(o => o.X < leftMost + (rightMost - leftMost) / 2).ToArray(), righties = controlPoints.Where(o => o.X > leftMost + (rightMost - leftMost) / 2).ToArray(); // HACK: if (lefties[0].Y < lefties[1].Y) { this.m_topLeft = lefties[0]; this.m_bottomLeft = lefties[1]; } else { this.m_topLeft = lefties[1]; this.m_bottomLeft = lefties[0]; } // HACK: if (righties[0].Y < righties[1].Y) { this.m_topRight = righties[0]; this.m_bottomRight = righties[1]; } else { this.m_topRight = righties[1]; this.m_bottomRight = righties[0]; } } if (!this.IsScannable) { return; } // Get the template data if (this.m_markerCodes != null && markerCode != null) { String[] templateData = markerCode.Text.Split(':'); if (templateData.Length > 2) { this.TemplateName = templateData[2]; if (templateData.Length > 3) { this.Parameters = templateData.Skip(3).ToArray(); } } } }
private void btn_search_Click(object sender, EventArgs e) { //bright = 0; // value = 10; openFileDialog1.Title = "영상파일 열기"; openFileDialog1.Filter = "All Files(*.*)|*.*| Bitmap File(*.bmp)|*.bmp|GIF File(*.gif)|*.gif|JPEG File(*.jpg)|*.jpg|PNG file(*.png)|*.png|TIFF(*.tif)|*.tif"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { openstrFilename = openFileDialog1.FileName; image = System.Drawing.Image.FromFile(openstrFilename); myBitmap = new Bitmap(image); this.xray_preview.Image = myBitmap;// 원 이미지를 담는 PictureBox 개체 } //임시화일삭제 string filePath = @"Image\temp.jpg"; FileInfo file = new FileInfo(filePath); if (file.Exists) { file.Delete(); } /* *손바닥 외곽선 추출 */ Bitmap CroppedImage = myBitmap.Clone(new System.Drawing.Rectangle(0, 100, myBitmap.Width, (myBitmap.Height - 100)), myBitmap.PixelFormat); //손바닥 int width = 600; int height = 600; Size resize = new Size(width, height); resizeImage = new Bitmap(CroppedImage, resize); gsImage = Grayscale.CommonAlgorithms.BT709.Apply(resizeImage); filter = new CannyEdgeDetector(); edge = filter.Apply(gsImage); //외곽선 블러링 Blur hfilter = new Blur(); // apply the filter hfilter.ApplyInPlace(edge); /////////////////////////// // process image with blob counter BlobCounter hblobCounter = new BlobCounter(); hblobCounter.ProcessImage(edge); Blob[] hblobs = hblobCounter.GetObjectsInformation(); // create convex hull searching algorithm GrahamConvexHull hhullFinder = new GrahamConvexHull(); // lock image to draw on it BitmapData hdata = edge.LockBits(new Rectangle(0, 0, edge.Width, edge.Height), ImageLockMode.ReadWrite, edge.PixelFormat); // process each blob List <IntPoint> hhull = new List <IntPoint> { }; List <IntPoint> hedgePoints = new List <IntPoint> { }; int hblobcount = 0; int hminX = 0, hmaxX = 700, hminY = 0, hmaxY = 700; foreach (Blob blob in hblobs) { List <IntPoint> leftPoints, rightPoints; // get blob's edge points hblobCounter.GetBlobsLeftAndRightEdges(blob, out leftPoints, out rightPoints); hedgePoints.AddRange(leftPoints); hedgePoints.AddRange(rightPoints); // blob's convex hull hhull = hhullFinder.FindHull(hedgePoints); foreach (IntPoint hulls in hhull) { // convexhull 최외곽선 추출 if (hblobcount == 0) { hminX = hulls.X; hmaxX = hulls.X; hminY = hulls.Y; hmaxY = hulls.Y; } if (hminX > hulls.X) { hminX = hulls.X; } else if (hmaxX < hulls.X) { hmaxX = hulls.X; } if (hminY > hulls.Y) { hminY = hulls.Y; } else if (hmaxY < hulls.Y) { hmaxY = hulls.Y; } hblobcount++; } Drawing.Polygon(hdata, hhull, Color.White); } edge = edge.Clone(new Rectangle(hminX, hminY, hmaxX - hminX, hmaxY - hminY), myBitmap.PixelFormat); this.xray_preview.Image = edge; /////////////////////////////////// //수골 및 지골 분할 및 특징 추출 //손목 : 요골 및 척골 (2곳) //손바닥 : 제1,3,5지 중수골 (3곳) //손가락 : 제1,3,5지 기절골 및 말절골 (6곳) //손가락 : 제3. 5지 중수골 (2곳) /////////////////////////////// //요골 추출 및 인지 알고리즘 CroppedImage1 = myBitmap.Clone(new System.Drawing.Rectangle(270, 620, 250, 180), myBitmap.PixelFormat); //1. 요골 //이미지 사이즈 정규화 요골 크롭 이미지 250 X 180 -> 125 X 125 CroppedImage1 = CroppedImage1.Clone(new System.Drawing.Rectangle(10, 0, 230, 150), myBitmap.PixelFormat); width = 125; height = 125; resize = new Size(width, height); resizeImage = new Bitmap(CroppedImage1, resize); //전처리 및 특징 추출 루틴 //DetectCorners(CroppedImage1); gsImage = Grayscale.CommonAlgorithms.BT709.Apply(resizeImage); filter = new CannyEdgeDetector(); edge = filter.Apply(gsImage); //외곽선 블러링 Blur Bfilter = new Blur(); // apply the filter Bfilter.ApplyInPlace(edge); /////////////////////////// // process image with blob counter BlobCounter blobCounter = new BlobCounter(); blobCounter.ProcessImage(edge); Blob[] blobs = blobCounter.GetObjectsInformation(); // create convex hull searching algorithm GrahamConvexHull hullFinder = new GrahamConvexHull(); // lock image to draw on it BitmapData data = edge.LockBits(new Rectangle(0, 0, edge.Width, edge.Height), ImageLockMode.ReadWrite, edge.PixelFormat); // process each blob List <IntPoint> hull = new List <IntPoint> { }; List <IntPoint> edgePoints = new List <IntPoint> { }; int blobcount = 0; int minX = 0, maxX = 125, minY = 0, maxY = 125; foreach (Blob blob in blobs) { List <IntPoint> leftPoints, rightPoints; // get blob's edge points blobCounter.GetBlobsLeftAndRightEdges(blob, out leftPoints, out rightPoints); edgePoints.AddRange(leftPoints); edgePoints.AddRange(rightPoints); // blob's convex hull hull = hullFinder.FindHull(edgePoints); foreach (IntPoint hulls in hull) // convexhull 최외곽선 추출 { if (blobcount == 0) { minX = hulls.X; maxX = hulls.X; minY = hulls.Y; maxY = hulls.Y; } if (minX > hulls.X) { minX = hulls.X; } else if (maxX < hulls.X) { maxX = hulls.X; } if (minY > hulls.Y) { minY = hulls.Y; } else if (maxY < hulls.Y) { maxY = hulls.Y; } blobcount++; } Drawing.Polygon(data, hull, Color.White); } edge = resizeImage.Clone(new System.Drawing.Rectangle(minX, minY, maxX - minX, maxY - minY), myBitmap.PixelFormat); CroppedImage1.Save(@"Image\temp.jpg", ImageFormat.Jpeg); this.pB_radius.Image = edge; ///////////////////////////////////////////////////////////// //////////////////////// //척골 ////////////// CroppedImage2 = myBitmap.Clone(new System.Drawing.Rectangle(133, 620, 200, 180), myBitmap.PixelFormat); //2. 척골 //이미지 사이즈 정규화 요골 크롭 이미지 250 X 180 -> 125 X 125 CroppedImage2 = CroppedImage2.Clone(new System.Drawing.Rectangle(0, 20, 200, 150), myBitmap.PixelFormat); width = 125; height = 125; resize = new Size(width, height); resizeImage = new Bitmap(CroppedImage2, resize); //전처리 및 특징 추출 루틴 //DetectCorners(CroppedImage1); gsImage = Grayscale.CommonAlgorithms.BT709.Apply(resizeImage); filter = new CannyEdgeDetector(); edge = filter.Apply(gsImage); //외곽선 블러링 Bfilter = new Blur(); // apply the filter Bfilter.ApplyInPlace(edge); /////////////////////////// // process image with blob counter blobCounter = new BlobCounter(); blobCounter.ProcessImage(edge); blobs = blobCounter.GetObjectsInformation(); // create convex hull searching algorithm hullFinder = new GrahamConvexHull(); // lock image to draw on it BitmapData data1 = edge.LockBits(new Rectangle(0, 0, edge.Width, edge.Height), ImageLockMode.ReadWrite, edge.PixelFormat); // process each blob hull = new List <IntPoint> { }; edgePoints = new List <IntPoint> { }; blobcount = 0; minX = 0; maxX = 125; minY = 0; maxY = 125; foreach (Blob blob in blobs) { List <IntPoint> leftPoints, rightPoints; // get blob's edge points blobCounter.GetBlobsLeftAndRightEdges(blob, out leftPoints, out rightPoints); edgePoints.AddRange(leftPoints); edgePoints.AddRange(rightPoints); // blob's convex hull hull = hullFinder.FindHull(edgePoints); foreach (IntPoint hulls in hull) { // convexhull 최외곽선 추출 if (blobcount == 0) { minX = hulls.X; maxX = hulls.X; minY = hulls.Y; maxY = hulls.Y; } if (minX > hulls.X) { minX = hulls.X; } else if (maxX < hulls.X) { maxX = hulls.X; } if (minY > hulls.Y) { minY = hulls.Y; } else if (maxY < hulls.Y) { maxY = hulls.Y; } blobcount++; } Drawing.Polygon(data1, hull, Color.White); } Bitmap edge1 = resizeImage.Clone(new System.Drawing.Rectangle(minX, minY, (maxX - minX), (maxY - minY)), myBitmap.PixelFormat); this.pB_ulna.Image = edge1; /////////////////////////////////////// CroppedImage3 = myBitmap.Clone(new System.Drawing.Rectangle(390, 500, 180, 180), myBitmap.PixelFormat); //3. 제1지 중수골 resizeImage = new Bitmap(CroppedImage3, resize); this.pB_Met1.Image = CroppedImage3; CroppedImage4 = myBitmap.Clone(new System.Drawing.Rectangle(266, 266, 180, 180), myBitmap.PixelFormat); //4.제3지 기절골/중절골 resizeImage = new Bitmap(CroppedImage4, resize); this.pB_Met3.Image = CroppedImage4; CroppedImage5 = myBitmap.Clone(new System.Drawing.Rectangle(75, 335, 180, 180), myBitmap.PixelFormat); //5. 제5지 중수골/기절골 resizeImage = new Bitmap(CroppedImage5, resize); this.pB_Met5.Image = CroppedImage5; CroppedImage6 = myBitmap.Clone(new System.Drawing.Rectangle(534, 410, 180, 180), myBitmap.PixelFormat); //6. 제1지 기절골 resizeImage = new Bitmap(CroppedImage6, resize); this.pB_Pph1.Image = CroppedImage6; CroppedImage7 = myBitmap.Clone(new System.Drawing.Rectangle(266, 266, 180, 180), myBitmap.PixelFormat); //7. 제3지 기절골/중절골 resizeImage = new Bitmap(CroppedImage7, resize); this.pB_Pph3.Image = CroppedImage7; CroppedImage8 = myBitmap.Clone(new System.Drawing.Rectangle(75, 335, 180, 180), myBitmap.PixelFormat); //8. 제5지 중수골/기절골 resizeImage = new Bitmap(CroppedImage8, resize); this.pB_Pph5.Image = CroppedImage8; CroppedImage9 = myBitmap.Clone(new System.Drawing.Rectangle(260, 110, 180, 180), myBitmap.PixelFormat); //9. 제3자 중절골 resizeImage = new Bitmap(CroppedImage9, resize); this.pB_Mph3.Image = CroppedImage9; CroppedImage10 = myBitmap.Clone(new System.Drawing.Rectangle(0, 250, 180, 180), myBitmap.PixelFormat); //10. 제5지 중절골 resizeImage = new Bitmap(CroppedImage10, resize); this.pB_Mph5.Image = CroppedImage10; CroppedImage11 = myBitmap.Clone(new System.Drawing.Rectangle(620, 320, 180, 180), myBitmap.PixelFormat); //11. 제1지 말절골 resizeImage = new Bitmap(CroppedImage11, resize); this.pB_Dph1.Image = CroppedImage11; CroppedImage12 = myBitmap.Clone(new System.Drawing.Rectangle(260, 0, 180, 180), myBitmap.PixelFormat); //12. 제3지 말절골 resizeImage = new Bitmap(CroppedImage12, resize); this.pB_Dph3.Image = CroppedImage12; CroppedImage13 = myBitmap.Clone(new System.Drawing.Rectangle(0, 133, 180, 180), myBitmap.PixelFormat); //13. 제5지 말절골 resizeImage = new Bitmap(CroppedImage13, resize); this.pB_Dph5.Image = CroppedImage13; //edge.UnlockBits(data1); //edge.UnlockBits(data); }
/// <summary> Blob Detection /// This method for color object detection by Blob counter algorithm. /// If you using this method, then you can detecting as follows: /// red circle, rectangle, triangle /// blue circle, rectangle, triangle /// green circle, rectangle, triangle /// the process of this method as follow: /// 1. color filtering by Euclidean filtering(R, G, B). /// 2. the grayscale filtering based on color filtered image. /// 3. In this step, you can choose the blur option. Applied blur option(or not), /// this method donging Sobel edge filtering based on grayscale(or grayscale + blur) image. /// 4. the binary filtering based on edge filter image. /// 5. Finally, detecting object, distance from the camera and degree are expreed on picturebox 1. /// </summary> private Bitmap BlobDetection(Bitmap _bitmapSourceImage) { #region Color filtering by Euclidean filtering switch (iColorMode) { case 1: //_colorFilter.CenterColor = new RGB(230, 30, 30); iRedValue = sbRedColor.Value; iBlueValue = sbBlueColor.Value; iGreenValue = sbGreenColor.Value; _colorFilter.CenterColor = new RGB((byte)iRedValue, (byte)iGreenValue, (byte)iBlueValue); _colorFilter.Radius = (short)iRadius; _colorFilterImage = _colorFilter.Apply(_bitmapSourceImage); break; case 2: iRedValue = sbRedColor.Value; iBlueValue = sbBlueColor.Value; iGreenValue = sbGreenColor.Value; _colorFilter.CenterColor = new RGB((byte)iRedValue, (byte)iGreenValue, (byte)iBlueValue); _colorFilter.Radius = (short)iRadius; _colorFilterImage = _colorFilter.Apply(_bitmapSourceImage); break; case 3: iRedValue = sbRedColor.Value; iBlueValue = sbBlueColor.Value; iGreenValue = sbGreenColor.Value; _colorFilter.CenterColor = new RGB((byte)iRedValue, (byte)iGreenValue, (byte)iBlueValue); _colorFilter.Radius = (short)iRadius; _colorFilterImage = _colorFilter.Apply(_bitmapSourceImage); break; } #endregion Grayscale _grayscale = new Grayscale(0.2125, 0.7154, 0.0721); _bitmapGreyImage = _grayscale.Apply(_colorFilterImage); #region blur option with Edge filter //create a edge detector instance if (_blurFlag == true) { //Blur _blurfilter = new Blur(); GaussianBlur _blurfilter = new GaussianBlur(1.5); _bitmapBlurImage = _blurfilter.Apply(_bitmapGreyImage); _bitmapEdgeImage = _edgeFilter.Apply(_bitmapBlurImage); } else if (_blurFlag == false) { _bitmapEdgeImage = _edgeFilter.Apply(_bitmapGreyImage); } #endregion Threshold _threshold = new Threshold(iThreshold); _bitmapBinaryImage = _threshold.Apply(_bitmapEdgeImage); /// /// blob counter algorithm initailze. /// BlobCounter.MinWidth and MinHeight -> for the defined minimum region /// BlobCounter _blobCounter = new BlobCounter(); //Configure Filter _blobCounter.MinWidth = 70; _blobCounter.MinHeight = 70; _blobCounter.FilterBlobs = true; _blobCounter.ProcessImage(_bitmapBinaryImage); Blob[] _blobPoints = _blobCounter.GetObjectsInformation(); Graphics _g = Graphics.FromImage(_bitmapSourceImage); SimpleShapeChecker _shapeChecker = new SimpleShapeChecker(); for (int i = 0; i < _blobPoints.Length; i++) { List <IntPoint> _edgePoint = _blobCounter.GetBlobsEdgePoints(_blobPoints[i]); List <IntPoint> _corners = null; AForge.Point _center; float _radius; #region detecting Rectangle /// /// _corners: the corner of Quadrilateral /// if (_shapeChecker.IsQuadrilateral(_edgePoint, out _corners)) { //Drawing the reference point of the picturebox _g.DrawEllipse(_PictureboxPen, (float)(pictureBox1.Size.Width), (float)(pictureBox1.Size.Height), (float)10, (float)10); // Drawing setting for outline of detected object Rectangle[] _rects = _blobCounter.GetObjectsRectangles(); System.Drawing.Point[] _coordinates = ToPointsArray(_corners); Pen _pen = new Pen(Color.Blue, ipenWidth); int _x = _coordinates[0].X; int _y = _coordinates[0].Y; // Drawing setting for centroid of detected object int _centroid_X = (int)_blobPoints[0].CenterOfGravity.X; int _centroid_Y = (int)_blobPoints[0].CenterOfGravity.Y; //Drawing the centroid point of object _g.DrawEllipse(_pen, (float)(_centroid_X), (float)(_centroid_Y), (float)10, (float)10); //Degree calculation int _deg_x = (int)_centroid_X - pictureBox1.Size.Width; int _deg_y = pictureBox1.Size.Height - (int)_centroid_Y; textBox1.Text = ("Degree: (" + _deg_x + ", " + _deg_y + ")"); /// /// Drawing outline of detected object /// if (_coordinates.Length == 4) { string _shapeString = "" + _shapeChecker.CheckShapeType(_edgePoint); _g.DrawString(_shapeString, _font, _brush, _x, _y); _g.DrawPolygon(_pen, ToPointsArray(_corners)); } //size of rectange foreach (Rectangle rc in _rects) { ///for debug //System.Diagnostics.Debug.WriteLine( // string.Format("Rect size: ({0}, {1})", rc.Width, rc.Height)); iFeatureWidth = rc.Width; //check the FindDistance method. double dis = FindDistance(iFeatureWidth); _g.DrawString(dis.ToString("N2") + "cm", _font, _brush, _x, _y + 60); } } #endregion #region detecting Circle /// /// _center: the center of circle /// _radius: the radius of circle /// if (_shapeChecker.IsCircle(_edgePoint, out _center, out _radius)) { //Drawing the reference point _g.DrawEllipse(_PictureboxPen, (float)(pictureBox1.Size.Width), (float)(pictureBox1.Size.Height), (float)10, (float)10); // Drawing setting for outline of detected object Rectangle[] _rects = _blobCounter.GetObjectsRectangles(); Pen _pen = new Pen(Color.Red, ipenWidth); string _shapeString = "" + _shapeChecker.CheckShapeType(_edgePoint); int _x = (int)_center.X; int _y = (int)_center.Y; /// /// Drawing outline of detected object /// _g.DrawString(_shapeString, _font, _brush, _x, _y); _g.DrawEllipse(_pen, (float)(_center.X - _radius), (float)(_center.Y - _radius), (float)(_radius * 2), (float)(_radius * 2)); //Drawing the centroid point of object int _centroid_X = (int)_blobPoints[0].CenterOfGravity.X; int _centroid_Y = (int)_blobPoints[0].CenterOfGravity.Y; _g.DrawEllipse(_pen, (float)(_centroid_X), (float)(_centroid_Y), (float)10, (float)10); //Degree calculation int _deg_x = _centroid_X - pictureBox1.Size.Width; int _deg_y = pictureBox1.Size.Height - _centroid_Y; textBox1.Text = ("Degree: (" + _deg_x + ", " + _deg_y + ")"); //size of rectange foreach (Rectangle rc in _rects) { ///for debug //System.Diagnostics.Debug.WriteLine( // string.Format("Circle size: ({0}, {1})", rc.Width, rc.Height)); iFeatureWidth = rc.Width; double dis = FindDistance(iFeatureWidth); _g.DrawString(dis.ToString("N2") + "cm", _font, _brush, _x, _y + 60); } } #endregion #region detecting Triangle /// /// _corners: the corner of Triangle /// if (_shapeChecker.IsTriangle(_edgePoint, out _corners)) { //Drawing the reference point _g.DrawEllipse(_PictureboxPen, (float)(pictureBox1.Size.Width), (float)(pictureBox1.Size.Height), (float)10, (float)10); // Drawing setting for outline of detected object Rectangle[] _rects = _blobCounter.GetObjectsRectangles(); Pen _pen = new Pen(Color.Green, ipenWidth); string _shapeString = "" + _shapeChecker.CheckShapeType(_edgePoint); int _x = (int)_center.X; int _y = (int)_center.Y; // Drawing setting for centroid of detected object int _centroid_X = (int)_blobPoints[0].CenterOfGravity.X; int _centroid_Y = (int)_blobPoints[0].CenterOfGravity.Y; /// /// Drawing outline of detected object /// _g.DrawString(_shapeString, _font, _brush, _x, _y); _g.DrawPolygon(_pen, ToPointsArray(_corners)); //Drawing the centroid point of object _g.DrawEllipse(_pen, (float)(_centroid_X), (float)(_centroid_Y), (float)10, (float)10); //Degree calculation int _deg_x = (int)_centroid_X - pictureBox1.Size.Width; int _deg_y = pictureBox1.Size.Height - (int)_centroid_Y; textBox1.Text = ("Degree: (" + _deg_x + ", " + _deg_y + ")"); //size of rectange foreach (Rectangle rc in _rects) { ///for debug //System.Diagnostics.Debug.WriteLine( // string.Format("Triangle Size: ({0}, {1})", rc.Width, rc.Height)); iFeatureWidth = rc.Width; double dis = FindDistance(iFeatureWidth); _g.DrawString(dis.ToString("N2") + "cm", _font, _brush, _x, _y + 60); } } #endregion } return(_bitmapSourceImage); }
private void videoSourcePlayer1_NewFrame(object sender, NewFrameEventArgs args) { Invert inv = new Invert(); inv.ApplyInPlace(args.Frame); UnmanagedImage ui = UnmanagedImage.FromManagedImage(args.Frame); pictureBox1.Image = args.Frame; if (controller.Tracker.TrackingObject == null) { return; } if (controller.Tracker.TrackingObject.IsEmpty) { return; } var rect = controller.Tracker.TrackingObject.Rectangle; Crop crop = new Crop(rect); UnmanagedImage head = crop.Apply(ui); var points = new List <IntPoint>() { new IntPoint(head.Width / 2, head.Height / 2) }; var pps = head.Collect16bppPixelValues(points); double mean = pps.Mean(); double cutoff = mean + 15; Threshold t = new Threshold((int)cutoff); var mask = t.Apply(ui); LevelsLinear16bpp levels = new LevelsLinear16bpp(); levels.InGray = new IntRange((int)cutoff, 65535); levels.OutGray = new IntRange(0, 65535); levels.ApplyInPlace(ui); var mask8bit = Accord.Imaging.Image.Convert16bppTo8bpp(mask.ToManagedImage()); BlobCounter bc = new BlobCounter(); bc.ObjectsOrder = ObjectsOrder.Area; bc.ProcessImage(mask8bit); var blobs = bc.GetObjectsInformation(); inv.ApplyInPlace(args.Frame); Intersect intersect = new Intersect(); intersect.UnmanagedOverlayImage = mask; mask = intersect.Apply(ui); List <Rectangle> rects = new List <Rectangle>(); // Extract the uppermost largest blobs. for (int i = 0; i < blobs.Length; i++) { double dx = (blobs[i].Rectangle.Top - controller.Tracker.TrackingObject.Center.Y); double d = (dx * dx) / controller.Tracker.TrackingObject.Area; if (d < 2 && blobs[i].Area > 1000) { rects.Add(blobs[i].Rectangle); } } rects.Sort(compare); if (rects.Count > 0) { captureHand(mask, rects[0], pbLeftArm, pbLeftHand); } if (rects.Count > 1) { captureHand(mask, rects[1], pbRightArm, pbRightHand); } RectanglesMarker marker = new RectanglesMarker(rects); marker.MarkerColor = Color.White; marker.ApplyInPlace(mask8bit); args.Frame = mask.ToManagedImage(); }
public double[] detectionGlyph(bool CalculTailleTerrain) { bool Trouve = false; double[] ratio = new double[2] { 0, 0 }; SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); BlobCounter blobCounter = new BlobCounter(); blobCounter.MinHeight = 23; blobCounter.MinWidth = 23; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; // 4 - find all stand alone blobs blobCounter.ProcessImage(imgContour); Blob[] blobs = blobCounter.GetObjectsInformation(); // 5 - check each blob for (int i = 0, n = blobs.Length; i < n; i++) { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]); List <IntPoint> corners = null; // Test de la forme selectionnée if (shapeChecker.IsQuadrilateral(edgePoints, out corners)) { // Détection des points de coutour List <IntPoint> leftEdgePoints, rightEdgePoints, topEdgePoints, bottomEdgePoints; Line Horizontale = Line.FromPoints(new IntPoint(0, 0), new IntPoint(10, 0)); blobCounter.GetBlobsLeftAndRightEdges(blobs[i], out leftEdgePoints, out rightEdgePoints); blobCounter.GetBlobsTopAndBottomEdges(blobs[i], out topEdgePoints, out bottomEdgePoints); // calculate average difference between pixel values from outside of the // shape and from inside float diff = CalculateAverageEdgesBrightnessDifference(leftEdgePoints, rightEdgePoints, imgNB); // check average difference, which tells how much outside is lighter than // inside on the average if (diff > 20) { // Transformation de l'image reçu en un carré pour la reconnaissance QuadrilateralTransformation quadrilateralTransformation = new QuadrilateralTransformation(corners, 60, 60); UnmanagedImage glyphImage = quadrilateralTransformation.Apply(imgNB); // Filtre de contraste OtsuThreshold otsuThresholdFilter = new OtsuThreshold(); otsuThresholdFilter.ApplyInPlace(glyphImage); imgContour = glyphImage; // Reconnaissance du Glyph Glyph Gl = new Glyph(glyphImage, GlyphSize); Gl.ReconnaissanceGlyph(corners, imgNB); // Si le Glyph est valide if (Gl.getIdentifiant() > 0) { if (AutAffichage[0]) { // Coloration des contours des zones détectées UnImgReel.SetPixels(leftEdgePoints, Color.Red); UnImgReel.SetPixels(rightEdgePoints, Color.Red); UnImgReel.SetPixels(topEdgePoints, Color.Red); UnImgReel.SetPixels(bottomEdgePoints, Color.Red); } // Détection du milieu Line line = Line.FromPoints(corners[0], corners[2]); Line line2 = Line.FromPoints(corners[1], corners[3]); IntPoint intersection = (IntPoint)line.GetIntersectionWith(line2); if (AutAffichage[1]) { dessinePoint(intersection, UnImgReel, 4, Color.Yellow); } // Calcul de la rotation Line ComparasionAngle = Line.FromPoints(corners[0], corners[1]); Double rotation = (int)ComparasionAngle.GetAngleBetweenLines(Horizontale); rotation += 90 * Gl.getNbRotation(); Gl.rotation = 360 - rotation; rotation *= (Math.PI / 180.0); // Calcul d'un point en bout de pince float Taille = corners[0].DistanceTo(corners[1]); float taille = (Taille / BibliotequeGlyph.Biblioteque[Gl.getPosition()].taille) * BibliotequeGlyph.Biblioteque[Gl.getPosition()].DistancePince; int x = -(int)(System.Math.Sin(rotation) * taille); int y = -(int)(System.Math.Cos(rotation) * taille); x += (int)intersection.X; y += (int)intersection.Y; Gl.Position = new int[2] { x, y }; if (AutAffichage[2]) { dessinePoint(new IntPoint(x, y), UnImgReel, 4, Color.Cyan); } imgContour = Gl.getImage(); addGlyph(Gl); if (CalculTailleTerrain == true && Trouve == false) { Trouve = true; int tailleglyph = BibliotequeGlyph.Biblioteque[Gl.getPosition()].taille; // Pythagore pour detection taille Rectangle a = blobs[i].Rectangle; double angle = -Gl.rotation + 180; List <IntPoint> coins = new List <IntPoint>(); coins.Add(new IntPoint(100, 100)); coins.Add(new IntPoint(100, 100 + tailleglyph)); coins.Add(new IntPoint(100 + tailleglyph, 100 + tailleglyph)); coins.Add(new IntPoint(100 + tailleglyph, 100)); IntPoint Centre = new IntPoint((coins[2].X + coins[0].X) / 2, (coins[2].Y + coins[0].Y) / 2); int radius = (int)(0.5 * Math.Sqrt(coins[0].DistanceTo(coins[1]) * coins[0].DistanceTo(coins[1]) + coins[1].DistanceTo(coins[2]) * coins[1].DistanceTo(coins[2]))); double alpha = Math.Atan2(coins[0].DistanceTo(coins[1]), coins[1].DistanceTo(coins[2])) * (180 / Math.PI); double ang = 0; for (i = 0; i < 4; i++) { IntPoint tmp = coins[i]; switch (i) { case 0: ang = alpha - 180 + angle; break; case 1: ang = +angle - alpha; break; case 2: ang = +angle + alpha; break; case 3: ang = -alpha + 180 + angle; break; } ang *= (Math.PI / 180); tmp.X = (int)(Centre.X + radius * Math.Cos(ang)); tmp.Y = (int)(Centre.Y + radius * Math.Sin(ang)); coins[i] = tmp; } Rectangle r = new Rectangle(min(coins[0].X, coins[1].X, coins[2].X, coins[3].X), min(coins[0].Y, coins[1].Y, coins[2].Y, coins[3].Y), max(coins[0].X, coins[1].X, coins[2].X, coins[3].X) - min(coins[0].X, coins[1].X, coins[2].X, coins[3].X), max(coins[0].Y, coins[1].Y, coins[2].Y, coins[3].Y) - min(coins[0].Y, coins[1].Y, coins[2].Y, coins[3].Y)); ratio[0] = ((double)r.Width / (double)a.Width) * 1.48; ratio[1] = ((double)r.Height / (double)a.Height) * 1.48; } } } } } if (Trouve == false || ratio[0] == 0 || ratio[0] == 1 || ratio[1] == 0 || ratio[1] == 1) { return(null); } ratio[0] *= 0.7; ratio[1] *= 0.7; return(ratio); }
private void vspMain_NewFrame(object sender, ref Bitmap image) { cntFrame++; if (cntFrame < 80) { return; } colorFiltering.Red = new Accord.IntRange(230, 245); colorFiltering.Green = new Accord.IntRange(200, 220); colorFiltering.Blue = new Accord.IntRange(150, 180); var filteringFrame = colorFiltering.Apply(image); var afterGray = grayscaleBT709.Apply(filteringFrame); var thersholdedFrame = threshold.Apply(afterGray); pbThreshold.Image = afterGray; var dilatedFrame = dilation.Apply(thersholdedFrame); ApplyMask applyMask = new ApplyMask(dilatedFrame); var maskedFrame = applyMask.Apply(image); var blobFrame = maskedFrame.Clone() as Bitmap; pbMasked.Image = maskedFrame; blobCounter.ProcessImage(blobFrame); var info = blobCounter.GetObjectsInformation(); var rectangles = info.Where(w => w.Area >= 200).Select(s => s.Rectangle).ToList(); //var rectangles = blobCounter.GetObjectsRectangles(); if (rectangles.Count == 0) { missFrame++; return; } var rectangle = rectangles[0]; lstHeight.Add(rectangle.Y); if (lstHeight.Count >= 2) { lstHeight.Remove(0); } //if(cntFrame >=130) // lastRect = rectangle; //rectangle = rectangles.Where(w => Math.Abs(lastRect.X - w.X) < 20).FirstOrDefault(); if (rectangle == null) { return; } Graphics g = Graphics.FromImage(image); rectangle.Width = 50; rectangle.Height = 100; //rectangle.Y = lstHeight.Sum(s => s) / lstHeight.Count; g.DrawRectangle(new Pen(Color.Yellow, 3), rectangle); var img = UnmanagedImage.FromManagedImage(blobFrame); camshift.SearchWindow = rectangle; camshift.ProcessFrame(img); var trackRect = camshift.TrackingObject.Rectangle; if (trackRect == null) { return; } trackRect.Inflate(30, 30); g.DrawRectangle(new Pen(Color.Aqua, 3), trackRect); }
public void FindContourTest2() { Bitmap bmp = Properties.Resources.hand2; BlobCounter bc = new BlobCounter(bmp); bc.ObjectsOrder = ObjectsOrder.Size; Blob[] blobs = bc.GetObjectsInformation(); bc.ExtractBlobsImage(bmp, blobs[0], true); List<IntPoint> expected = bc.GetBlobsEdgePoints(blobs[0]); Bitmap blob = blobs[0].Image.ToManagedImage(); BorderFollowing bf = new BorderFollowing(); List<IntPoint> actual = bf.FindContour(blob); foreach (IntPoint point in expected) Assert.IsTrue(actual.Contains(point)); IntPoint prev = actual[0]; for (int i = 1; i < actual.Count; i++) { IntPoint curr = actual[i]; Assert.IsTrue(System.Math.Abs(prev.X - curr.X) <= 1 && System.Math.Abs(prev.Y - curr.Y) <= 1); prev = curr; } IntPoint first = actual[0]; IntPoint last = actual[actual.Count - 1]; Assert.IsTrue(System.Math.Abs(first.X - last.X) <= 1 && System.Math.Abs(first.Y - last.Y) <= 1); }
/// <summary> /// Detects and recognizes cards from source image /// </summary> /// <param name="source">Source image to be scanned</param> /// <returns>Recognized Cards</returns> public CardCollection Recognize(Bitmap source, string filePath, int id, int minSize, Rectangle suitRect, Rectangle rankRect ) { CardCollection collection = new CardCollection(); //Collection that will hold cards Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image FiltersSequence seq = new FiltersSequence(); seq.Add(Grayscale.CommonAlgorithms.BT709); //First add grayScaling filter seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter temp = seq.Apply(source); // Apply filters on source image //if (!string.IsNullOrEmpty(fileName)) //{ // temp.Save(fileName, ImageFormat.Bmp); //} //Extract blobs from image whose size width and height larger than 150 BlobCounter extractor = new BlobCounter(); extractor.FilterBlobs = true; extractor.MinWidth = extractor.MinHeight = minSize;//TODO card size //extractor.MaxWidth = extractor.MaxHeight = 70;//TODO card size extractor.ProcessImage(temp); //Will be used transform(extract) cards on source image //QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation(); foreach (Blob blob in extractor.GetObjectsInformation()) { var cardImg = source.Clone(blob.Rectangle, PixelFormat.DontCare); Card card = new Card(cardImg); //Create Card Object Bitmap suitBmp = card.GetPart(suitRect); char color = ScanColor(suitBmp); //Scan color seq.Clear(); seq.Add(Grayscale.CommonAlgorithms.BT709); seq.Add(new OtsuThreshold()); suitBmp = seq.Apply(suitBmp); card.Suit = ScanSuit(suitBmp, color); //Scan suit of face card Bitmap rankBmp = card.GetPart(rankRect); seq.Clear(); seq.Add(Grayscale.CommonAlgorithms.BT709); seq.Add(new OtsuThreshold()); rankBmp = seq.Apply(rankBmp); //var ext = new BlobsFiltering(0, 0, 40, 40); //ext.ApplyInPlace(rankBmp); card.Rank = ScanRank(rankBmp); //Scan Rank of non-face card //if (card.Rank == Rank.NOT_RECOGNIZED) //{ // if (!string.IsNullOrEmpty(filePath)) // { // while (File.Exists(filePath + id + ".bmp")) // id++; // top.Save(filePath + id + ".bmp", ImageFormat.Bmp); // } //} if (card.Rank != Rank.NOT_RECOGNIZED && card.Suit != Suit.NOT_RECOGNIZED) { collection.Add(card); //Add card to collection } } collection.SortByRank(); return(collection); }
static void Main(string[] args) { Threshold thresh = new Threshold(10); Median median = new Median(9); Erosion3x3 erode = new Erosion3x3(); Dilatation3x3 dilate = new Dilatation3x3(); GrahamConvexHull hullFinder = new GrahamConvexHull(); ConnectedComponentsLabeling ccLabeler = new ConnectedComponentsLabeling(); BorderFollowing contourFinder = new BorderFollowing(); GrayscaleToRGB rgb = new GrayscaleToRGB(); ConvexHullDefects defectFinder = new ConvexHullDefects(10); Bitmap img = (Bitmap)Bitmap.FromFile("hand3.jpg"); Bitmap image = Grayscale.CommonAlgorithms.BT709.Apply(img); thresh.ApplyInPlace(image); //median.ApplyInPlace(image); erode.ApplyInPlace(image); dilate.ApplyInPlace(image); BlobCounter counter = new BlobCounter(image); counter.ObjectsOrder = ObjectsOrder.Area; Blob[] blobs = counter.GetObjectsInformation(); if (blobs.Length > 0) { counter.ExtractBlobsImage(image, blobs[0], true); UnmanagedImage hand = blobs[0].Image; var contour = contourFinder.FindContour(hand); if (contour.Count() > 0) { var initialHull = hullFinder.FindHull(contour); var defects = defectFinder.FindDefects(contour, initialHull); var filteredHull = initialHull.ClusterHullPoints().FilterLinearHullPoints(); var palmCenter = defects.Centroid(contour); var wristPoints = filteredHull.SelectWristPoints(defects, contour); Bitmap color = rgb.Apply(hand).ToManagedImage(); //BitmapData data = color.LockBits(new Rectangle(0, 0, color.Width, color.Height), ImageLockMode.ReadWrite, color.PixelFormat); //Drawing.Polyline(data, contour, Color.Blue); //Drawing.Polygon(data, filteredHull, Color.Red); //color.UnlockBits(data); Graphics gr = Graphics.FromImage(color); gr.DrawPolygon(new Pen(Brushes.Red, 3), filteredHull.ToPtArray()); gr.DrawLines(new Pen(Brushes.Blue, 3), contour.ToPtArray()); gr.DrawEllipse(new Pen(Brushes.Red, 3), palmCenter.X - 10, palmCenter.Y - 10, 20, 20); foreach (ConvexityDefect defect in defects) { gr.DrawEllipse(new Pen(Brushes.Green, 6), contour[defect.Point].X - 10, contour[defect.Point].Y - 10, 20, 20); } foreach (AForge.IntPoint pt in filteredHull) { gr.DrawEllipse(new Pen(Brushes.Yellow, 6), pt.X - 10, pt.Y - 10, 20, 20); } foreach (AForge.IntPoint pt in wristPoints) { gr.DrawEllipse(new Pen(Brushes.PowderBlue, 6), pt.X - 10, pt.Y - 10, 20, 20); } ImageBox.Show(color); } } }
public void nesnebul(Bitmap image) { BlobCounter blobCounter = new BlobCounter(); blobCounter.MinWidth = 5; blobCounter.MinHeight = 5; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721); UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData)); image.UnlockBits(objectsData); blobCounter.ProcessImage(image); Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs = blobCounter.GetObjectsInformation(); pictureBox2.Image = image; foreach (Rectangle recs in rects) { if (rects.Length > 0) { Rectangle objectRect = rects[0]; //Graphics g = Graphics.FromImage(image); Graphics g = pictureBox1.CreateGraphics(); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2)) { g.DrawRectangle(pen, objectRect); } //Cizdirilen Dikdörtgenin Koordinatlari aliniyor. int objectX = objectRect.X + (objectRect.Width / 2); int objectY = objectRect.Y + (objectRect.Height / 2); // g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1)); g.Dispose(); if (objectX < 108 && objectY < 90) { ardino.Write("1"); } else if ((objectX > 108 && objectX < 212) && (objectY < 90)) { ardino.Write("2"); } else if ((objectX > 212 && objectX < 360) && (objectY < 90)) { ardino.Write("3"); } else if ((objectX < 108) && (objectY > 90 && objectY < 185)) { ardino.Write("4"); } else if ((objectX > 108 && objectX < 212) && (objectY > 90 && objectY < 185)) { ardino.Write("5"); } else if ((objectX > 212 && objectX < 360) && (objectY > 90 && objectY < 185)) { ardino.Write("6"); } else if ((objectX < 108) && (objectY > 185 && objectY < 275)) { ardino.Write("7"); } else if ((objectX > 108 && objectX < 212) && (objectY > 185 && objectY < 275)) { ardino.Write("8"); } else if ((objectX > 212 && objectX < 360) && (objectY > 185 && objectY < 275)) { ardino.Write("9"); } } } }
/// <summary> /// Process the filter on the specified image. /// </summary> /// /// <param name="image">Source image data.</param> /// protected override unsafe void ProcessFilter( UnmanagedImage image ) { int width = image.Width; int height = image.Height; // 1 - invert the source image Invert invertFilter = new Invert( ); UnmanagedImage invertedImage = invertFilter.Apply( image ); // 2 - use blob counter to find holes (they are white objects now on the inverted image) BlobCounter blobCounter = new BlobCounter( ); blobCounter.ProcessImage( invertedImage ); Blob[] blobs = blobCounter.GetObjectsInformation( ); // 3 - check all blobs and determine which should be filtered byte[] newObjectColors = new byte[blobs.Length + 1]; newObjectColors[0] = 255; // don't touch the objects, which have 0 ID for ( int i = 0, n = blobs.Length; i < n; i++ ) { Blob blob = blobs[i]; if ( ( blob.Rectangle.Left == 0 ) || ( blob.Rectangle.Top == 0 ) || ( blob.Rectangle.Right == width ) || ( blob.Rectangle.Bottom == height ) ) { newObjectColors[blob.ID] = 0; } else { if ( ( ( coupledSizeFiltering ) && ( blob.Rectangle.Width <= maxHoleWidth ) && ( blob.Rectangle.Height <= maxHoleHeight ) ) | ( ( !coupledSizeFiltering ) && ( ( blob.Rectangle.Width <= maxHoleWidth ) || ( blob.Rectangle.Height <= maxHoleHeight ) ) ) ) { newObjectColors[blob.ID] = 255; } else { newObjectColors[blob.ID] = 0; } } } // 4 - process the source image image and fill holes byte* ptr = (byte*) image.ImageData.ToPointer( ); int offset = image.Stride - width; int[] objectLabels = blobCounter.ObjectLabels; for ( int y = 0, i = 0; y < height; y++ ) { for ( int x = 0; x < width; x++, i++, ptr++ ) { *ptr = newObjectColors[objectLabels[i]]; } ptr += offset; } }
/* Metoda, která dostane na vstup název obrazku na kterém má najít modrou misku a vrátí souøadnici levého horního rohu, šíøku a výšku. */ public static int[] DetekujModryOkrajObrazku(string obrazek) { /* Obrázek naètu dvakrát - na prvním provádím úpravy a druhý pak oøežu a uložím. */ Bitmap image = (Bitmap)Bitmap.FromFile(obrazek); Bitmap puvodniImage = (Bitmap)Bitmap.FromFile(obrazek); /* Definice pomocných promìnných: */ int[] souradniceRozmeryOrezu = new int[4]; int levyHorniX = 0; int levyHorniY = 0; /* Definice rozostøovacího filtru */ GaussianBlur filterBlur = new GaussianBlur(4, 11); /* Definice modrého filtru */ ColorFiltering filterBlue = new ColorFiltering(); filterBlue.Red = new IntRange(18, 35); filterBlue.Green = new IntRange(23, 40); filterBlue.Blue = new IntRange(40, 85); /* Definice hnìdého filtru */ ColorFiltering filterBrown = new ColorFiltering(); filterBrown.Red = new IntRange(35, 45); filterBrown.Green = new IntRange(32, 42); filterBrown.Blue = new IntRange(30, 40); /* Aplikace modrého filtru - vytáhne mi z pùvodního obrázku modrou barvu. */ filterBlue.ApplyInPlace(image); /* Na vytáhnutou modrou vrstvu aplikuji rozostøení abych v nìm mohl vyhledat obdélník. */ filterBlur.ApplyInPlace(image); /* Definice vyhledáváèe obdelníkù. */ BlobCounter bc = new BlobCounter(); bc.FilterBlobs = true; bc.MinHeight = 500; bc.MinWidth = 500; /* Aplikce vyhledávaèe obdelníkù. */ bc.ProcessImage(image); Blob[] blobs = bc.GetObjectsInformation(); SimpleShapeChecker shapeChecker = new SimpleShapeChecker(); foreach (var blob in blobs) { List <IntPoint> edgePoints = bc.GetBlobsEdgePoints(blob); List <IntPoint> cornerPoints; /* kontrola jestli nalezený obdelník je ctyruhelnik a do pomocné promìnné doplnìní jeho rohových bodù. */ if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints)) { /* Kontrola jestli nalezený obdelník je fakt obdelník. */ if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle) { /* procházím nalezené souøadnice a vytvoøím si z nich kolekci bodù, podle kterých pak oøežu modrý obdelník. */ List <System.Drawing.Point> Points = new List <System.Drawing.Point>(); foreach (var point in cornerPoints) { Points.Add(new System.Drawing.Point(point.X, point.Y)); } /* Tady zpracuju rohove body a vypocitam si vzdalenosti */ int sirkaModrehoObdelnika = (Points[2].X - Points[0].X); int vyskaModrehoObdelnika = (Points[3].Y - Points[1].Y); levyHorniX = Points[0].X; levyHorniY = Points[1].Y; /* Oøežu pùvodní obrázek podle získaných rozmìrù modrého okraje. */ puvodniImage = puvodniImage.Clone(new Rectangle(levyHorniX, levyHorniY, sirkaModrehoObdelnika, vyskaModrehoObdelnika), puvodniImage.PixelFormat); /* Upravovaný obrázek nahradím oøezem pùvodního - chystám se na nìm hledat ještì šedou misku, kterou to oøežu ještì lépe. */ image = puvodniImage.Clone(new Rectangle(0, 0, puvodniImage.Width, puvodniImage.Height), puvodniImage.PixelFormat); } } } /* Teï jsem ve fázi kdy v promìnné image mame oøezaný obrázek v pùvodním tvaru. * Aplikuji hnedý filtr a jedu to co s modrou, */ filterBrown.ApplyInPlace(image); filterBlur.ApplyInPlace(image); bc.FilterBlobs = true; bc.MinHeight = 500; bc.MinWidth = 500; bc.ProcessImage(image); blobs = bc.GetObjectsInformation(); shapeChecker = new SimpleShapeChecker(); foreach (var blob in blobs) { List <IntPoint> edgePoints = bc.GetBlobsEdgePoints(blob); List <IntPoint> cornerPoints; if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints)) { if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle) { List <System.Drawing.Point> Points = new List <System.Drawing.Point>(); foreach (var point in cornerPoints) { Points.Add(new System.Drawing.Point(point.X, point.Y)); } /* Tady zpracuju rohový bod a vypoèítám rozmìry. */ souradniceRozmeryOrezu[0] = Points[0].X + levyHorniX; souradniceRozmeryOrezu[1] = Points[1].Y + levyHorniY; souradniceRozmeryOrezu[2] = Points[2].X - Points[0].X; souradniceRozmeryOrezu[3] = Points[3].Y - Points[1].Y; } } } /* Vrátím získané hodnoty * 1. pozice - levé horní souøadnice X. * 2. pozice - levá horní souøadnice Y. * 3. pozice - šíøka oøezu. * 4. pozice - výška oøezu. */ return(souradniceRozmeryOrezu); }