How can i convert a polygon into a rectangle?
20 次查看(过去 30 天)
显示 更早的评论
I have a 4 side polygon that i draw on a video frame. I would like to take this polygon and make it a rectangle so i will be able to treat it as a matrix.I want to take the smallest rectangle i can have but still be as close as i can to the original vertices. Can anyone help me with that please? I've been trying to do it for a several days and with no success. Keep in mind that my polygon can be completely not parallel to the axes and in that case i want to get a rectangle that is also not parallel to the axes.
0 个评论
采纳的回答
Image Analyst
2013-9-17
It's not trivial. But luckily John D'Errico has done it for you. See his File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/34767-a-suite-of-minimal-bounding-objects
14 个评论
Simon
2013-9-17
As far as I see it is not obvious what the smallest rectangle that still contains the polygon is. One way is
- search for the longest side of the polygon
- Take this side as the base of the new rectangle (has two points in common with the polygon)
- Get the distance of the furthermost remaining point
- Construct the opposite edge of the rectangle, crossing this point
- Construct the remaining two connecting edges perpendicular to the first edge
This will give you a rectangle with small size (I don't know if it is the minimum size!). And it is all pure 2d-geometry, easy to implement, nothing fancy
Image Analyst
2013-9-17
I haven't used John's function for rectangle but I imagine it would give the coordinates of the corners. If there are 5 instead of 4 it could be because the last point is the same as the first point to "close off" the rectangle instead of it being a "U" shape.
Imagine that you put a box around your points. Now, as you rotate the box around your points, the sides of the box will have to lengthen or shorten to still contain your points. With one of those shapes of boxes, the area will be a minimum. That is the box he returns to you.
更多回答(1 个)
Simon
2013-9-17
Hi!
You have your polygon coordinates in (x/y) pairs? Take their min/max values and cut from the video frame the rectangle R(xmin:xmax, ymin:ymax).
Maybe you post your code, then it's easier to help.
3 个评论
Simon
2013-9-17
Hi!
You have your variable "pos". Now you get the minimum and maximum x and y values. This are the coordinates of your rectangle, it contains all polygons.
Image Analyst
2013-9-17
Simon is saying to use the "bounding box" of your polygon, which will have be aligned with the axes and probably larger than what I suggested. What I suggested may give a tilted rectangle but it will have a smaller size than the bounding box.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Point Cloud Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!