Skip to content

Instantly share code, notes, and snippets.

@StefRe
Created June 11, 2020 20:48
Show Gist options
  • Save StefRe/e27f8a1e379868cd279515923deefc6b to your computer and use it in GitHub Desktop.
Save StefRe/e27f8a1e379868cd279515923deefc6b to your computer and use it in GitHub Desktop.
2D interpolation
from scipy import interpolate
import numpy as np
array = np.array([[0,20,10], [0,10,5]])
new_shape = (2, 21)
f = interpolate.interp2d(np.linspace(0, new_shape[1], array.shape[1]), np.linspace(0, new_shape[0], array.shape[0]), array)
new_array = f(np.linspace(0, new_shape[1], new_shape[1]), np.linspace(0, new_shape[0], new_shape[0]))
# array([[ 0. , 2. , 4. , 6. , 8. , 10. , 12. , 14. , 16. , 18. , 20. ,
# 19. , 18. , 17. , 16. , 15. , 14. , 13. , 12. , 11. , 10. ],
# [ 0. , 1. , 2. , 3. , 4. , 5. , 6. , 7. , 8. , 9. , 10. ,
# 9.5, 9. , 8.5, 8. , 7.5, 7. , 6.5, 6. , 5.5, 5. ]])
# see https://stackoverflow.com/questions/62323694/how-to-prevent-repeated-values-when-doing-cv2-resize for comparison with cv2.resize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment