Hi,
After definning the boundaries using bwboudaries() try making use of poly2mask() under the for loop to crop out the interested region in image. In order to rotate the image make use of imrotate(). Get the edge points in resulted image using max and min value of the boundary and calculate the angle between the x-axis and line connecting the mid-points of the bounding box (as shown).
Hope it helps!!
Following links may help:
- https://in.mathworks.com/matlabcentral/answers/512025-how-do-i-apply-a-window-on-an-image-to-keep-the-part-in-the-window-while-reduce-the-resolution-clar (poly2mask example)