In: Computer Science
Develop a program that magnifies (monochrome) image by a factor of 2^N using bilinear interpolation. Implement this up-scaling in 2 different ways: 1) directly, i.e. calling interp2 once; and 2) ? times iteratively doubling the size. Compare the outputs and discuss the variation in the output (if any).
Answer:
Program using interp2 to upscale the image
clear all
% read the image and convert to 2D array
image1=rgb2gray(imread('CuteFace.png'));
% display the image
figure
imshow(image1)
% convert to double values
image1=double(image1);
% get the size of the image1
[m n]= size(image1);
% define the meshgrid
[x,y] = meshgrid(1:n, 1:m);
% define the scaling factor
upScalingFactor = 2;
% define the meshgrid with the scaling factor
[p,q]=meshgrid(1:upScalingFactor:n, 1:upScalingFactor:m);
% user the interp2 to get the upscaling factor
image2=interp2(x,y,image1,p,q,'linear');
% plot the image
figure
subplot(1,2,1), imagesc(image1), axis, image
title('Original image','FontSize',18)
% plot the upscaled image
subplot(1,2,2), imagesc(image2), axis, image
title('Bilinear interpolation','FontSize',18)
% conver the image
i3 = cast(image2, class(image2));
% store the image
imwrite(i3,'gray1.png');
% display the new image
figure
imshow('gray1.png')
Program to upscale the image using iteration:
% Import my original picture file
N = 2;
image1 = imread('tiger.jpg');
% convert the image to monochrome
image1 = rgb2gray(image1);
% get the dimensions of the image1
[rows columns] = size(image1);
% define the scaling factor
factor = 2 ^ N;
row_new = rows * factor;
column_new = k * factor;
% define the scaling for the new image
x_scale = row_new./(rows - 1);
y_scale = column_new./(columns - 1);
% define the array for the new image
newZoomImage = zeros(row_new, column_new);
% create the output image by using bilinear interpolation
for count1 = 0 : row_new - 1
for count2 = 0 : column_new - 1
newZoomImage(count1+1,count2+1) = image1(1+round(count1./x_scale),1+round(count2./y_scale));
end
end
% plot the images
subplot(1,2,1);
imagesc(image1);
colormap gray;
subplot(1,2,2);
imagesc(newZoomImage);
colormap gray;
% Write the images to files
imwrite(image1,gray(256),'original.jpg');
imwrite(newZoomImage,gray(256),'newZoom.jpg');
In both the programs,
But at final, the images are up scaled to the given factor size.