0

Let's say I have a 4 pixel by 4 pixel image with values between 0 and 255 and I want to expand it to an 8-by-8 image, interpolating where necessary. I know how to interpolate a vector this way with interp1:

interp1(linspace(0,1,numel(vector)), vector, linspace(0,1,newSize))

But I'm unclear how to use interp2 to do the same thing for a matrix.

EDIT: Would it be the same if I were to create a meshgrid after using linspace for each dimension?

EDIT2: Yep, that worked. It's the same, but with a meshgrid.

playitright
  • 174
  • 7

1 Answers1

0

For data structured in the form of a regular grid, you can use interp2 as you correctly stated, but since you are dealing with an image, I suggest you to use the imresize function, which is fine-tuned for rescaling images using appropriate interpolation algorithms:

% Load an image and display it...
img = imread('peppers.png');
figure(),imshow(img);

% Create a second image that is
% twice the size of the original
% one and display it...
img2 = imresize(img,2);
figure(),imshow(img2);

Anyway, if you really want to use the aforementioned function, here is how I would perform the interpolation:

% Load an image, convert it into double format
% and retrieve its basic properties...
img = imread('rice.png');
img = im2double(img);
[h,w] = size(img);

% Perform the interpolation...
x = linspace(1,w,w*2);
y = linspace(1,h,h*2);
[Xq,Yq] = meshgrid(x,y);
img2 = interp2(img,Xq,Yq,'cubic') ./ 255;

% Display the original image...
figure(),imshow(img);

%Display the rescaled image...
figure(),imshow(img2,[]);
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98