-
Notifications
You must be signed in to change notification settings - Fork 1
/
imresizen.m
71 lines (54 loc) · 1.37 KB
/
imresizen.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
60
61
62
63
64
65
66
67
68
69
70
71
function Y=imresizen(X,scaling,varargin)
%Resize an n-dimensional array
%
% Y=imresizen(X,scaling,extrapMethod)
%
%IN:
%
% X: n-dimensional input array (single or double)
% scaling: scaling factor(s) as scalar or n-vector. Scalings along
% singleton dimensions of X are always ignored.
% extrapMethod: extrapolation method (same options as for
% griddedInterpolant). Default is linear interpolation.
%
%OUT:
%
% Y: resized array
%
%EXAMPLES:
%
% img4D=rand(30,20,10,40);
%
% (1) imresizen(img4D,2);
% (2) imresizen(img4D,1./[3,2,1,4]);
% (3) imresizen(___, 'cubic')
if ~isa(X,'single') && ~isa(X,'double')
error 'Input array must be single or double'
end
N=ndims(X);
scaling(1,1:N)=scaling(:).';
sz=size(X);
xvec=cell(1,N);
yvec=cell(1,N);
szy=nan(1,N);
nonsing=true(1,N);
for i=1:N
n=sz(i);
if n==1 %for vector input
nonsing(i)=0;
szy(i)=1;
continue
end
szy(i)=round(sz(i)*scaling(i));
m=szy(i);
xax=linspace(1/n/2, 1-1/n/2 ,n);
xax=xax-.5;
yax=linspace(1/m/2, 1-1/m/2 ,m);
yax=yax-.5;
xvec{i}=xax;
yvec{i}=yax;
end
xvec=xvec(nonsing);
yvec=yvec(nonsing);
F=griddedInterpolant(xvec,squeeze(X),varargin{:});
Y=reshape(F(yvec),szy);