gpt4 book ai didi

matlab - 在不使用 imrotate 的情况下通过 Matlab 旋转图像

转载 作者:太空宇宙 更新时间:2023-11-03 19:07:32 26 4
gpt4 key购买 nike

我试图在不使用 imrotate 函数的情况下使用 Matlab 旋转图像。我实际上是通过使用变换矩阵来实现的。但是它还不够好。问题是,旋转后的图像在“滑动”。让我用图片告诉你。

这是我要旋转的图像:

enter image description here

但是当我旋转它时,例如 45 度,它变成了这样:

enter image description here

我在问为什么会这样。这是我的代码,是否有任何数学或编程错误?

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));

最佳答案

图像中有孔洞的原因是因为您正在计算 imagepad 中每个像素在 imagerot 中的位置。您需要以相反的方式进行计算。也就是说,对于 imagerot 中的每个像素,在 imagepad 中进行插值。为此,您只需要应用逆变换,在旋转矩阵的情况下,它只是矩阵的转置(只需更改每个 sin 上的符号并以另一种方式转换)。

imagerot 中循环像素:

imagerot=zeros(size(imagepad)); % midx and midy same for both

for i=1:size(imagerot,1)
for j=1:size(imagerot,2)

x= (i-midx)*cos(rads)+(j-midy)*sin(rads);
y=-(i-midx)*sin(rads)+(j-midy)*cos(rads);
x=round(x)+midx;
y=round(y)+midy;

if (x>=1 && y>=1 && x<=size(imagepad,2) && y<=size(imagepad,1))
imagerot(i,j)=imagepad(x,y); % k degrees rotated image
end

end
end

另请注意,您的midxmidy 需要使用size(imagepad,2)size(imagepad, 1) 分别,因为第一个维度指的是行数(高度),第二个维度指的是宽度。

注意:当您决定采用最近邻以外的插值方案时,同样的方法也适用,如 Rody 的线性插值示例。

编辑:我假设您出于演示目的使用循环,但实际上不需要循环。这是最近邻插值的示例(您正在使用的),保持相同大小的图像,但您可以修改它以生成包含整个源图像的更大图像:

imagepad = imread('peppers.png');
[nrows ncols nslices] = size(imagepad);
midx=ceil((ncols+1)/2);
midy=ceil((nrows+1)/2);

Mr = [cos(pi/4) sin(pi/4); -sin(pi/4) cos(pi/4)]; % e.g. 45 degree rotation

% rotate about center
[X Y] = meshgrid(1:ncols,1:nrows);
XYt = [X(:)-midx Y(:)-midy]*Mr;
XYt = bsxfun(@plus,XYt,[midx midy]);

xout = round(XYt(:,1)); yout = round(XYt(:,2)); % nearest neighbor!
outbound = yout<1 | yout>nrows | xout<1 | xout>ncols;
zout=repmat(cat(3,1,2,3),nrows,ncols,1); zout=zout(:);
xout(xout<1) = 1; xout(xout>ncols) = ncols;
yout(yout<1) = 1; yout(yout>nrows) = nrows;
xout = repmat(xout,[3 1]); yout = repmat(yout,[3 1]);
imagerot = imagepad(sub2ind(size(imagepad),yout,xout,zout(:))); % lookup
imagerot = reshape(imagerot,size(imagepad));
imagerot(repmat(outbound,[1 1 3])) = 0; % set background value to [0 0 0] (black)

要将上述内容修改为线性插值,请计算 XYt 中每个坐标的 4 个相邻像素,并使用小数分量乘积作为权重执行加权和。我会把它留作练习,因为它只会使我的答案膨胀到超出你的问题范围。 :)

关于matlab - 在不使用 imrotate 的情况下通过 Matlab 旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com