pythonopencvimage-stitchingbriskfreak

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)
    

plt.imshow(img3)
plt.show()

This is the complete error I get

Using: bf feature matcher Raw matches (Brute force): 1967 --------------------------------------------------------------------------- error Traceback (most recent call last) in () 4 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) 7 8

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


Solution

  • You are mixing keypoints from FREAK with features from BRISK:

    Take a careful look at the code of detectAndDescribe:

    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)  # <------ keypoints from freakExtractor.compute and features from descriptor.detectAndCompute
    

    The reported exception looks random, so it was difficult to find the problem...


    You may implement detectAndDescribe as follows:


    Suggested implementation:

    def detectAndDescribe(image, method=None):
        descriptor = cv2.BRISK_create()
        kps = descriptor.detect(image) # kps, features = descriptor.detectAndCompute(image, None)
        freakExtractor = cv2.xfeatures2d.FREAK_create()
        keypoints, descriptors= freakExtractor.compute(image, kps)
        return (keypoints, descriptors)