Ask Your Question

OpenCV Best way to match the spot patterns

asked 2020-03-23 07:57:16 -0500

updated 2020-03-23 11:05:06 -0500

supra56 gravatar image

I'm trying to write an app for wild leopard classification and conservation in South Asia. For this, I have the main challenge to identify the leopards by their spot pattern in the forehead.

The current approach I used was,

  1. Store the known leopard forehead images as a base list
  2. Get the user-provided leopard image and crop the forehead of the leopard
  3. identify the keypoints using the SIFT algorithm
  4. Use FLANN matcher to get KNN matches
  5. Select good matches based on the ratio threshold

Sample code below:

img1 = cv.bilateralFilter(baseImg, 9, 75, 75)
img2 = cv.bilateralFilter(userImage, 9, 75, 75)

detector = cv.xfeatures2d_SIFT.create()
keypoints1, descriptors1 = detector.detectAndCompute(img1, None)
keypoints2, descriptors2 = detector.detectAndCompute(img2, None)

# FLANN parameters
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)  # or pass empty dictionary
matcher = cv.FlannBasedMatcher(index_params, search_params)

knn_matches = matcher.knnMatch(descriptors1, descriptors2, 2)

allmatchpointcount = len(knn_matches)
ratio_thresh = 0.7
good_matches = []

for m, n in knn_matches:
    if m.distance < ratio_thresh * n.distance:

goodmatchpointcount = len(good_matches)
print("Good match count : ", goodmatchpointcount)
matchsuccesspercentage = goodmatchpointcount/allmatchpointcount*100
print("Match percentage : ", matchsuccesspercentage)

Problems I have with this approach:

  1. The method has a medium-low success rate and tends to break when there is a new user image.
  2. The user images are sometimes taken from different angles where some key patterns are not visible or warped.
  3. The user image quality affects the match result significantly.

I appreciate any suggestions to get this improved in any manner.

Example user image: (Base image)

Base Image

Above is matching to below: (Incorrect pattern matched)

Matching incorrect pattern

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-03-23 09:49:58 -0500

juanmamdp gravatar image

In the example image you shared, I can see a picture with a lot of "noise". If you try to find features in this image you will surely find not only the spots, but also the hairs and things that will be difficult to find in other images with different qualities.

Try to leave only what you want to focus on by applying some image processing techniques (blur, thresholding, etc.) And then, find the features.

edit flag offensive delete link more


Yes, I'm adding a bilateral filter beforehand as a preprocessing step. which takes care of the noise pretty effectively. I mentioned that in the question. Is there anything else I can do?

img1 = cv.bilateralFilter(img1, 9, 75, 75)
debugger89 gravatar imagedebugger89 ( 2020-03-23 21:13:58 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-03-23 07:57:16 -0500

Seen: 68 times

Last updated: Mar 23