I have a image warp code, however this warp code takes a target size to set itself.…
- User
I have a image warp code, however this warp code takes a target size to set itself. However I don't know the target size before hand, can we not calculate target size by applying homography to source size somehow ? Here is my code: "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)])
codeif 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" - Assistant
To calculate the target size from the source size, you can apply the homography matrix to the corners of the source image and compute the bounding box of the transformed corners. Here's how you can modify your code to achieve this:
python - User
I got a problem I am not sure if it is related to warp perspective but it started happening after your code change. What is happening is the image interest point gets aligned to left and the images lose the data of what ever is left of the interest point. So the left side of the image gets cropped. This is not what I want as I don't want any data to be lost and final warped image to be as big as it needs to be so it gets the whole image. And may be center the interest point so when I am merging I can use the center of each image to stitch together.
- Assistant