How to draw a rectangles around a shape

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    Shape
Click For Summary

Discussion Overview

The discussion revolves around the challenge of drawing rectangles around shapes in images, specifically focusing on real-time image segmentation techniques. Participants explore various algorithms and programming approaches to achieve this task using C# and image processing libraries.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks assistance with drawing rectangles around shapes in images and mentions that previous algorithms did not run in real time.
  • Another participant suggests providing details about the programming language being used to facilitate clearer assistance.
  • A participant proposes drawing the rectangle immediately after creating each shape, although they later clarify that there is no initial drawing involved.
  • Image processing techniques are recommended, including loading the image into a 2D byte array and recognizing pixel patterns to determine where to place rectangles.
  • One suggestion involves using the OpenCV library for image processing, highlighting the need to find the smallest rectangles that enclose shapes without white pixels.
  • A flood fill algorithm is mentioned as a potential method for identifying black outlines of shapes in simple images.
  • Questions are raised about the characteristics of the rectangles, such as whether they can be tilted, if they can enclose multiple shapes, and if they can overlap, indicating the complexity of the problem.

Areas of Agreement / Disagreement

Participants express various approaches and techniques, but there is no consensus on a single method or solution. Multiple competing views and uncertainties about the specifics of the problem remain evident.

Contextual Notes

Limitations include the lack of clarity on the types of shapes involved, the specific requirements for the rectangles, and the absence of a defined algorithm that works in real time.

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: 464
  • pf2.png
    pf2.png
    531 bytes · Views: 482
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.
 

Similar threads

Replies
20
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 42 ·
2
Replies
42
Views
8K
  • · Replies 45 ·
2
Replies
45
Views
5K
  • · Replies 5 ·
Replies
5
Views
7K