【问题标题】:How to rotate a line using Matlab?如何使用 Matlab 旋转一条线?
【发布时间】:2015-05-18 05:55:15
【问题描述】:

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

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

现在我想将图像I 旋转45°。所以我像这样使用imrotate

IR = imrotate(I,45);

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

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

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

【问题讨论】:

    标签: matlab rotation


    【解决方案1】:

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

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

    在下面的例子中,线是围绕着旋转的:

    • 线的下点(偏移量 = 线下点的坐标)
    • 原点 (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
    

    删除上一行(使用函数delete)并删除新行。

    在 MatLab 2015 上存在函数rotx

    希望这会有所帮助。

    围绕 A 点旋转

    绕原点 (0,0) 旋转

    绕线的一点旋转

    【讨论】:

    • 请问xy 的尺寸是多少?你的意思是L1 = m* L,其中L是(x,y)L1=(x1,y1)
    • 我已经更新了答案中的代码; x 和 y 是具有原始坐标的数组。为了围绕其中一个顶点旋转线,您应该首先将线“移动”到原点 (0;0),然后应用旋转矩阵,然后将线移回。
    • 不是 11 而是 l1
    • 如果我正在绘制plot( [l1 l1],[20 100], 'r'),这意味着我有两个点A(l1,20)B(l1,100)。我仍然没有得到你的代码,但我正在努力......
    • 你好@OSryx,在你的问题中你没有指定旋转线的方向;当我编写代码来回答您的问题时,我选择了线的较低点作为旋转线的点。然而,通过offset 参数,可以围绕任何点旋转线:我已经通过引入一个循环来更新代码,该循环允许围绕 3 个不同的点(较低、原点和线)。结果显示在图像中。
    【解决方案2】:

    这是一个围绕原点 (0,0) 旋转两个点并在绘图上显示结果的函数。

    更新

    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])
    

    图像旋转 A(13,20) , B(13,100) 和 t= 10°

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多