My merge image function just replaces the pixels with the latest image. However I am…
- User
My merge image function just replaces the pixels with the latest image. However I am thinking it is better to interpolate alpha so images down below has a visibility. This merge image function aim to create panorama images. Can you help me. Here is my code: "def image_merging(images, homographies): if len(images) - 1 != len(homographies): raise ValueError("The number of homography matrices must be one less than the number of images.")
code# Warp all images except the first one using their corresponding homography matrices warped_images = [images[0]] + [warp_perspective(img, H, True) for img, H in zip(images[1:], homographies)] # Calculate the bounds of each warped image warped_image_bounds = np.array([(0, 0, *images[0].shape[:2])] + [get_warped_image_bounds(img, H) for img, H in zip(images[1:], homographies)]) # Compute the size of the merged image min_x = min(-warped_image_bounds[:,0].max(), warped_image_bounds[:,0].min()) min_y = min(-warped_image_bounds[:,1].max(), warped_image_bounds[:,1].min()) max_x = warped_image_bounds[:,2].max() max_y = warped_image_bounds[:,3].max() print("new: ", min_x, min_y, max_x, max_y) print(warped_image_bounds) merged_width = int(np.ceil(max_y - min_y - min_y)) merged_height = int(np.ceil(max_x - min_x - min_x)) print("---") print(merged_width) print(max_x) print(min_x) print(max_x - min_x) print(np.ceil(max_x - min_x)) print(int(np.ceil(max_x - min_x))) print("---") # Initialize the merged image with zeros merged_image = np.zeros((merged_height, merged_width, 3), dtype=np.uint8) print("Merged Shape: ", merged_image.shape) # Merge the warped images by overlaying them on the merged image for img, bounds in zip(warped_images, warped_image_bounds): yy, xx= bounds[:2] print("yx: ", yy ,xx) mask = np.all(img == 0, axis=-1) y_offset, x_offset = int(max(0, -min_x+yy)), int(max(0, -min_y+xx)) merged_image_slice = merged_image[y_offset:y_offset + img.shape[0], x_offset:x_offset + img.shape[1]] print(img.shape, merged_image_slice.shape, mask.shape) merged_image_slice[~mask] = img[~mask] print("Merged Shape 2: ", merged_image.shape) return merged_image" - Assistant