The 'Orientation' property of REGIONPROPS returns a scalar value corresponding to the angle (in degrees) between the x-axis and the major axis of the ellipse that has the same second-moments as the region.
In order to find the angle of rotation of a square, you might want to use the 'Extrema' field of the output of REGIONPROPS. Given the extrema (square corners), you will be able to calculate the angle as in the example below:
s = imread('square0.bmp');
s15 = imrotate(s, 15);
g = rgb2gray(s15);
p = regionprops(l, 'Extrema');
sides = p.Extrema(4,:) - p.Extrema(6,:);
OrientationAngle = rad2deg(atan(-sides(2)/sides(1))) ;
This illustatres how to find the angle orientation of a rotated square object in all cases (orientation relative to the lower right side of the square). Note that when you run this program the angle returned is not exactly 15 degrees. The reason for this discrepancy is the the angle is computed based on the pixel centers, which usually will not be on the exact X-Y values of the corner, since pixel centers cannot be a fraction of a pixel away from each other.
Best Answer