ok I have made an image warping function, please check it for errors: "def…
- User
ok I have made an image warping function, please check it for errors: "def warp_perspective(img, H, reverse=False): # Apply homography matrix to the source image corners if reverse: H = np.linalg.inv(H) h, w = img.shape[0], img.shape[1] corners = np.array([[0, 0, 1], [h, 0, 1], [0, w, 1], [h, w, 1]]) transformed_corners = np.dot(H, corners.T).T transformed_corners /= transformed_corners[:, 2].reshape((-1, 1)) print(img.shape) # Compute the bounding box of the transformed corners and # define the target_shape as the (height, width) of the bounding box min_x, min_y, max_x, max_y = 0, 0, 0, 0 min_x = min(min_x, transformed_corners[:, 1].min()) min_y = min(min_y, transformed_corners[:, 0].min()) max_x = max(max_x, transformed_corners[:, 1].max()) max_y = max(max_y, transformed_corners[:, 0].max()) target_shape = (int(np.round(max_y - min_y)), int(np.round(max_x - min_x))) h, w = target_shape print("a", target_shape)
codetarget_coordinates = np.zeros((h,w,img.shape[2])) center = np.array(target_shape)//2 center_source = np.dot(np.linalg.inv(H), [center[0],center[1],1]) source_center = np.array(img.shape)//2 shift = center_source - source_center for y in range(h): for x in range(w): source = np.dot(np.linalg.inv(H), [y,x,1]) s_y = int(np.floor(source[0])-shift[0]) s_x = int(np.floor(source[1])-shift[1]) if 0 <= s_y < img.shape[0] and 0 <= s_x < img.shape[1]: target_coordinates[y,x] = img[s_y, s_x] print("done") warped_image = target_coordinates display_image(img) display_image(warped_image) print(warped_image.shape) return warped_image" - Assistant