Question

I have polygon given by the points sequence I need to apply follow rules:

  • angles must be 180,90,45 degrees;
  • if lines parallel then distanse beetwen the lines greater then minValue;
  • orientation of polygon is horizontal and vertical.

(If the closest to a specific problem, then I find the contours of objects in OpenCV, then they should be nice to draw)

I need to do somthing like this: Example

Was it helpful?

Solution

What you are asking seems non trivial, and not so well defined. More context is required.

I would try two approaches:

  • overlay a square grid on the polygon and keep the squares that are sufficiently filled.

  • skelettonize the shape (http://en.wikipedia.org/wiki/Topological_skeleton), cleanup, and break the skeleton into mostly horizontal/vertical segments; then replace these segments by truly horizontal/vertical strokes.

OTHER TIPS

I would approach this problem using DP point reduction. There are several different flavors of this routine and the most common isn't the best for what you are trying to do but in the end this approach will result in the best quality.

The classic DP operation takes an array of points that forms a poly-line and removes any points that won't corrupt the shape beyond a certain factor. This factor is a unit of measure based on your data so in your case it would probably be in pixels. As you can imagine, picking the factor is the most difficult part of using DP unless you just want to remove points from the polygon that you can easily quantify aren't important.

In your case, and mine a lot of time, you want to remove the point that is contributing to the shape of the polygon the least. You should be able to take the typical recursive examples of DP and get it to break after the first iteration through the points so it removes one point at a time. You would then score your shape to see if it meets the requirements you have. If it doesn't score perfect you remove another point and re-score until it does or you only have three points left. If there is no perfect score you can take the shape with the best score and maybe have a 2nd algorithm that forces it to meet the requirements.

http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top