Skip to content

flann.knnMatch sometimes outputs a queryIdx that throws an out of bounds error  #1040

Open
@manasmacherla

Description

@manasmacherla

Expected behaviour

we filter good matches from matches that are generated by flann.knnMatch using Lowe's ratio test.
usually we get the pixel location of the match in the queryImage or the trainImage by using queryIdx or trainIdx and the keypoint object.

Actual behaviour

sometimes (these are edge cases in my data) where there aren't sufficient matches in the image pair, when i try to get the pixel location of the match using keypoint objects generated by ORB using queryIdx or trainIdx, it generates an out of bound error meaning there aren't enough keypoints in the image and queryIdx is greater than the length of the keypoints.

Write here what went wrong.

Steps to reproduce

CODE:

# my initialization parameters for flann

    orb = cv2.ORB_create()

    FLANN_INDEX_LSH = 6
    index_params = dict(algorithm=FLANN_INDEX_LSH, table_number=6, key_size=12, multi_probe_level=1)
    search_params = dict(checks=50)

    flann = cv2.FlannBasedMatcher(index_params, search_params)

# working code 
matches = flann.knnMatch(prev_descriptors, descriptors, k=2)

good_matches = []
for match in matches:
    if len(match) == 2:
        m, n = match
    if m.distance < 0.7 * n.distance:
        good_matches.append(m)

for m in good_matches:
    # having to do these two below checks to ensure queryIdx is less than length of keypoints
    if m.queryIdx > len(keypoints): 
        continue
    if m.trainIdx > len(keypoints):
        continue
    
     # previous keypoints are keypoints saved from the previous image, 
     # running the feature matcher with current and previous frame of the video

    matched_points_prev.append(prev_keypoints[m.queryIdx].pt)
    matched_points_curr.append(keypoints[m.trainIdx].pt)

matched_points_prev = np.array(matched_points_prev, dtype=np.float32)
matched_points_curr = np.array(matched_points_curr, dtype=np.float32)

ERROR:

matched_points_prev.append(prev_keypoints[m.queryIdx].pt)
IndexError: tuple index out of range
  • operating system: macOS
  • architecture: ARM (apple silicon)
  • opencv-python version: 4.10.0
Issue submission checklist
  • This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
  • I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)
  • The issue is related to the build scripts in this repository, to the pre-built binaries or is a feature request (such as "please enable this additional dependency")
  • I'm using the latest version of opencv-python

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions