如何使用Matlab旋转一条线?

5 matlab rotation

假设我有一个图像I和一条线h1。使用Matlab,我可以像这样画线(或线段)

\n\n
h1 = plot( [l1 l1],[20 100], 'r');\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在我想将图像旋转I角度为 45\xc2\xb0。所以我imrotate这样使用:

\n\n
IR = imrotate(I,45);\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我想旋转线,如何使用 Matlab 来做到这一点?

\n\n

我找到了这个功能rotate。我正在尝试这个,但它不起作用!

\n\n
rotate(h1,[1 1],45);\n
Run Code Online (Sandbox Code Playgroud)\n

il_*_*ffa 5

要旋转线,您可以使用旋转矩阵显式“旋转”定义线的点:

在此输入图像描述

旋转中心由添加到线点坐标的“偏移”来定义:

在以下示例中,线旋转:

  • 线的下点(偏移量=线下点的坐标)
  • 原点 (0,0)(偏移量 = 0)
  • 原始直线上的一个点(偏移量 = 直线上一点的坐标)

更新了代码

% 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
Run Code Online (Sandbox Code Playgroud)

删除前一行(使用函数delete)并覆盖新行。

MatLab 2015 上存在该函数rotx

希望这可以帮助。

绕A点旋转 在此输入图像描述

绕原点 (0,0) 旋转

在此输入图像描述

绕线的一点旋转 在此输入图像描述