使用C#查找图像中的黑色方块/矩形

时间:2015-06-19 15:45:14

标签: c# image-processing aforge

我想在扫描表中找到小的黑色方块/矩形:

  1. 根据需要调整图像。
  2. 删除白页边距。
  3. 输入图片 sample image 1 http://us.cdn.persiangig.com/preview/ii2jf6/2.jpg

    输出图片 sample image 2 http://us.cdn.persiangig.com/preview/9ntpnc/1.jpg

    我找到方格的代码是:

    Bitmap pic =(Bitmap) pictureBox1.Image;
    
    // create filter
    AForge.Imaging.Filters.Median Medianfilter = new AForge.Imaging.Filters.Median();
    // apply the filter
    Medianfilter.ApplyInPlace(pic);
    
    Bitmap grayImage;
    
    if (pic.PixelFormat != PixelFormat.Format16bppGrayScale && pic.PixelFormat != PixelFormat.Format8bppIndexed)
    {
        AForge.Imaging.Filters.Grayscale grayscalefilter = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721);
        grayImage = grayscalefilter.Apply((Bitmap)pictureBox1.Image);
    }
    else
    {
        grayImage = pic;
    }
    
    // black & white:
    Threshold(ref grayImage, Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox1.Text));
    
    // invert filter
    Invert invertfilter = new Invert();
    // apply the filter
    invertfilter.ApplyInPlace(grayImage);
    
    // Edge Detector  filter
    DifferenceEdgeDetector EdgeDetectorfilter = new DifferenceEdgeDetector();
    // apply the filter
    EdgeDetectorfilter.ApplyInPlace(grayImage);
    
    // create filter
    Dilatation Dilatationfilter = new Dilatation();
    // apply the filter
    Dilatationfilter.ApplyInPlace(grayImage);
    

    在图像中查找对象(方形/矩形):

            // lock image
            BitmapData bitmapData = grayImage.LockBits(new Rectangle(0, 0, grayImage.Width, grayImage.Height),
            ImageLockMode.ReadWrite, grayImage.PixelFormat);
    
            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();
    
            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = 10;          //*-*-*-*-
            blobCounter.MinWidth = 10;
            blobCounter.MaxHeight = 50;
            blobCounter.MaxWidth = 50;
    
            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            grayImage.UnlockBits(bitmapData);
    
            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
    
            Graphics g = Graphics.FromImage(pic);
            Pen redPen = new Pen(Color.Red, 2);       // quadrilateral
            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]);
    
                List<IntPoint> corners;
    
                // is triangle or quadrilateral
                if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                {
                    // get sub-type
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
    
                    Pen pen;
    
                    if (subType == PolygonSubType.Square)
                    {
                        pen = (corners.Count == 4) ? bluePen : redPen;
                        g.DrawPolygon(pen, ToPointsArray(corners));
                    }
                }
            }
            redPen.Dispose();
            bluePen.Dispose();
            g.Dispose();
            pictureBox1.Image = pic;
    

    问题是检测正方形和矩形的准确度很低!!!

    我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果你可以使用OpenCV,这很容易。使用霍夫变换并在输出中找到峰值。这些对应于输入中的直线。

如果您无法使用OpenCV,则需要自行实施。这是让你入门的东西。

https://en.wikipedia.org/?title=Hough_transform

修改

正如Anders Gustafsson在下面的评论中所指出的,Hough变换可以在AForge for .NET中使用,因此自己实现它并不是必需的。

http://www.aforgenet.com/framework/features/hough_transformation.html