import numpy as np
import cv2
def coordinate_inverse_mapping(image, inverse_mapping_func):
height, width = image.shape[:2]
# Create mapping arrays for x and y coordinates
# (x_old and y_old' keys i.e., number index are the new position,
# their values are the old position)
x_old = np.zeros((height, width), dtype=np.float32)
y_old = np.zeros((height, width), dtype=np.float32)
x_new = np.linspace(0, 1, width)
y_new = np.linspace(0, 1, height)
# Call the inverse mapping function to populate the new coordinate maps
for i, y in enumerate(y_new):
for j, x in enumerate(x_new):
# inverse_mapping_func: x_old, y_old -> x_old, y_old
x_old[i, j], y_old[i, j] = inverse_mapping_func(x, y)
x_old[i, j] *= (width - 1)
y_old[i, j] *= (height - 1)
return x_old, y_old