17

I'm looking to detect and get a array of Rects, one for each rectangle, in the image below. How might I do this in c#?

Basically I'm trying to scan the image taken of the screen and parse the array of windows.

Rect being some form of (xloc,yloc,xsize,ysize) Returned array: rectangles = ParseRects(image);

Image

2
  • Nice EVE Online screenshot you have there ;) Commented Nov 10, 2016 at 14:10
  • @PicMickael ;). Commented Nov 11, 2016 at 3:03

2 Answers 2

28

Your best option will be to use the AForge.Net library.

The following code is derived from the documentation for the ShapeChecker class, and you may want to look at the documentation to further familiarize yourself.

static void Main(string[] args)
{
    // Open your image
    string path = "test.png";
    Bitmap image = (Bitmap)Bitmap.FromFile(path);

    // locating objects
    BlobCounter blobCounter = new BlobCounter();

    blobCounter.FilterBlobs = true;
    blobCounter.MinHeight = 5;
    blobCounter.MinWidth = 5;

    blobCounter.ProcessImage(image);
    Blob[] blobs = blobCounter.GetObjectsInformation();

    // check for rectangles
    SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

    foreach (var blob in blobs)
    {
        List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
        List<IntPoint> cornerPoints;

        // use the shape checker to extract the corner points
        if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
        {
            // only do things if the corners form a rectangle
            if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
            {
                // here i use the graphics class to draw an overlay, but you
                // could also just use the cornerPoints list to calculate your
                // x, y, width, height values.
                List<Point> Points = new List<Point>();
                foreach (var point in cornerPoints)
                {
                    Points.Add(new Point(point.X, point.Y));
                }

                Graphics g = Graphics.FromImage(image);
                g.DrawPolygon(new Pen(Color.Red, 5.0f), Points.ToArray());

                image.Save("result.png");
            }
        }
    }
}

The original input: original input

The resultant image:enter image description here

5
  • Hey! I actually managed to get almost this result, using the same program. I'm accepting your answer but I'd like to get more input if you will give it, do you think filtering out all color but the range the windows are would be better? I need to introduce a random background behind it and have it still be able to recognize ONLY the windows. What are your thoughts? Commented May 10, 2011 at 18:04
  • 3
    @Christian I'll be perfectly honest: random backgrounds are going to be hard. You can probably use the blob detection in my answer, but background subtraction is a science in itself. I'm guessing this random background is animated, so I would take two frames, and XOR them to get a static image to use. Commented May 11, 2011 at 0:20
  • @GregBuehler : BlobCounter class is coming from some Dll? if so, please share the dll location..Thanks.
    – Chamaququm
    Commented Nov 27, 2013 at 13:51
  • BlobCounter is in the AForge.Imaging namespace. See: aforgenet.com/framework/docs/html/… Commented Nov 27, 2013 at 19:20
  • 2
    For anyone finding that AForge is looking a bit out-of-date, it has been incorporated into the Accord Machine Learning framework: accord-framework.net
    – Kohanz
    Commented Oct 25, 2018 at 15:41
0

You can see the line Detection by Hough transformation and Corner detection. Some algorithms have probably already been implemented in C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.