Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to draw a rectangles around a shape

  1. Jun 28, 2015 #1
    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

    Attached Files:

    • pf1.png
      File size:
      427 bytes
    • pf2.png
      File size:
      528 bytes
  2. jcsd
  3. Jun 28, 2015 #2


    User Avatar
    Homework Helper

    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.
  4. Jun 28, 2015 #3
    there is no initial drawing.
    Im using c#
  5. Jun 28, 2015 #4


    User Avatar
    Homework Helper

    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).
  6. Jun 28, 2015 #5
    Ohkay let me rephrase my problem, I need to segment an image in real time, how would i do that?
  7. Jun 28, 2015 #6
    Hey, first of all, for general real world purposes, the library you need to be working with is opencv, the open computer vision library:
    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.
  8. Jun 28, 2015 #7
    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.
  9. Jun 28, 2015 #8


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook