【问题标题】:Image rotation about an arbitrary point关于任意点的图像旋转
【发布时间】:2013-10-09 13:09:22
【问题描述】:

我被要求对任意点执行图像旋转。他们提供的框架是在 matlab 中的,所以我必须填写一个名为 MakeTransformMat 的函数,该函数接收旋转角度和我们想要旋转的点。

正如我在课堂上看到的那样,首先我们将点平移到原点,然后旋转,最后平移回来。

框架要求我返回一个转换矩阵。我是否可以将该矩阵构建为 translate-rotate-translate 矩阵的乘法?否则,我忘记了什么?

function TransformMat = MakeTransformMat(theta,center_y,center_x) 

%Translate image to origin
trans2orig = [1 0 -center_x;
              0 1 -center_y;
              0 0 1];
%Rotate image theta degrees
rotation = [cos(theta) -sin(theta) 0;
            sin(theta) cos(theta)  0;
            0          0           1];
%Translate back to point
trans2pos = [1 0 center_x;
             0 1 center_y;
             0 0 1];

TransformMat = trans2orig * rotation * trans2pos;

end

【问题讨论】:

  • 你试过了吗?它是否为您返回了正确旋转的图像?
  • 不,我没有,因为在应用之前我正在努力处理框架的其他部分,我不习惯 MATLAB,他们使用非常奇怪的函数,我不知道他们做了什么跨度>
  • 好吧,我建议您首先构建一个框架来测试它(或者如果您有图像处理工具箱,您可以使用imtransform 进行测试)。
  • 请记住,转换的顺序很重要。 T2*RT1 会给你与 T1*RT2 不同的结果。只需确保您的积分首先乘以您的第一个转换,然后是您的第二个转换,依此类推。
  • 是的,该矩阵确实做到了。

标签: matlab image-rotation


【解决方案1】:

这对我有用。这里 I 是输入图像,J 是旋转图像

[height, width] = size(I);
rot_deg = 45;       % Or whatever you like (in degrees)
rot_xc = width/2;   % Or whatever you like (in pixels)
rot_yc = height/2;  % Or whatever you like (in pixels)


T1 = maketform('affine',[1 0 0; 0 1 0; -rot_xc -rot_yc 1]);
R1 = maketform('affine',[cosd(rot_deg) sind(rot_deg) 0; -sind(rot_deg) cosd(rot_deg) 0; 0 0 1]);
T2 = maketform('affine',[1 0 0; 0 1 0; width/2 height/2 1]);

tform = maketform('composite', T2, R1, T1);
J = imtransform(I, tform, 'XData', [1 width], 'YData', [1 height]);

干杯。

【讨论】:

  • 好答案!只有最后一个矩阵被固定到它应该是的中心点: T2 = maketform('affine',[1 0 0; 0 1 0; rot_xc rot_yc 1]);
【解决方案2】:

我在其他地方回答了一个非常相似的问题:Here is the link.

在链接到的代码中,您旋转的点取决于meshgrid 的定义方式。

这有帮助吗?你读过Wikipedia page on rotation matrices吗?

【讨论】:

  • 我很怀疑,但后来我用纸一步一步地尝试了它,是的,它实际上就像它应该工作的那样工作。
猜你喜欢
  • 2020-03-04
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多