尝试使用INTERP2功能在Matlab中旋转图像时出错

时间:2018-03-17 10:36:19

标签: image matlab matlab-figure

我试图在没有嵌入Matlab功能的情况下执行图像旋转。 但我仍然收到此错误: 使用。' 时出错 未定义ND阵列上的转置。改为使用PERMUTE。

interp2错误(第130行)          V = V。';

但我不知道为什么会出现这样的错误,而且我也不知道如何自定义功能 interp2 PERMUTE 来制作它的功能(我在Matlab中使用了 help )。

您可以帮忙自定义代码吗?

提前致谢!

clc; clear all; close all;

input_image = imread('mri.png');
Z = double(input_image);

Size = size(Z);
[X,Y] = meshgrid(1:Size(2), 1:Size(1));

%Center of an image
c = Size(end:-1:1)/2;

%Angle of rotation
angle = 45;
t = angle*pi/180;

%Making the rotation
ct = cos(t);
st = sin(t);
Xi = c(1) + ct*(X - c(1)) - st*(Y - c(2));
Yi = c(2) + st*(X - c(1)) + ct*(Y - c(2));

%Interpolation
Zi = interp2(X, Y, Z, Xi, Yi);

figure()
subplot(121); imshow(I); title('Original image');
subplot(122); imshow(uint8(Zi)); title('Rotated image without embedded 
function');

1 个答案:

答案 0 :(得分:1)

Z是一个3D矩阵,interp2仅适用于2D矩阵。所以你必须分别对每种颜色进行插值,并重新组合它们:

%Interpolation
Zir = interp2(X, Y, Z(:,:,1), Xi, Yi);
Zig = interp2(X, Y, Z(:,:,2), Xi, Yi);
Zib = interp2(X, Y, Z(:,:,3), Xi, Yi);
Zi = cat(3, Zir, Zig, Zib);