【发布时间】:2013-11-10 03:59:15
【问题描述】:
我正在尝试使用 Matlab 旋转图像而不使用 imrotate 函数。我其实是用变换矩阵做的。但是还不够好。问题是,旋转后的图像是“滑动”的。我用图片告诉你。
这是我要旋转的图像:
但是当我旋转它时,例如 45 度,它变成了这样:
我在问为什么会这样。这是我的代码,是否有任何数学或编程错误?
image=torso;
%image padding
[Rows, Cols] = size(image);
Diagonal = sqrt(Rows^2 + Cols^2);
RowPad = ceil(Diagonal - Rows) + 2;
ColPad = ceil(Diagonal - Cols) + 2;
imagepad = zeros(Rows+RowPad, Cols+ColPad);
imagepad(ceil(RowPad/2):(ceil(RowPad/2)+Rows-1),ceil(ColPad/2):(ceil(ColPad/2)+Cols-1)) = image;
degree=45;
%midpoints
midx=ceil((size(imagepad,1)+1)/2);
midy=ceil((size(imagepad,2)+1)/2);
imagerot=zeros(size(imagepad));
%rotation
for i=1:size(imagepad,1)
for j=1:size(imagepad,2)
x=(i-midx)*cos(degree)-(j-midy)*sin(degree);
y=(i-midx)*sin(degree)+(j-midy)*cos(degree);
x=round(x)+midx;
y=round(y)+midy;
if (x>=1 && y>=1)
imagerot(x,y)=imagepad(i,j); % k degrees rotated image
end
end
end
figure,imagesc(imagerot);
colormap(gray(256));
【问题讨论】:
-
你试过用弧度代替度数吗?
-
其实我做了。但什么都没有改变。旋转后的图片是对的,只是它的坐标。
-
正如@Junuxx 指出的那样,
cos和sin使用弧度,而不是度数。如果要使用度数,则应使用cosd和sind。 -
谢谢,但我也试过了。还是一样。我的方程式有数学错误吗?
-
@Moondra 我想将额外的 1 左 1 右,1 上 1 下填充到图像中。如果我没记错的话,你不必使用它。
标签: matlab image-processing interpolation