0
$\begingroup$

Suppose we have a square shape rotated by $d$ degrees counterclockwise (or $-d$ degrees clockwise) in a binary image as the following:

enter image description here

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$?)

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Can you add a drawing, please? $\endgroup$ – Abdulkader Dec 28 '18 at 3:01
  • $\begingroup$ @Abdulkader, I've added some math instead. Hopefully, that will help you more. Maybe you can provide a good drawing once you get it. $\endgroup$ – Cedron Dawg Dec 28 '18 at 4:12
  • $\begingroup$ Why not $$ \Delta x = x_{RB} - x_{LT} $$? $\endgroup$ – Abdulkader Dec 28 '18 at 11:04
  • $\begingroup$ @Abdulkader, Sure, as long as you also use: $$ \Delta y = y_{RB} - y_{LT} $$ The slope of a line between two points is the same no matter which one you consider the first point. $\endgroup$ – Cedron Dawg Dec 28 '18 at 21:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.