MasteringOpenCV / code

Code for the book "Mastering OpenCV with Practical Computer Vision Projects" by Packt Publishing 2012.
Other
2.72k stars 1.64k forks source link

BFMatcher raises error for Python interface when crossCheck option is enabled #46

Closed erogol closed 8 years ago

erogol commented 9 years ago

bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) matches = bf.knnMatch(descs,descs2, k=2)

raises

in () 24 # BFMatcher with default params 25 bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) ---> 26 matches = bf.knnMatch(descs,descs2, k=2) 27 28 # Apply ratio test error: /home/retina18/Downloads/Install-OpenCV-master/Ubuntu/OpenCV/opencv-2.4.10/modules/core/src/stat.cpp:2500: error: (-215) K == 1 && update == 0 && mask.empty() in function batchDistance If I remove crossCheck option this works fine but I really need cross checking :(
dereks commented 8 years ago

I get this same error. Compiled OpenCV 3.0.0 from source on Ubuntu 14.04.

dereks commented 8 years ago

Found explanation here: https://github.com/MasteringOpenCV/code/issues/5

crossCheck is an alternative to the ratio test. It breaks knnMatch. So either use crossCheck=False and then do the ratio test, or use crossCheck=True and use bf.match() instead of bf.knnMatch().

BloodAxe commented 8 years ago

Cross-check does matching of two sets of descriptors D1 and D2 in both directions (D1 -> D2 and D2 <- D1) retaining matches that exists in both.

Knn-match searches for N-best candidate for each descriptor. But it's impossible to make Cross-check between D1 and D2[N]. Therefore you have exception. As @dereks mentioned either you make cross-check or do knn-match with ratio test.

dereks commented 8 years ago

But it's impossible to make Cross-check between D1 and D2[N].

I think it would be reasonable for crossCheck to compare D1[0] <-> D2[0] in the case of knnMatch(). That would validate the points by making sure the "best" matches were all crossChecked, and furthermore, they would then be subject to the distance ratio test between D[0] and D[1].

If I understand the algorithms correctly, this would apply two types of quality filtering, giving (perhaps?) better matching results with fewer outliers.

Consider this a request to re-open this bug as a new wishlist feature.

sakthigeek commented 6 years ago

I too would like this feature of combining ratio test and crosscheck to improve matching results to be added to a new wishlist feature. If it would help in decreasing the outliers, then it is worth the effort.

sabarishkumaramaravadi commented 5 years ago

I am getting the same error(in opencv 3.x). I tried in opencv 2.x with crossCheck=True and bf.matcher.knnMatch(instead of bf.matcher.match). I got better results(less outliers). Is this problem, resolved or anyone has a solution?, please let me know.