can you help me fix error in warpperspective(img, H, targetshape) 147 warpedimage =…
- User
can you help me fix error in warp_perspective(img, H, target_shape) 147 warped_image = np.zeros((w, h, 3), dtype=np.uint8) # Swap w and h 148 for i in range(3): --> 149 warped_image[..., i][valid_target_coordinates[1], valid_target_coordinates[0]] = img[..., i][valid_source_coordinates[1], valid_source_coordinates[0]] 150 151 return warped_image
IndexError: index 752 is out of bounds for axis 0 with size 653, The full code is here: "import numpy as np import cv2 import os import glob import matplotlib.pyplot as plt import time
def feature_extraction(sub_images, method="SIFT"): if method == "SIFT": keypoint_extractor = cv2.xfeatures2d.SIFT_create() elif method == "SURF": keypoint_extractor = cv2.xfeatures2d.SURF_create() elif method == "ORB": keypoint_extractor = cv2.ORB_create()
keypoints = [] descriptors = [] for sub_image in sub_images: keypoint, descriptor = keypoint_extractor.detectAndCompute(sub_image, None) keypoints.append(keypoint) descriptors.append(descriptor) return keypoints, descriptorsdef feature_matching(descriptors, matcher_type="BF"): if matcher_type == "BF": matcher = cv2.BFMatcher()
matches = [] for i in range(1, len(descriptors)): match = matcher.knnMatch(descriptors[0], descriptors[i], k=2) matches.append(match) return matchesdef compute_homography_matrix(src_pts, dst_pts): A = []
for i in range(4): u1, v1 = src_pts[i] u2, v2 = dst_pts[i] A.append([-u1, -v1, -1, 0, 0, 0, u1 * u2, v1 * u2, u2]) A.append([0, 0, 0, -u1, -v1, -1, u1 * v2, v1 * v2, v2]) A = np.array(A) _, _, VT = np.linalg.svd(A) h = VT[-1] H = h.reshape(3, 3) return Hdef filter_matches(matches, ratio_thres=0.7): filtered_matches = [] for match in matches: good_match = [] for m, n in match: if m.distance < ratio_thres * n.distance: good_match.append(m) filtered_matches.append(good_match)
return filtered_matchesdef find_homography(keypoints, filtered_matches): homographies = [] skipped_indices = [] # Keep track of skipped images and their indices
for i, matches in enumerate(filtered_matches): src_pts = np.float32([keypoints[0][m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) dst_pts = np.float32([keypoints[i + 1][m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) H = ransac_homography(src_pts, dst_pts) if H is not None: H = H.astype(np.float32) homographies.append(H) else: print(f"Warning: Homography computation failed for image pair (0, {i + 1}). Skipping.") skipped_indices.append(i + 1) # Add indices of skipped images to the list continue return homographies, skipped_indicesdef ransac_homography(src_pts, dst_pts, iterations=500, threshold=3): best_inlier_count = 0 best_homography = None
- Assistant
It seems there is an issue with the valid_target_coordinates and valid_source_coordinates array dimensions. Try switching the order of the index in these lines:
valid_target_coordinates[1], valid_target_coordinates[0]] = img[..., i][valid_source_coordinates[1], valid_source_coordinates[0]]to,
valid_target_coordinates[0], valid_target_coordinates[1]] = img[..., i][valid_source_coordinates[0], valid_source_coordinates[1]]