How to get the orientation value of a rotated square? Suppose we have a square shape rotated by $d$ degrees counterclockwise (or $-d$ degrees clockwise) in a binary image as the following:



I'm trying to rotate it by 'Orientation' value of regionprops using imrotate function.

But in this case, the major and the minor axis are the same (the ellipse here is a circle).

Hence, the Orientation value equal to $0$ degrees instead of $d$ (or $-d$) degrees. 

What is the easiest way to rotate it so that its sides become parallel to the x-axis and y-axis?

(i.e. How to get the value of $d$?)
Disclaimer:  I am not a MATLAB user, so I had to search for this and have no way to test it.

You should be able to find two opposite corner points by using the "Extrema" property from "regionprops".  The tangent of an angle is a slope, so calculate the slope and take the inverse tangent.  Do note that the line may be vertical, i.e. undefined slope, and you have to deal with that case.  If you choose the left-top and right-bottom, you should get a more horizontal line and not have to worry about it.

You also have to keep radians and degrees straight.



As math:

$$ \Delta y = y_{LT} - y_{RB} $$
$$ \Delta x = x_{LT} - x_{RB} $$

$$ m = \frac{\Delta y}{\Delta x} $$

$$ \theta = tan_{deg}^{-1} (m) $$

$$ d = \theta - 45 $$

By finding the leftmost and rightmost $x$'s, there shouldn't be a problem with the slope calculation.  The line between the two extreme points should be a diagonal of the square.  A diagonal should be at 45 degrees, so $d$ is how much you need to rotate the figure to get it there.