Bacon
Bacon

Reputation: 37

Error trying to rotate an image in Matlab using INTERP2 funcion

I'm trying to perform an image rotation without embedded Matlab's function. But I'm still getting this error: Error using .' Transpose on ND array is not defined. Use PERMUTE instead.

Error in interp2 (line 130) V = V.';

But I don't know why there is such a mistake and likewise I don't know how to customize the function interp2 or PERMUTE to make it functional (I have used help in Matlab).

Could you please help to customize the code?

Thanks in advance!

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

Upvotes: 0

Views: 405

Answers (1)

ViG
ViG

Reputation: 1868

Z is a 3D matrix and interp2 only works for 2D matrices. So you have to do the interpolation for each colour separately, and recombine them:

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

Upvotes: 1

Related Questions