I doubt you'll find ready-made code to do it. However, you can use houghlines() to fit line segments to your edge points. Once you do, you can test which line segments are perpendicular to each other, etc...
When you have a non-scalar structure, this plots all rectangles on the same figure. For seperate figures you should move "figure(), hold on" into the first loop.
Best Answer