Skip to content

Instantly share code, notes, and snippets.

@ruofeidu
Created April 10, 2018 17:29
Show Gist options
  • Save ruofeidu/e68930ed5c027a67c21522bc1403f598 to your computer and use it in GitHub Desktop.
Save ruofeidu/e68930ed5c027a67c21522bc1403f598 to your computer and use it in GitHub Desktop.
Obtain color with bilinear filtering on the CPU, equivalent to the default bilinear filtering on the GPU
# type: (list, float, float) -> list
# img: the input image
# cx, cy: image coordinates in floats
def bilinear_get_color(img, cx, cy):
x, y = [int(floor(cx)), int(ceil(cx))], [int(floor(cy)), int(ceil(cy))]
u, v = cx - x[0], cy - y[0]
r = np.zeros((2, 2, 3), np.uint8)
w, h = len(img[0]), len(img)
for i in range(2):
for j in range(2):
tx, ty = min(w - 1, max(0, x[i])), min(h - 1, max(0, y[j]))
r[i][j] = img[ty][tx]
return r[0][0] * (1 - u) * (1 - v) + r[0][1] * (1 - u) * v + r[1][0] * u * (1 - v) + r[1][1] * u * v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment