MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / bilinear_interpolate

Function bilinear_interpolate

numpy_ml/preprocessing/dsp.py:106–153  ·  view source on GitHub ↗

Estimates of the pixel values at the coordinates (x, y) in `X` via bilinear interpolation. Notes ----- Assumes the current entries in X reflect equally-spaced samples from a 2D integer grid. Modified from https://bit.ly/2NMb1Dr Parameters ---------- X : :p

(X, x, y)

Source from the content-addressed store, hash-verified

104
105
106def bilinear_interpolate(X, x, y):
107 """
108 Estimates of the pixel values at the coordinates (x, y) in `X` via bilinear
109 interpolation.
110
111 Notes
112 -----
113 Assumes the current entries in X reflect equally-spaced
114 samples from a 2D integer grid.
115
116 Modified from https://bit.ly/2NMb1Dr
117
118 Parameters
119 ----------
120 X : :py:class:`ndarray <numpy.ndarray>` of shape `(in_rows, in_cols, in_channels)`
121 An input image sampled along a grid of `in_rows` by `in_cols`.
122 x : list of length `k`
123 A list of x-coordinates for the samples we wish to generate
124 y : list of length `k`
125 A list of y-coordinates for the samples we wish to generate
126
127 Returns
128 -------
129 samples : list of length `(k, in_channels)`
130 The samples for each (x,y) coordinate computed via bilinear
131 interpolation
132 """
133 x0 = np.floor(x).astype(int)
134 y0 = np.floor(y).astype(int)
135 x1 = x0 + 1
136 y1 = y0 + 1
137
138 x0 = np.clip(x0, 0, X.shape[1] - 1)
139 y0 = np.clip(y0, 0, X.shape[0] - 1)
140 x1 = np.clip(x1, 0, X.shape[1] - 1)
141 y1 = np.clip(y1, 0, X.shape[0] - 1)
142
143 Ia = X[y0, x0, :].T
144 Ib = X[y1, x0, :].T
145 Ic = X[y0, x1, :].T
146 Id = X[y1, x1, :].T
147
148 wa = (x1 - x) * (y1 - y)
149 wb = (x1 - x) * (y - y0)
150 wc = (x - x0) * (y1 - y)
151 wd = (x - x0) * (y - y0)
152
153 return (Ia * wa).T + (Ib * wb).T + (Ic * wc).T + (Id * wd).T
154
155
156#######################################################################

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected