Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

% Function Name: imageInterp % Inputs (3): - (uint8) An image array % - (double)

ID: 3539976 • Letter: #

Question

% Function Name: imageInterp
% Inputs (3): - (uint8) An image array
%             - (double) The new number of rows the image should have
%             - (double) The new number of columns the image should have
% Output (1): - (uint8) A resized image using interpolation
%
% Function Description:
% Write a function called "imageInterp" that takes in an image array and
% the new dimensions of the images and scales the image using
% interpolation. Scaling using interpolation results in clearer images
% when resizing versus traditional techniques.
%
% To scale an image with interpolation, the function first scales the
% width of the image to the new column amount. This can be accomplished by
% iterating through each row of image and using the interp() function to
% find the new pixel values. The interp() function takes in a vector of
% x-values, a vector of corresponding y-values, and a vector of new
% x-values within the same range as the original, and gives back what the
% probable new y-values would be if there was a linear relationship
% between the data sets. For additional information, please type 'help
% interp' in the Command Window.
%
% When performing the linear interpolation, the x-values are taken to be
% the column indices of the image, and the y-values are the current values
% at a layer. The new x-values are then the indices found using the
% conventional re-indexing formula taught in class, except that the
% round() is not applied. Essentially, the new x-values should equal the
% linspace(1, col, new_col). After all the rows are scaled, the image has
% been scaled to the given width. The same process is then repeated except
% we now loop through the columns and use the row indices as the
% x-values, completing the resizing of the image.
%
% Test Cases:
%   img = imread('sample.png');
%   new_img = imageInterp(img, 400, 400);
%   imshow(new_img)
%       => Should look like imageInterp1.png



%   img = imread('buzz.png');
%   new_img = imageInterp(img, round(336*1.4), round(445*1.4));
%   imshow(new_img)
%       => Should look like imageInterp2.png


Explanation / Answer

function ret = imageInterp(img,r,c)

ret =zeros(r,c,3,'uint8');

[rold cold v]=size(img);

lin = linspace(1,cold,c);

for k=1:rold

ret(k,:,1)=uint8(interp1(double(img(k,:,1)),lin));

ret(k,:,2)=uint8(interp1(double(img(k,:,2)),lin));

ret(k,:,3)=uint8(interp1(double(img(k,:,3)),lin));

end

lin = linspace(1,rold,r);

for k=1:c

ret(:,k,1)=uint8(interp1(double(ret(1:rold,k,1)),lin));

ret(:,k,2)=uint8(interp1(double(ret(1:rold,k,2)),lin));

ret(:,k,3)=uint8(interp1(double(ret(1:rold,k,3)),lin));

end

end