user813853
user813853

Reputation:

How to rotate a line using Matlab?

Suppose I have an image I and a line h1. Using Matlab, I can draw the line (or segment) like this

h1 = plot( [l1 l1],[20 100], 'r');

Now I want to rotate the image I with an angle of 45°. So I used imrotate like this:

IR = imrotate(I,45);

Now, I want to rotate the line, how to do that using Matlab ?

I found the function rotate. I am trying this, but it is not working !

rotate(h1,[1 1],45);

Upvotes: 5

Views: 6865

Answers (2)

il_raffa
il_raffa

Reputation: 5190

To rotate the line you can explicitly "rotate" the points defining the line by using the rotation matrix:

enter image description here

The centre of the rotation is defined by the "offset" added to the coordinates of the line's points:

In the following examples, the line is rotated around:

  • the lower point of the line (offset = coordinates of the lower point of the line)
  • the origin (0,0) (offset = 0)
  • a point of the original line (offset = coordinates of a point of the line)

Updated code

% Definition of the L1 parameter
L1=13;
% Definition of the points A and B
A=[L1,20]
B=[L1,100]
x=[A(1) B(1)];
y=[A(2) B(2)];
% Definition of the offset
x_offset=[x(1) 0 x(1)];
y_offset=[y(1) 0 50];
for k=1:3
   figure
   % Plot of the original line
   plot(x,y,'r','linewidth',2)
   grid on
   hold on
   for a=10:10:350
      % Definitin of the rotation angle
      % a=45;
      t=a*pi/180;
      % Definition of the rotation matrix
      mx=[ ...
         cos(t) -sin(t)
         sin(t) cos(t)
         ];
      % Traslation and rotation of the points A and B
      x1_y1=mx*[x - x_offset(k);y - y_offset(k)]
      x1=x1_y1(1,:);
      y1=x1_y1(2,:);
      % Plot of the rotated line
      ph=plot(x1+x_offset(k),y1+y_offset(k),'b','linewidth',2)
      daspect([1 1 1])
      % xlim([-100 30])
      % ylim([-80 120])
      plot(x1+x_offset(k),y1+y_offset(k),'o','markeredgecolor','b', ...
         'markersize',3,'markerfacecolor','b')
      pause(.05)
      % delete(ph)
   end
   legend('Original','Rotated',-1)
end

delete the previous line (using the function delete) and drowing the new line.

On MatLab 2015 exists the function rotx.

Hope this helps.

Rotation around point A enter image description here

Rotation around the origin (0,0)

enter image description here

Rotation around a point of the line enter image description here

Upvotes: 5

user813853
user813853

Reputation:

Here is a function that rotate two points around the origin (0,0) and show the result on a plot.

Updated

function [Af,Bf] = rotateTwoPoints (A,B,t)
% Definition of the rotation matrix (rotation around origin (0,0) )
R=[ ...
   cosd(t) -sind(t)
   sind(t) cosd(t)
   ];

% rotation of the points A and B
Af = R*A;
Bf = R*B;

% Plot of the original line
plot(A(1),A(2),'k*', B(1),B(2),'b*');
line([A(1) B(1)],[A(2) B(2)], 'Color','r');

grid on
hold on

% Plot of the rotated line
plot(Af(1),Af(2),'g*', Bf(1),Bf(2),'r*');
line([Af(1) Bf(1)],[Af(2) Bf(2)], 'Color','b');
legend('A','B','line AB','Af','Bf','line AfBf','Location','northeastoutside');
daspect([1 1 1])

image rotation of A(13,20) , B(13,100) and t= 10°

[Af,Bf] = rotateTwoPoints(A,B,10)

enter image description here

Upvotes: 1

Related Questions