我正在尝试在Matlab中围绕任意点旋转图像。
到目前为止,我已经使用了旋转,但是看起来旋转只是围绕中心旋转。
是否有一种不错的方法,而无需先填充图像然后使用旋转?
谢谢
我正在尝试在Matlab中围绕任意点旋转图像。
到目前为止,我已经使用了旋转,但是看起来旋转只是围绕中心旋转。
是否有一种不错的方法,而无需先填充图像然后使用旋转?
谢谢
“好方法”正在使用imwarp
构建转换矩阵有些棘手。
我从以下问题中弄清楚了如何构建它:Matlab image rotation
转换支持旋转,平移和缩放。
参数:
(x0
,y0
)是围绕它旋转的中心点。phi
是旋转角度。sx
,sy
是水平和垂直缩放(将值设置为1
)。W
和H
是输入(和输出)图像的宽度和高度。
构建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),1];
tform = affine2d(T);
J = imwarp(I,'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)。