How to draw a rectangles around a shape

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    Shape
AI Thread Summary
The discussion revolves around the challenge of drawing red rectangles around black and white shapes in an image using C#. The user has attempted two algorithms that do not run in real time and seeks advice on achieving this task efficiently. Suggestions include drawing rectangles immediately after shape creation, although the user clarified that there is no initial drawing involved. For real-time segmentation, image processing techniques are recommended, particularly using OpenCV, a widely recognized computer vision library. Participants suggest loading the image into a 2D byte array and analyzing pixel patterns to determine where to place the rectangles. The conversation highlights the importance of identifying the characteristics of the shapes, such as whether they overlap or if tilted rectangles are acceptable, as these factors significantly influence the complexity of the solution. The flood fill algorithm is mentioned as a potential method for identifying outlines of shapes, which could simplify the rectangle-drawing process if the shapes are distinct and have solid borders.
Superposed_Cat
Messages
388
Reaction score
5
Hey all, as you can see in the below images i have an image that contains many shapes, all black and white. I need each shapes to have a rectangle drawn around it.In image pf1.png it illustrates the shapes before being fenced in red rectangles, pf2.png shows the shapes after being rectangled. I have tried 2 algorithms to try this but none run in real time, how could i achieve this? Any help apreciated
 

Attachments

  • pf1.png
    pf1.png
    429 bytes · Views: 441
  • pf2.png
    pf2.png
    531 bytes · Views: 460
Technology news on Phys.org
It's hard to know what language construct you happen to be using. You should let people know so they can give unambiguous help.

Perhaps when you initially draw each shape, have the red box drawn around that shape immediately after its creation. That way you can set the relative position of the box according to the last shape drawn.
 
Zondrina said:
It's hard to know what language construct you happen to be using. You should let people know so they can give unambiguous help.

Perhaps when you initially draw each shape, have the red box drawn around that shape immediately after its creation. That way you can set the relative position of the box according to the last shape drawn.
there is no initial drawing.
Im using c#
 
You might have to do some image processing then.

Suppose any N-gon is allowed to be enclosed within a red rectangle and is drawn in black. Suppose further the background of the image is always white.

Load the image into a 2D byte array, and loop over the image pixel by pixel. You will need to recognize particular patterns of pixels, which will inform you whether or not you need to place a red pixel or a series of red pixels in the surrounding area.

I am unsure what patterns you will need to find because I don't know what N-gons are allowed (a hexagon for example would prove more challenging than a simple rectangle).
 
Ohkay let me rephrase my problem, I need to segment an image in real time, how would i do that?
 
Hey, first of all, for general real world purposes, the library you need to be working with is opencv, the open computer vision library:
http://opencv.org/
For this particular problem though, if your images look like that, its easier: merely about finding the smallest rectangles without white pixels in them. I can't immediately see the algorithm to do it, but I don't think its too hard. For instance with closed shapes with solid lines, think about a line scanning horizontally then vertically, and the way that intersects with both lines to define the area of the space.
 
If all your images are as simple as that, i.e. non overlapping shapes with completely black border, you can use a flood fill algorithm to find all the black outlines. After all your outlines are actually areas.
https://en.wikipedia.org/wiki/Flood_fill
 
Tell us some more. Can the rectangles be tilted? Can a rectangle enclose multiple shapes? Can the rectangles overlap? Just identifying disjoint shapes can be a tough problem.
 
Back
Top