【问题标题】:Rotate image in Matlab about arbitrary points在 Matlab 中旋转图像关于任意点
【发布时间】:2020-03-04 15:10:35
【问题描述】:

我试图在 Matlab 中围绕任意一组点旋转图像。

到目前为止,我使用过 imrotate,但看起来 imrotate 只围绕中心旋转。

有没有一种很好的方法来做到这一点,而无需先填充图像然后使用 imrotate?

谢谢

【问题讨论】:

标签: image matlab image-processing


【解决方案1】:

“好方法”是使用imwarp

构建转换矩阵有点棘手。
我从以下问题中弄清楚了如何构建它:Matlab image rotation
变换支持旋转、平移和缩放。

参数:
(x0, y0) 是您围绕它旋转的中心点。
phi 是旋转角度。
sx, sy 是水平和垂直缩放(设置为@ 987654332@).
WH 是输入(和输出)图像的宽度和高度。

构建 3x3 变换矩阵:

T = [sx*cos(phi), -sx*sin(phi), 0
     sy*sin(phi),  sy*cos(phi), 0
     (W+1)/2-((sx*x0*cos(phi))+(sy*y0*sin(phi))), (H+1)/2+((sx*x0*sin(phi))-(sy*y0*cos(phi))), 1];

使用imwarp

tform = affine2d(T);
J = imwarp(I, tform, 'OutputView', imref2d([H, W]), 'Interp', 'cubic');

这是一个完整的可执行代码示例:

I = imresize(imread('peppers.png'), 0.5); %I is the input image

[H, W, ~] = size(I);  %Height and Width of I

phi = 120*pi/180; %Rotate 120 degrees

%Zoom coefficients
sx = 1;
sy = 1;

%Center point (the point that the image is rotated around it).
x0 = (W+1)/2 + 50;
y0 = (H+1)/2 + 20;

%Draw white cross at the center of the point of the input image.
I(y0-0.5:y0+0.5, x0-19.5:x0+19.5, :) = 255;
I(y0-19.5:y0+19.5, x0-0.5:x0+0.5, :) = 255;

%Build transformation matrix.
T = [sx*cos(phi), -sx*sin(phi), 0
     sy*sin(phi),  sy*cos(phi), 0
     (W+1)/2-((sx*x0*cos(phi))+(sy*y0*sin(phi))), (H+1)/2+((sx*x0*sin(phi))-(sy*y0*cos(phi))), 1];

tform = affine2d(T);
J = imwarp(I, tform, 'OutputView', imref2d([H, W]), 'Interp', 'cubic');

%Draw black cross at the center of the output image:
J(end/2:end/2+1, end/2-15:end/2+15, :) = 0;
J(end/2-15:end/2+15, end/2:end/2+1, :) = 0;

%Shows that the center of the output image is the point that the image was rotated around it.
figure;imshow(J)

输入图片:

输出图片:


注意:
与其他方法(如填充后的imrotate)相比的重要优势是中心坐标不必是整数值。
例如,您可以围绕 (100.4, 80.7) 旋转。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多