假设我有一个图像I
和一条线h1
。使用Matlab,我可以像这样画线(或线段)
h1 = plot( [l1 l1],[20 100], 'r');\n
Run Code Online (Sandbox Code Playgroud)\n\n现在我想将图像旋转I
角度为 45\xc2\xb0。所以我imrotate
这样使用:
IR = imrotate(I,45);\n
Run Code Online (Sandbox Code Playgroud)\n\n现在,我想旋转线,如何使用 Matlab 来做到这一点?
\n\n我找到了这个功能rotate
。我正在尝试这个,但它不起作用!
rotate(h1,[1 1],45);\n
Run Code Online (Sandbox Code Playgroud)\n
要旋转线,您可以使用旋转矩阵显式“旋转”定义线的点:
旋转中心由添加到线点坐标的“偏移”来定义:
在以下示例中,线旋转:
更新了代码
% 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) 旋转
绕线的一点旋转