Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 of 4 tasks
manasmacherla opened this issue Oct 22, 2024 · 0 comments
Open
3 of 4 tasks

Comments

@manasmacherla
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant