OpenCV  4.1.0
Open Source Computer Vision
Feature Matching

Goal

In this chapter

Basics of Brute-Force Matcher

Brute-Force matcher is simple. It takes the descriptor of one feature in first set and is matched with all other features in second set using some distance calculation. And the closest one is returned.

For BF matcher, first we have to create the BFMatcher object using cv.BFMatcher(). It takes two optional params. First one is normType. It specifies the distance measurement to be used. By default, it is cv.NORM_L2. It is good for SIFT, SURF etc (cv.NORM_L1 is also there). For binary string based descriptors like ORB, BRIEF, BRISK etc, cv.NORM_HAMMING should be used, which used Hamming distance as measurement. If ORB is using WTA_K == 3 or 4, cv.NORM_HAMMING2 should be used.

Second param is boolean variable, crossCheck which is false by default. If it is true, Matcher returns only those matches with value (i,j) such that i-th descriptor in set A has j-th descriptor in set B as the best match and vice-versa. That is, the two features in both sets should match each other. It provides consistent result, and is a good alternative to ratio test proposed by D.Lowe in SIFT paper.

Once it is created, two important methods are BFMatcher.match() and BFMatcher.knnMatch(). First one returns the best match. Second method returns k best matches where k is specified by the user. It may be useful when we need to do additional work on that.

Like we used cv.drawKeypoints() to draw keypoints, cv.drawMatches() helps us to draw the matches. It stacks two images horizontally and draw lines from first image to second image showing best matches. There is also cv.drawMatchesKnn which draws all the k best matches. If k=2, it will draw two match-lines for each keypoint. So we have to pass a mask if we want to selectively draw it.

Let's see one example for each of SURF and ORB (Both use different distance measurements).

Brute-Force Matching with ORB Descriptors

Here, we will see a simple example on how to match features between two images. In this case, I have a queryImage and a trainImage. We will try to find the queryImage in trainImage using feature matching. ( The images are /samples/c/box.png and /samples/c/box_in_scene.png)

We are using ORB descriptors to match features. So let's start with loading images, finding descriptors etc.

1 import numpy as np
2 import cv2 as cv
3 import matplotlib.pyplot as plt
4 
5 img1 = cv.imread('box.png',cv.IMREAD_GRAYSCALE) # queryImage
6 img2 = cv.imread('box_in_scene.png',cv.IMREAD_GRAYSCALE) # trainImage
7 
8 # Initiate ORB detector
9 orb = cv.ORB_create()
10 
11 # find the keypoints and descriptors with ORB
12 kp1, des1 = orb.detectAndCompute(img1,None)
13 kp2, des2 = orb.detectAndCompute(img2,None)

Next we create a BFMatcher object with distance measurement cv.NORM_HAMMING (since we are using ORB) and crossCheck is switched on for better results. Then we use Matcher.match() method to get the best matches in two images. We sort them in ascending order of their distances so that best matches (with low distance) come to front. Then we draw only first 10 matches (Just for sake of visibility. You can increase it as you like)

1 # create BFMatcher object
2 bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
3 
4 # Match descriptors.
5 matches = bf.match(des1,des2)
6 
7 # Sort them in the order of their distance.
8 matches = sorted(matches, key = lambda x:x.distance)
9 
10 # Draw first 10 matches.
11 img3 = cv.drawMatches(img1,kp1,img2,kp2,matches[:10],None,flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
12 
13 plt.imshow(img3),plt.show()

Below is the result I got:

matcher_result1.jpg
image

What is this Matcher Object?

The result of matches = bf.match(des1,des2) line is a list of DMatch objects. This DMatch object has following attributes:

Brute-Force Matching with SIFT Descriptors and Ratio Test

This time, we will use BFMatcher.knnMatch() to get k best matches. In this example, we will take k=2 so that we can apply ratio test explained by D.Lowe in his paper.

1 import numpy as np
2 import cv2 as cv
3 import matplotlib.pyplot as plt
4 
5 img1 = cv.imread('box.png',cv.IMREAD_GRAYSCALE) # queryImage
6 img2 = cv.imread('box_in_scene.png',cv.IMREAD_GRAYSCALE) # trainImage
7 
8 # Initiate SIFT detector
9 sift = cv.xfeatures2d.SIFT_create()
10 
11 # find the keypoints and descriptors with SIFT
12 kp1, des1 = sift.detectAndCompute(img1,None)
13 kp2, des2 = sift.detectAndCompute(img2,None)
14 
15 # BFMatcher with default params
16 bf = cv.BFMatcher()
17 matches = bf.knnMatch(des1,des2,k=2)
18 
19 # Apply ratio test
20 good = []
21 for m,n in matches:
22  if m.distance < 0.75*n.distance:
23  good.append([m])
24 
25 # cv.drawMatchesKnn expects list of lists as matches.
26 img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
27 
28 plt.imshow(img3),plt.show()

See the result below:

matcher_result2.jpg
image

FLANN based Matcher

FLANN stands for Fast Library for Approximate Nearest Neighbors. It contains a collection of algorithms optimized for fast nearest neighbor search in large datasets and for high dimensional features. It works faster than BFMatcher for large datasets. We will see the second example with FLANN based matcher.

For FLANN based matcher, we need to pass two dictionaries which specifies the algorithm to be used, its related parameters etc. First one is IndexParams. For various algorithms, the information to be passed is explained in FLANN docs. As a summary, for algorithms like SIFT, SURF etc. you can pass following:

1 FLANN_INDEX_KDTREE = 1
2 index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)

While using ORB, you can pass the following. The commented values are recommended as per the docs, but it didn't provide required results in some cases. Other values worked fine.:

1 FLANN_INDEX_LSH = 6
2 index_params= dict(algorithm = FLANN_INDEX_LSH,
3  table_number = 6, # 12
4  key_size = 12, # 20
5  multi_probe_level = 1) #2

Second dictionary is the SearchParams. It specifies the number of times the trees in the index should be recursively traversed. Higher values gives better precision, but also takes more time. If you want to change the value, pass search_params = dict(checks=100).

With this information, we are good to go.

1 import numpy as np
2 import cv2 as cv
3 import matplotlib.pyplot as plt
4 
5 img1 = cv.imread('box.png',cv.IMREAD_GRAYSCALE) # queryImage
6 img2 = cv.imread('box_in_scene.png',cv.IMREAD_GRAYSCALE) # trainImage
7 
8 # Initiate SIFT detector
9 sift = cv.xfeatures2d.SIFT_create()
10 
11 # find the keypoints and descriptors with SIFT
12 kp1, des1 = sift.detectAndCompute(img1,None)
13 kp2, des2 = sift.detectAndCompute(img2,None)
14 
15 # FLANN parameters
16 FLANN_INDEX_KDTREE = 1
17 index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
18 search_params = dict(checks=50) # or pass empty dictionary
19 
20 flann = cv.FlannBasedMatcher(index_params,search_params)
21 
22 matches = flann.knnMatch(des1,des2,k=2)
23 
24 # Need to draw only good matches, so create a mask
25 matchesMask = [[0,0] for i in range(len(matches))]
26 
27 # ratio test as per Lowe's paper
28 for i,(m,n) in enumerate(matches):
29  if m.distance < 0.7*n.distance:
30  matchesMask[i]=[1,0]
31 
32 draw_params = dict(matchColor = (0,255,0),
33  singlePointColor = (255,0,0),
34  matchesMask = matchesMask,
35  flags = cv.DrawMatchesFlags_DEFAULT)
36 
37 img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
38 
39 plt.imshow(img3,),plt.show()

See the result below:

matcher_flann.jpg
image

Additional Resources

Exercises