MATLAB: Extract Shapes (Polygons) from boundaries

MATLABshape polygon boundary

I have implemented code in Matlab to analyze an image of a PCB and then highlight possible ICs via edge detection.
Is there any way for me to use my set of boundaries (shown in red in the attached image) to extract sub-images of board components from the master images? If possible, I would only like to derive and then extract square and rectangular shaped areas so I skip board silkscreen and holes.

Best Answer

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...