Created
December 28, 2021 16:00
Subsample a numpy array (or an image) using bilinear interpolation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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