# How to draw a rectangles around a shape

• Superposed_Cat
In summary, the conversation discusses the problem of segmenting an image in real time and suggests using the open computer vision library, OpenCV. It also suggests using a flood fill algorithm to find the black outlines of the shapes in the image. However, more information is needed about the specifics of the shapes and their arrangement in the image in order to provide a more specific solution.
Superposed_Cat
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
429 bytes · Views: 405
• pf2.png
531 bytes · Views: 429
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.

## 1. How do I draw a rectangle around a shape in a drawing software?

To draw a rectangle around a shape in a drawing software, you can use the "Rectangle" tool or the "Rectangle Selection" tool. Simply click and drag your cursor to create a rectangle around the shape you want to enclose.

## 2. Can I draw a rectangle around a shape without a drawing software?

Yes, you can draw a rectangle around a shape without a drawing software by using a ruler and a pencil. Place the ruler along the edges of the shape and use the ruler as a guide to draw straight lines to create a rectangle around the shape.

## 3. How do I ensure that the rectangle I draw is perfectly aligned with the shape?

To ensure that the rectangle you draw is perfectly aligned with the shape, you can use the "Snap to Point" or "Snap to Grid" feature in your drawing software. This will help you align the corners of the rectangle with the corners of the shape.

## 4. Can I customize the thickness or color of the rectangle I draw?

Yes, most drawing software allows you to customize the thickness and color of the rectangle you draw. You can usually find these options in the "Stroke" or "Outline" settings of the shape or object.

## 5. How can I draw a rectangle around a shape that is not a perfect square or circle?

To draw a rectangle around a shape that is not a perfect square or circle, you can use the "Freeform" or "Polygon" tool in your drawing software. These tools allow you to draw a rectangle with custom angles and sides to fit the shape you want to enclose.

• General Math
Replies
20
Views
1K
• Biology and Medical
Replies
7
Views
2K
• Programming and Computer Science
Replies
1
Views
1K
• Classical Physics
Replies
2
Views
870
• Earth Sciences
Replies
42
Views
1K
• General Math
Replies
45
Views
3K
• Programming and Computer Science
Replies
2
Views
949
• General Math
Replies
8
Views
2K
• Electromagnetism
Replies
3
Views
929
• Introductory Physics Homework Help
Replies
13
Views
2K