how can I apply ground truth homography but in reverse. What I mean is I got a image and…
- User
how can I apply ground truth homography but in reverse. What I mean is I got a image and a homography and that converts image to another images perspective. However I want the second image to be converted to first image perspective, meaning I need the reverse homography. Can you add reverse parameter to my function so when it is passed true, the homopgraphy will be applied in reverse. here is my function: "def warp_perspective(img, H, target_shape): h, w = target_shape target_y, target_x = np.meshgrid(np.arange(h), np.arange(w)) target_coordinates = np.stack([target_x.ravel(), target_y.ravel(), np.ones(target_x.size)])
source_coordinates = np.dot(np.linalg.inv(H), target_coordinates) source_coordinates /= source_coordinates[2, :] valid = np.logical_and(np.logical_and(0 <= source_coordinates[0, :], source_coordinates[0, :] < img.shape[1] - 1), np.logical_and(0 <= source_coordinates[1, :], source_coordinates[1, :] < img.shape[0] - 1)) # Change the dtype to int instead of float64 to save memory valid_source_coordinates = np.round(source_coordinates[:, valid].astype(np.float32)[:2]).astype(int) valid_target_coordinates = target_coordinates[:, valid].astype(int)[:2] valid_source_coordinates[0] = np.clip(valid_source_coordinates[0], 0, img.shape[1] - 1) valid_source_coordinates[1] = np.clip(valid_source_coordinates[1], 0, img.shape[0] - 1) valid_target_coordinates[0] = np.clip(valid_target_coordinates[0], 0, w - 1) valid_target_coordinates[1] = np.clip(valid_target_coordinates[1], 0, h - 1) warped_image = np.zeros((h, w, 3), dtype=np.uint8) for i in range(3): warped_image[..., i][valid_target_coordinates[1], valid_target_coordinates[0]] = img[..., i][valid_source_coordinates[1], valid_source_coordinates[0]] return warped_image" - Assistant
In order to add a reverse parameter, you should modify the function definition and decide whether to use the original homography matrix H or its inverse, depending on the value of the reverse parameter. Here's the updated function definition:
def warp_perspective(img, H, target_shape, reverse=False): h, w = target_shape target_y, target_x = np.meshgrid(np.arange(h), np.arange(w)) target_coordinates = np.stack([target_x.ravel(), target_y.ravel(), np.ones(target_x.size)]) if reverse: H = np.linalg.inv(H) source_coordinates = np.dot(np.linalg.inv(H), target_coordinates) source_coordinates /= source_coordinates[2, :] valid = np.logical_and(np.logical_and(0 <= source_coordinates[0, :], source_coordinates[0, :] < img.shape[1] - 1), np.logical_and(0 <= source_coordinates[1, :], source_coordinates[1, :] < img.shape[0] - 1)) # Change the dtype to int instead of float64 to save memory valid_source_coordinates = np.round(source_coordinates[:, valid].astype(np.float32)[:2]).astype(int) valid_target_coordinates = target_coordinates[:, valid].astype(int)[:2] valid_source_coordinates[0] = np.clip(valid_source_coordinates[0], 0, img.shape[1] - 1) valid_source_coordinates[1] = np.clip(valid_source_coordinates[1], 0, img.shape[0] - 1) valid_target_coordinates[0] = np.clip(valid_target_coordinates[0], 0, w - 1) valid_target_coordinates[1] = np.clip(valid_target_coordinates[1], 0, h - 1) warped_image = np.zeros((h, w, 3), dtype=np.uint8) for i in range(3): warped_image[..., i][valid_target_coordinates[1], valid_target_coordinates[0]] = img[..., i][valid_source_coordinates[1], valid_source_coordinates[0]] return warped_image