Skip to content

Instantly share code, notes, and snippets.

@TimoStoff
Created December 28, 2021 16:00
Subsample a numpy array (or an image) using bilinear interpolation
def subsample_image(coords, img):
"""
Given a list of floating point coordinates (Nx2) in the image,
return the pixel value at each location using bilinear interpolation.
"""
if len(img.shape) == 2:
img = np.expand_dims(img, 2)
xs, ys = coords[:, 0], coords[:, 1]
pxs = np.floor(xs).astype(int)
pys = np.floor(ys).astype(int)
dxs = xs-pxs
dys = ys-pys
wxs, wys = 1.0-dxs, 1.0-dys
weights = np.multiply(img[pys, pxs, :].T , wxs*wys).T
weights += np.multiply(img[pys, pxs+1, :].T , dxs*wys).T
weights += np.multiply(img[pys+1, pxs, :].T , wxs*dys).T
weights += np.multiply(img[pys+1, pxs+1, :].T , dxs*dys).T
return weights
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment