I moved on to the rotated images for warping however applying homography does not rotate…
- User
I moved on to the rotated images for warping however applying homography does not rotate the image to correct rotation. Can you know why, does the rotation not get calculated by using ORB, SURF or my warp can not apply rotation ? Here is my warp function: "def warp_perspective(img, H, reverse=True): # The function to warp images by applying reversed homography matrix min_x, min_y, max_x, max_y = get_warped_image_bounds(img, H) # Trying to predict the shape of the warped image in a smart way w, h = int(max_y - min_y), int(max_x - min_x) # Calculating width and height from the x and y
if reverse: H = np.linalg.inv(H) # Reversing the homography # The general explanation is this: # Instead of applying homography to image which can result in glitched image, for example empty pixels # After the warped image shape is estimated, we apply the homography in reverse and use the image as a lookup table # This way no pixel will be empty or glitched, all pixels will get a value target_y, target_x = np.meshgrid(np.arange(min_x, max_x), np.arange(min_y, max_y)) # Creating coordinate matrices that represents a canvas for the warped image target_coordinates = np.stack([target_x.ravel(), target_y.ravel(), np.ones(target_x.size)]) # Vectorizing the coordinate matrix, using vectorized solution to compute is way faster source_coordinates = np.dot(np.linalg.inv(H), target_coordinates) # Applying the reversed homography to target coordinates to get where they corespond in source image source_coordinates /= source_coordinates[2, :] # Normalizing the coordinates valid = np.logical_and(np.logical_and(0 <= source_coordinates[0, :], source_coordinates[0, :] < img.shape[1]), np.logical_and(0 <= source_coordinates[1, :], source_coordinates[1, :] < img.shape[0])) # Creating a mask from the valid coordinates valid_source_coordinates = source_coordinates[:, valid].astype(int) # Applying mask to coordinate systems to get image cutouts valid_target_coordinates = target_coordinates[:, valid].astype(int) valid_source_coordinates[0] = np.clip(valid_source_coordinates[0], -np.inf, img.shape[1] - 1) # Clipping the coordinates to the image shape valid_source_coordinates[1] = np.clip(valid_source_coordinates[1], -np.inf, img.shape[0] - 1) # This is another part where the fine adjustment is made # After applying homography matrix, the point of interest aligns to the left edge # However there is data in the negative region # In order to make all data visible, the image is transformed to the right and bottom as much as the calculated negative minimum corner # In the future (merging) this transform is substracted to align images back # However while developing or printing out warped images this transform allows all of the image to be warped valid_target_coordinates[0] += int(abs(min_y)) valid_target_coordinates[1] += int(abs(min_x)) valid_target_coordinates[0] = np.clip(valid_target_coordinates[0], -np.inf, w - 1) # Clipping target coordinates valid_target_coordinates[1] = np.clip(valid_target_coordinates[1], -np.inf, h - 1) warped_image = np.zeros((h, w, 3), dtype=np.uint8) # Creating an array to hold warped image 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]] # Replacing the pixels from the source image return warped_image # Returning warped image" - Assistant
The function you provided is for warping the images, which means applying a homography matrix to change the perspective of an image. It does not handle any rotation or scale changes directly, as they are assumed to be included in the homography matrix.
ORB and SURF features can directly calculate an affine transformation (which includes rotation and scaling) between images when there are enough matching features. However, the homography matrix represents a more complex transformation, including changes in perspective. If you want to focus only on correcting rotations and scalings, you might want to consider using an affine transformation instead.