我正在尝试在Matlab xyz 3-D空间中旋转2D图像。我想围绕x轴以θ= i 的角度旋转图像 I 。我通过与旋转矩阵(定义为here)的乘法来实现这一点:
这是我的代码:
x = linspace(-1,1,size(I,1));
[x0,y0,z0] = meshgrid(x,x,1);
R = [1 0 0; 0 cosd(-i) -sind(-i); 0 sind(-i) cosd(-i)];
xy = [x0(:),y0(:), z0(:)]*R';
X = reshape(xy(:,1),size(x0));
Y = reshape(xy(:,2),size(y0));
Z = reshape(xy(:,3),size(z0));
rotatedI = interp3(x0,y0,z0,I,X,Y,Z, 'nearest');
rotatedI(isnan(rotatedI)) = 0;
我得到的错误是在插值行中:
Error using griddedInterpolant
Interpolation requires at least two sample points in each
dimension.
那么问题到底是什么,我该如何解决呢?
我很困惑,因为这个代码在专门用于两个维度时工作正常。非常感谢任何解释。
答案 0 :(得分:0)
问题是您的输入z网格z0
包含单个值(1
),而您想要的z网格Z
包含多个值。因此无法将此单个值插入多个值。
您可以为输入z网格指定多个值,以便可以进行插值。
I = im2double(imread('cameraman.tif'));
I = cat(3,I,I,I);
i = 10;
x = linspace(-1,1,size(I,1));
[x0,y0,z0] = meshgrid(x,x,-1:1); % 3 z values
R = [1 0 0; 0 cosd(-i) -sind(-i); 0 sind(-i) cosd(-i)];
xy = [x0(:),y0(:), z0(:)]*R;
X = reshape(xy(:,1),size(x0));
Y = reshape(xy(:,2),size(y0));
Z = reshape(xy(:,3),size(z0));
rotatedI = interp3(x0,y0,z0,I,X,Y,Z, 'nearest');
rotatedI(isnan(rotatedI)) = 0;
imshow(rotatedI);
此外,您可以使用rotx
,roty
和rotz
轻松生成轮播矩阵。
<强>更新强>
如果要在3D空间中旋转图像2D平面,则不需要使用插值,可以执行以下操作:
I = im2double(imread('cameraman.tif'));
i = 10;
x = linspace(-1,1,size(I,1));
[x0,y0,z0] = meshgrid(x,x,-1:1); % 3 z values
[x0,y0,z0] = meshgrid(x,x,1); % 3 z values
R = [1 0 0; 0 cosd(-i) -sind(-i); 0 sind(-i) cosd(-i)];
xy = [x0(:),y0(:), z0(:)]*R;
X = reshape(xy(:,1),size(x0));
Y = reshape(xy(:,2),size(y0));
Z = reshape(xy(:,3),size(z0));
warp(X,Y,Z,I);
grid on