-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterp2d.m
116 lines (101 loc) · 4.26 KB
/
interp2d.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function outputImage = interp2d(inputImage,X,Y,method,fillValues)
% FOR INTERNAL USE ONLY -- This function is intentionally
% undocumented and is intended for use only within other toolbox
% classes and functions. Its behavior may change, or the feature
% itself may be removed in a future release.
%
% Vq = INTERP2D(V,XINTRINSIC,YINTRINSIC,METHOD,FILLVAL) computes 2-D
% interpolation on the input grid V at locations in the intrinsic
% coordinate system XINTRINSIC, YINTRINSIC. The value of the output grid
% Vq(I,J) is determined by performing 2-D interpolation at locations
% specified by the corresponding grid locations in XINTRINSIC(I,J),
% YINTRINSIC(I,J). XINTRINSIC and YINTRINSIC are plaid matrices of the
% form constructed by MESHGRID. When V has more than two dimensions, the
% output Vq is determined by interpolating V a slice at a time beginning at
% the 3rd dimension.
%
% See also INTERP2, MAKERESAMPLER, MESHGRID
% Copyright 2012-2020 The MathWorks, Inc.
% Algorithm Notes
%
% This function is intentionally very similar to the MATLAB INTERP2
% function. The differences between INTERP2 and images.internal.interp2d
% are:
%
% 1) Edge behavior. This function uses the 'fill' pad method described in
% the help for makeresampler. When the interpolation kernel partially
% extends beyond the grid, the output value is determined by blending fill
% values and input grid values.
%
% 2) Plane at a time behavior. When the input grid has more than 2
% dimensions, this function treats the input grid as a stack of 2-D interpolation
% problems beginning at the 3rd dimension.
%
% 3) Degenerate 2-D grid behavior. Unlike interp2, this function handles
% input grids that are 1-by-N or N-by-1.
% IPP requires that X,Y,and fillVal are of same type. We enforce this for
% both codepaths for consistency of results.
switch class(inputImage)
case 'double'
X = double(X);
Y = double(Y);
fillValues = double(fillValues);
case 'single'
X = single(X);
Y = single(Y);
fillValues = single(fillValues);
case 'uint8'
X = single(X);
Y = single(Y);
fillValues = uint8(fillValues);
otherwise
assert('Unexpected inputImage datatype.');
end
if (~ismatrix(inputImage) && isscalar(fillValues))
% If we are doing plane at at time behavior, make sure fillValues
% always propogates through code as a matrix of size determine by
% dimensions 3:end of inputImage.
sizeInputImage = size(inputImage);
if (ndims(inputImage)==3)
% This must be handled as a special case because repmat(X,N)
% replicates a scalar X as a NxN matrix. We want a Nx1 vector.
sizeVec = [sizeInputImage(3) 1];
else
sizeVec = sizeInputImage(3:end);
end
fillValues = repmat(fillValues,sizeVec);
end
if ippl
%inputImage = padImage(inputImage, fillValues);
% We have to account for 1 vs. 0 difference in intrinsic
% coordinate system between remap and MATLAB
if isreal(inputImage)
outputImage = images.internal.builtins.remap(inputImage,X,Y,method,fillValues);
else
outputImage = complex( images.internal.builtins.remap(real(inputImage),X,Y,method,real(fillValues)),...
images.internal.builtins.remap(imag(inputImage),X,Y,method,imag(fillValues)));
end
else
inputClass = class(inputImage);
% Required since we allow uint8 inputs to interp2d and interp2 in
% MATLAB does not support integer datatype inputs.
if ~isfloat(inputImage)
inputImage = single(inputImage);
fillValues = cast(fillValues, 'like', inputImage);
end
% Preallocate outputImage so that we can call interp2 a plane at a time if
% the number of dimensions in the input image is greater than 2.
if ~ismatrix(inputImage)
[~,~,P] = size(inputImage);
sizeInputVec = size(inputImage);
outputImage = zeros([size(X) sizeInputVec(3:end)],'like',inputImage);
else
P = 1;
outputImage = zeros(size(X),'like',inputImage);
end
%inputImage = padImage(inputImage, fillValues);
for plane = 1:P
outputImage(:,:,plane) = interp2(inputImage(:,:,plane),X,Y,method,fillValues(plane));
end
outputImage = cast(outputImage,inputClass);
end