0

I am Currently Working on a project related to Sfm Photogrammetry using Python OpenCV Library.I extracted and matched the Features using SIFT Algorithm. I tried to find the fundamental matrix by taking each keypoints and matches from adjacent points and I get Out of range error. But while just taking two keypoints and matches it worked fine. I am Getting error when I'm trying to find the points "pts1" and "pts2" which are the arguments necessary to find the fundamental matrix using the function "cv2.findFundamentalMat(pts1, pts2, cv2.FM_RANSAC)"

This the function for getting the points pts1 and pts2

import numpy as np
def extract_matched_feature_points(matches, keypoints1, keypoints2):
    """Get matched points from keypoints and matches"""
    pts1 = []
    pts2 = []
    
    for match in matches:
        # Get the index of the keypoints in the two images
        idx1 = match.queryIdx
        idx2 = match.trainIdx

        # Get the x,y coordinates of the matched keypoints
        pt1 = keypoints1[idx1].pt
        pt2 = keypoints2[idx2].pt

        # Add the points to the respective arrays
        pts1.append(pt1)
        pts2.append(pt2)
    
    # Convert the points to numpy arrays
    pts1 = np.array(pts1)
    pts2 = np.array(pts2)

    return pts1, pts2

And to Estimate the fundamental Matrix between All points,

# Loop over all pairs of images
for i in range(num_images):
    for j in range(i+1, num_images):
        # Extract matched feature points between the two images
        pts1, pts2 = extract_matched_feature_points(matches[i], kpts[i], kpts[j])

        # Estimate fundamental matrix and filter out outliers
        F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_RANSAC)

        # Filter out outlier matches
        pts1 = pts1[mask.ravel()==1]
        pts2 = pts2[mask.ravel()==1]

        # Store F and filtered matches for this pair of images
        F_list.append(F)
        pts1_list.append(pts1)
        pts2_list.append(pts2)

And I get this Error


IndexErrorTraceback (most recent call last)
<ipython-input-154-54fe88d1c0f1> in <module>()
      7     for j in range(i+1, num_images):
      8         # Extract matched feature points between the two images
----> 9         pts1, pts2 = get_matched_points(kp_list1[i], kp_list1[j],matches_list[i])
     10 
     11         # Estimate fundamental matrix and filter out outliers

<ipython-input-109-6611d51905b5> in get_matched_points(keypoints1, keypoints2, matches)
     12         # Get the x,y coordinates of the matched keypoints
     13         pt1 = keypoints1[idx1].pt
---> 14         pt2 = keypoints2[idx2].pt
     15 
     16         # Add the points to the respective arrays

IndexError: list index out of range

Can someone please Help me with this.

NOTE: I used 36 Photos as my dataset.and the length of my kp_list is 36 and matches_list is 35 (Sorry if I'm not being Clear)

  • In your example you are using `extract_matched_feature_points` and your error says `get_matched_points` with different order of arguments. So that is not the error for the minimal example? Generally, kp_lists and matches must have similar size. – Grillteller Mar 02 '23 at 19:04

0 Answers0