【问题标题】:Image Rotation in Three Dimensions (Matlab)三维图像旋转(Matlab)
【发布时间】:2017-04-22 13:04:37
【问题描述】:

我正在尝试在 Matlab xyz 3-D 空间中旋转 2D 图像。我想将图像 I 围绕 x 轴旋转一个角度 θ=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.

那么这里到底是什么问题,我该如何解决呢?

我很困惑,因为此代码在专门用于二维时可以正常工作。任何解释都非常感谢。

【问题讨论】:

    标签: matlab image-processing rotation image-rotation


    【解决方案1】:

    问题是您的输入 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);
    

    此外,您还可以使用rotxrotyrotz 轻松生成旋转矩阵。

    更新

    如果您想在 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
    

    【讨论】:

    • 非常感谢。但是生成的图像会叠加在原始图像上。它并没有真正在 3-D 世界坐标中旋转。是否有可能通过在 Matlab 的 FEX 上将代码与“image3”结合使用来以某种方式实现这一点?到目前为止,图像是这样的:i.stack.imgur.com/MDi9m.jpg
    • interp3 旋转的二维图像的z 维度实际上是颜色 维度(RGB),因此结果看起来像是“叠加”的图像。如果您想在 3D 空间中旋转图像 2D 平面,您可以查看我的编辑
    • 谢谢。这很有意义。最后一个问题:当前代码围绕其边缘旋转图像。是否有一个简单的修改可以使图像围绕其中心点旋转?或者我们是否需要通过移动它们来手动居中图像?例如,对于 0° 和 90°,图像的中心不重合:i.stack.imgur.com/MM55y.jpg
    • 使用值 0 作为 z 坐标。
    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    相关资源
    最近更新 更多