opencv / opencv/opencv-python

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

Open
#1,040 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.4k
Forks
1k
Avg merge
22h 17m
Merged PRs (30d)
3

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the repository README and the reported flann.knnMatch and ORB reproduction using opencv-python 4.10.0. Determine whether the behavior involves this packaging toolchain or OpenCV itself; done should include evidence that the build or pre-built package is responsible, rather than only application-level matching behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
opencv, python
Domain
computer-vision
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.