简体   繁体   English

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

[英]Find Black Squares/Rectangles in Image with C#

I want to find small black squares/rectangles in scanned sheet to: 我想在扫描的纸张中找到小的黑色方块/矩形:

  1. deskewing image if needed. 如果需要,歪斜图像。
  2. remove white page margin. 删除白页边距。

Input image: sample image 1 http://us.cdn.persiangig.com/preview/ii2jf6/2.jpg 输入图像: 示例图像1 http://us.cdn.persiangig.com/preview/ii2jf6/2.jpg

Output image: sample image 2 http://us.cdn.persiangig.com/preview/9ntpnc/1.jpg 输出图像: 样本图像2 http://us.cdn.persiangig.com/preview/9ntpnc/1.jpg

My code to find square is: 我找到square的代码是:

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);

Finding object(square/rectangle) in image: 在图像中查找对象(方形/矩形):

        // 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;

Problem is low accuracy on detecting squares and rectangles!!! 问题是检测正方形和矩形的准确度低!!!

How can I solve this problem? 我怎么解决这个问题?

If you can use OpenCV this is quite easy. 如果你可以使用OpenCV,这很容易。 Use the Hough transform and find peaks in the output. 使用霍夫变换并在输出中找到峰值。 Those correspond to straight lines in your input. 这些对应于输入中的直线。

If you can't use OpenCV, you'll need to implement it yourself. 如果你不能使用OpenCV,你需要自己实现它。 Here's something to get you started. 这是让你入门的东西。

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

EDIT 编辑

As Anders Gustafsson points out in the comment below, the Hough Transform is available in AForge for .NET so implementing it yourself isn't a necessity. 正如Anders Gustafsson在下面的评论中指出的那样,Hough变换可以在AForge for .NET中使用,因此自己实现它并不是必需的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM