-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyInterp2.m
59 lines (53 loc) · 1.61 KB
/
myInterp2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function [out] = myInterp2(I, xx, yy, method)
%
% This function computes the interpolation method selected by the
% variable 'method' to the input image 'I.' The inputs 'xx' and
% 'yy' represent the grid for the location of the new pixel values.
% The variable 'method' can be: 'nn' (nearest neighbor, this is the
% default), 'bilinear,' or 'bicubic.'
%
% ---- Oscar Castro, 10 05 2021
%
% ---------- Example ----------
%
% image = "img\cameraman.tif";
% I = imread(image);
% xx = 500; yy = 500;
% % Nearest neighbor interpolation
% out = myInterp2(I, xx, yy, 'nearest');
% % Bilinear interpolation
%
% % Bicubic interpolation
%
% figure,
% subplot(121),imshow(I);title('Original Image'); axis([0,xx,0,yy]);axis on;
% subplot(122),imshow(out);title('After interpolation'); axis([0,xx,0,yy]);axis on;
[x,y,~]=size(I);
ratio_x = xx/x;
ratio_y = yy/y;
out = zeros(xx, yy, class(I));
if strcmp(method,'nearest')
for i=1:xx
for j=1:yy
% map from output image location to input image location
ii = round((i-1)*(x-1)/(ratio_x*x-1)+1);
jj = round((j-1)*(y-1)/(ratio_y*y-1)+1);
% assign value
out(i,j) = I(ii,jj);
end
end
elseif strcmp(method,'bilinear')
disp('Bilinear interpolation');
elseif strcmp(method,'bicubic')
disp('Bicubic interpolation');
else
for i=1:xx
for j=1:yy
% map from output image location to input image location
ii = round((i-1)*(x-1)/(ratio_x*x-1)+1);
jj = round((j-1)*(y-1)/(ratio_y*y-1)+1);
% assign value
out(i,j) = I(ii,jj);
end
end
end