Assertion failed while drawing match Key Points BF BRISK+FREAK

I am trying to stitch two images using BRISK+FREAK

Here’s the code, When I try to draw the matches I get an error

error: OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225: error: (-215:Assertion failed) i1 >= 0 && i1 < static_cast(keypoints1.size()) in function ‘drawMatches’

import cv2
import numpy as np
import matplotlib.pyplot as plt

trainImg = cv2.imread('/content/img1.JPG')
trainImg_gray = cv2.cvtColor(trainImg, cv2.COLOR_BGR2GRAY)

queryImg = cv2.imread('/content/img2.JPG')
queryImg_gray = cv2.cvtColor(queryImg, cv2.COLOR_BGR2GRAY)

def detectAndDescribe(image, method=None):
    Compute key points and feature descriptors using an specific method
    descriptor = cv2.BRISK_create()
    # get keypoints and descriptors
    (kps, features) = descriptor.detectAndCompute(image, None)

    freakExtractor = cv2.xfeatures2d.FREAK_create()
    keypoints,descriptors= freakExtractor.compute(image,kps)

    return (keypoints, features)

method = 'brisk'
feature_extractor = 'brisk'
feature_matching = 'bf'
kpsA, featuresA = detectAndDescribe(trainImg_gray, method=feature_extractor)
kpsB, featuresB = detectAndDescribe(queryImg_gray, method=feature_extractor)

"Create and return a Matcher Object"
createMatcher = lambda crossCheck :  cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=crossCheck)

def matchKeyPointsBF(featuresA, featuresB, method):
    bf = createMatcher(crossCheck=True)
    # Match descriptors.
    best_matches = bf.match(featuresA,featuresB)
    # Sort the features in order of distance.
    # The points with small distance (more similarity) are ordered first in the vector
    rawMatches = sorted(best_matches, key = lambda x:x.distance)
    print("Raw matches (Brute force):", len(rawMatches))
    return rawMatches

print("Using: {} feature matcher".format(feature_matching))

fig = plt.figure(figsize=(20,8))

matches = matchKeyPointsBF(featuresA, featuresB, method=feature_extractor)
img3 = cv2.drawMatches(trainImg,kpsA,queryImg,kpsB,matches,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)


This is the complete error I get

Using: bf feature matcher Raw matches (Brute force): 1967
--------------------------------------------------------------------------- error Traceback (most recent call
last) in ()
5 matches = matchKeyPointsBF(featuresA, featuresB, method=feature_extractor)
----> 6 img3 = cv2.drawMatches(trainImg,kpsA,queryImg,kpsB,matches,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

error: OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225:
error: (-215:Assertion failed) i1 >= 0 && i1 <
static_cast(keypoints1.size()) in function ‘drawMatches’

Can’t seem to know what is going wrong here, found this OpenCV Sift/Surf/Orb : drawMatch function is not working well not able to understand how to correct this


check the contents of all objects (arrays) you pass to the call. one of them is probably empty when it should not be.

Yes it is. How do I use FREAK then in opencv-python?
Here’s a post FREAK Descriptor with Opencv Python but not so helpful

I don’t know what’s going on.

this works for me:

import numpy as np
import cv2 as cv

img = cv.imread("someimage.png")

brisk = cv.BRISK_create()
freak = cv.xfeatures2d.FREAK_create()

(kps_brisk, descrs_brisk) = brisk.detectAndCompute(img, None)
print(len(kps_brisk), len(descrs_brisk))

(kps_freak, descrs_freak) = freak.compute(img, kps_brisk)
print(len(kps_freak), len(descrs_freak))

is your image empty or blank?

by the way, from your detectAndDescribe you return keypoints from FREAK and features from BRISK… that looks wrong. they won’t be the same number of things. they should be, if you want to use them together.

use both keypoints and descriptors from either FREAK or BRISK.

oh, right, you were cross-posting, and someone over there already gave you the answer!

I want to test how the stitching on images takes place with different keypoints and descriptors Image Panorama Stitching with OpenCV