I am having error: 50 for i in range(len(images)): 51 warpedimage =…
- User
I am having error: 50 for i in range(len(images)): 51 warped_image = cv2.warpPerspective(images[i], homography_list[i-1] if i>0 else np.eye(3), new_size) ---> 52 panorama[:,curr_x:curr_x+new_size[0]] = kayla_tools.merge_images(cv2.cvtColor(images[i], cv2.COLOR_GRAY2BGR), cv2.cvtColor(warped_image, cv2.COLOR_GRAY2BGR), homography_list[i-1] if i>0 else np.eye(3)) 53 curr_x += new_size[0] 54
ValueError: could not broadcast input array from shape (1946,2300,3) into shape (973,1232,3)
- Assistant
This error message suggests that there is a problem with the size of the images or the way they are being merged in the code. The input array of shape (1946,2300,3) cannot be broadcast into the shape (973,1232,3), which means that the dimensions of the images are different and cannot be merged together.
To fix this error, you can try resizing the images to the same size using cv2.resize() function before merging them. This can be done by adding the following line of code before the loop:
images = [cv2.resize(img, (1232, 973)) for img in images]
Alternatively, you can adjust the code that merges the images to account for different sizes by creating an empty array of the correct size before merging:
panorama = np.zeros((max_height, curr_x+new_size[0], 3), dtype=np.uint8)
This will create a new array of size (max_height, curr_x+new_size[0], 3) that can accommodate images of different sizes. Then, you can use cv2.resize() to adjust each image before merging it into the panorama array.
- User
hmm I think problem is the code is wrong, there is warped_image = cv2.warpPerspective(images[i], homography_list[i-1] if i>0 else np.eye(3), new_size) line while the called function already does warped_img2 = np.zeros((max_h, max_w, 3), dtype=np.uint8)
- Assistant
You are correct that this may be causing the error. The issue could be that
warped_imageis being resized tonew_sizeusingcv2.warpPerspective()even though the dimensions ofmax_handmax_w, which are used to createwarped_img2, are not the same asnew_size.One solution is to modify the
cv2.warpPerspective()line to warp the image to the size ofwarped_img2. This can be done as follows:# change this line warped_image = cv2.warpPerspective(images[i], homography_list[i-1] if i>0 else np.eye(3), new_size) # to this warped_image = cv2.warpPerspective(images[i], homography_list[i-1] if i>0 else np.eye(3), (max_w, max_h))This should ensure that the dimensions of the
warped_imagematch those of thewarped_img2created in themerge_images()function, so that they can be concatenated to form the final panorama image without issues. - User
I think I need to remove warped_img2 = np.zeros((max_h, max_w, 3), dtype=np.uint8) line as the warping is already handled in merge_images function
- Assistant
Yes, that's correct. If the warping is already handled in the
merge_images()function, then you don't need to createwarped_img2outside of the function.You can simply remove the line
warped_img2 = np.zeros((max_h, max_w, 3), dtype=np.uint8)from your code, and modify thecv2.warpPerspective()line to useoutput_sizeas the size parameter instead ofnew_size:warped_image = cv2.warpPerspective(images[i], homography_list[i-1] if i>0 else np.eye(3), output_size)This should ensure that the dimensions of the warped image matches that of the final panorama image, which is created inside the
merge_images()function. - User
I will give you merge images function here: def merge_images(img1, img2, H): # Determine size of panorama h1, w1 = img1.shape[:2] h2, w2 = img2.shape[:2] if H[2, 2] != 0: scale_factor = 1 / H[2, 2] else: scale_factor = 1 max_w = int(scale_factor * (w1 + w2)) max_h = int(scale_factor * (h1 + h2))
# Warp img2 to img1 coordinate system warped_img2 = np.zeros((max_h, max_w, 3), dtype=np.uint8) for j in range(max_w): for i in range(max_h): p = np.array([j/scale_factor, i/scale_factor, 1]) q = np.dot(H, p) x, y = int(q[0]/q[2]), int(q[1]/q[2]) if x >= 0 and x < w2 and y >= 0 and y < h2: warped_img2[i, j, :] = img2[y, x, :] # Blend the two images blended_img = np.zeros((max_h, max_w, 3), dtype=np.uint8) blended_img[:h1, :w1, :] = img1 for i in range(h2): for j in range(w2): x = int(scale_factor * j + 0.5 * scale_factor) y = int(scale_factor * i + 0.5 * scale_factor) if warped_img2[y, x, :].all() != 0: blended_img[y, x, :] = warped_img2[y, x, :] return blended_img - Assistant
Thank you for providing the
merge_images()function.In this case, you're correct that the image warping is already handled inside this function. This means that you don't need to use
cv2.warpPerspective()to warp the image in the main loop. Instead, you can simply pass the images to this function and it will take care of the warping.So, you can remove the
warped_imageline from the main loop, and modify the panorama merging line to pass the resulting images fromcv2.cvtColor()tomerge_images()instead:panorama[:, curr_x:curr_x+new_size[0]] = merge_images(cv2.cvtColor(images[i], cv2.COLOR_GRAY2BGR), cv2.cvtColor(images[i-1], cv2.COLOR_GRAY2BGR), homography_list[i-1] if i>0 else np.eye(3))By doing this, you'll be passing the two images to
merge_images()directly, along with the Homography matrix, for them to be blended and output as a single image, with the warping applied. - User
sadly I still got the error: ---> 51 panorama[:, curr_x:curr_x+new_size[0]] = kayla_tools.merge_images(cv2.cvtColor(images[i], cv2.COLOR_GRAY2BGR), cv2.cvtColor(images[i-1], cv2.COLOR_GRAY2BGR), homography_list[i-1] if i>0 else np.eye(3)) 52 curr_x += new_size[0] 53
ValueError: could not broadcast input array from shape (1626,2054,3) into shape (973,1232,3)
- Assistant
It seems that the error still persists. One thing you can try is to resize the images before merging using
merge_images()function. Since themerge_images()function is designed to handle images of different sizes, it may be helpful to resize the images to a consistent size before passing them to the function.You can use the following code to resize each image to
(1232, 973):images_resized = [] for img in images: img_resized = cv2.resize(img, (1232, 973)) images_resized.append(img_resized)Then, you can use
images_resizedinstead ofimagesin your loop, and pass the resized images tomerge_images()function:panorama[:, curr_x:curr_x+new_size[0]] = merge_images(cv2.cvtColor(images_resized[i], cv2.COLOR_GRAY2BGR), cv2.cvtColor(images_resized[i-1], cv2.COLOR_GRAY2BGR), homography_list[i-1] if i>0 else np.eye(3))This should ensure that all the images are of the same size before merging using
merge_images(), and may help to avoid the previous error. - User
we should not be doing resizing, I am thinking there is something wrong with curr_x += new_size[0] line. May be the implementation is wrong
- Assistant
The
curr_x += new_size[0]line incrementscurr_xby the width of each new image that is being added to the panorama. This ensures that the next image is placed to the right of the previous one in the panorama.However, if there is a problem with this line of code, it could potentially cause gaps or overlaps between images in the final panorama. You could try printing out the values of
curr_xandnew_size[0]during runtime to check if they are being updated and incremented correctly.Also, make sure that the dimensions of
panoramaare large enough to accommodate all the images being added to it. Ifpanoramais smaller than the combined dimensions of the images, it may cause the error you described earlier.Another thing to check is whether the dimensions of the output image returned by the
merge_images()function matchnew_size[0]. If the dimensions don't match, it could cause the dimensions of thepanoramaarray to be mismatched and lead to the error. - User
I will give you whole function so you will see where the panorama is defined: for method in methods: start_time = time.time()
# Feature extraction, matching, finding homography, and image merging steps here keypoints_list = [] descriptors_list = [] for image in images: kp, des = kayla_tools.feature_extraction(method, image) keypoints_list.append(kp) descriptors_list.append(des) matches_list = [] for i in range(len(images)-1): matches = kayla_tools.feature_matching(descriptors_list[i], descriptors_list[i+1], method) matches_list.append(matches) homography_list = [] for i in range(len(matches_list)): src_pts = np.float32([keypoints_list[i][match.queryIdx].pt for match in matches_list[i]]) dst_pts = np.float32([keypoints_list[i+1][match.trainIdx].pt for match in matches_list[i]]) homography = kayla_tools.find_homography_ransac(src_pts, dst_pts) homography_list.append(homography) image_sizes = [image.shape[::-1] for image in images] new_size = tuple(map(max, zip(*image_sizes))) panorama = np.zeros((new_size[1], sum(new_size), 3), dtype=np.uint8) curr_x = 0 for i in range(len(images)): panorama[:, curr_x:curr_x+new_size[0]] = kayla_tools.merge_images(cv2.cvtColor(images[i], cv2.COLOR_GRAY2BGR), cv2.cvtColor(images[i-1], cv2.COLOR_GRAY2BGR), homography_list[i-1] if i>0 else np.eye(3)) curr_x += new_size[0] runtime = time.time() - start_time print(f"{method} runtime: {runtime}s") # Display your results (plot keypoints, matching lines, panorama, etc.) fig, ax = plt.subplots() ax.imshow(cv2.cvtColor(panorama, cv2.COLOR_BGR2RGB)) ax.axis("off") plt.title(f"{method} Panorama") plt.show() - Assistant
Thank you for providing the full code.
In this case, the error could be caused by the dimensions of the
panoramaarray not matching the combined size of the images being added to it.Currently,
panoramais defined with a shape of(new_size[1], sum(new_size), 3). This will create an array with a height ofnew_size[1]and a width that is the sum of the widths of all the images.However, if the height of the images is different, this may cause the error you are seeing. One solution is to set the height of the
panoramaarray to the maximum height of all the images, to ensure that all images will fit within the array. You can use the following code to set the height of the array to the maximum height: - User
I got the exact same error: ---> 55 panorama[:, curr_x:curr_x + image_sizes[i][0]] = kayla_tools.merge_images(cv2.cvtColor(images[i], cv2.COLOR_GRAY2BGR), cv2.cvtColor(images[i-1], cv2.COLOR_GRAY2BGR), homography_list[i-1] if i>0 else np.eye(3)) 56 curr_x += image_sizes[i][0] 57
ValueError: could not broadcast input array from shape (1626,2054,3) into shape (973,1068,3)
- Assistant
In that case, it seems that even with the changes to the
panoramaarray definition and the updatedcurr_xvariable, there is still an issue with the dimensions of the images being merged.One possibility is that the
merge_images()function is returning images with a different size than expected. You can check if the size of the output ofmerge_images()matchesimage_sizes[i]andimage_sizes[i-1].Another possibility is that some of the original images are already smaller than
(1232, 973), causing the error message despite the resizing code not being run. In that case, you could check the size of each image and discard any images that are too small, or resize all images to have a consistent size usingcv2.resize().It may also help to add some print statements to the loop to check the dimensions of
image_sizes,image_sizes[i][0], andimage_sizes[i-1][0]during runtime, to see where the issue is occurring.I hope this helps. Let me know if you have any further questions!
- User
okay tell me the lines to add for the print statements to check all those
- Assistant
You can add the following print statements to the loop to check the values of
image_sizes,image_sizes[i][0], andimage_sizes[i-1][0]during runtime:image_sizes = [image.shape[::-1] for image in images] new_size = tuple(map(max, zip(*image_sizes))) # Add this loop to print the size of each image for i, image_size in enumerate(image_sizes): print(f"Size of image {i}: {image_size}") panorama = np.zeros((max_height, sum(new_size), 3), dtype=np.uint8) curr_x = 0 for i in range(len(images)): print(f"Current image size: {image_sizes[i]}") print(f"Previous image size: {image_sizes[i-1]}") panorama[:, curr_x:curr_x + image_sizes[i][0]] = kayla_tools.merge_images(cv2.cvtColor(images[i], cv2.COLOR_GRAY2BGR), cv2.cvtColor(images[i-1], cv2.COLOR_GRAY2BGR), homography_list[i-1] if i>0 else np.eye(3)) curr_x += image_sizes[i][0]These print statements should help you identify where the issue is coming from. They will print the size of each image as well as the size of the current and previous image in the loop. You can use this information to check if the sizes of the images match, and to figure out where any discrepancies are coming from.
- User
here is the output: Size of image 0: (1068, 973) Size of image 1: (986, 653) Size of image 2: (986, 653) Size of image 3: (986, 653) Size of image 4: (1232, 816) Size of image 5: (986, 653) Current image size: (1068, 973) Previous image size: (986, 653)
- Assistant
Based on the output, it seems that the issue may be coming from the fact that the size of the first image is not the same as the other images, causing the error when trying to merge them together. Specifically, the size of the first image is
(1068, 973), while the size of the other images is(986, 653).To fix this issue, you can resize the first image to the same size as the other images using
cv2.resize(). You can do this before the loop, like this:images_resized = [] for i, image in enumerate(images): if i == 0: # resize first image to same size