【实例简介】不调用方法,手动实现图像缩放
【实例截图】
【核心代码】
% MATLAB script for Assessment Item-1
% Task-1
clear; close all; clc;
%% Step-1: Load input image and conversion of input image to grey-scale image
I = imread('Zebra.jpg');
figure;
imshow(I);
title('Load input image', 'FontSize', 18);
Igray = rgb2gray(I);
figure;
imshow(Igray);
title('Conversion of input image to greyscale', 'FontSize', 18);
%% Step-2: Nearest-neighbor interpolation
n = 3; % multiple of expand
[w,h] = size(Igray); % the size of greyscale
Inearest = zeros(w*n,h*n); % build a matrix which size is triple times of original image
for row1 = 1:w*n % build a circluation to traverse every pixel
for col1 = 1:h*n
row = round(row1/n); % copy the value of pixel based on index
col = round(col1/n);
if row<1
row = 1;
end
if row > w
row = w;
end
if col < 1
col = 1;
end
if col > h
col = h;
end
% ensure the index of pixel is positive integer
Inearest(row1,col1) = Igray(row,col); % use pixels of original image to fill output image
end
end
Inearest=uint8(Inearest); % Conversion of double to uint8
figure;
imshow(Inearest);
colorbar;
axis on;
title('Figure 1: Nearest-neighbor interpolation', 'FontSize', 22);
%% Step-3: Bilinear Interpolation
Ibilinear = zeros(w*n,h*n); % build a matrix which size is triple times of original image
for row2 = 1:w*n % build a circluation to traverse every pixel
for col2 = 1:h*n
row = row2/n; % copy the value of pixel based on index
col = col2/n;
dec1 = row-floor(row); % get fractional part for bilinear interpolation formula
dec2 = col-floor(col);
if row < 1
row = 1;
end
if row > w
row = w;
end
if col < 1
col = 1;
end
if col > h
col = h;
end
% ensure the index of pixel is positive integer
Ibilinear(row2,col2) = (1-dec2)*(1-dec1)*Igray(floor(row),floor(col)) ...
dec2*(1-dec1)*Igray(floor(row),ceil(col)) ...
(1-dec2)*dec1*Igray(ceil(row),floor(col)) ...
dec2*dec1*Igray(ceil(row),ceil(col));
% use pixels of original image to fill output image based on bilinear interpolation formula
end
end
Ibilinear = uint8(Ibilinear); % Conversion of double to uint8
figure;
imshow(Ibilinear);
colorbar;
axis on;
title('Figure 2: Bilinear interpolation', 'FontSize', 22);