KnnMatch does not work with K! = 1

I have python code to compare two images:

detector_FeatureDetector_1 = cv2.FastFeatureDetector_create() detector_FeatureDetector_2 = cv2.FastFeatureDetector_create() detector_DescriptorExtractor_1 = cv2.BRISK_create() detector_DescriptorExtractor_2 = cv2.BRISK_create() detector_DescriptorMatcher_1 = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck = True) detector_DescriptorMatcher_2 = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck = True) image_1 = cv2.imread('/Users/rui/image1.png') image_2 = cv2.imread('/Users/rui/image2.png') obj_descriptor_keypoints_1 = detector_FeatureDetector.detect(image_1) obj_descriptor_keypoints_2 = detector_FeatureDetector.detect(image_2) keypoints1, obj_descriptor_descriptors_1 = detector_DescriptorExtractor.compute(image_1, obj_descriptor_keypoints_1) keypoints2, obj_descriptor_descriptors_2 = detector_DescriptorExtractor.compute(image_2, obj_descriptor_keypoints_2) matches = detector_DescriptorMatcher.knnMatch(obj_descriptor_descriptors_1, obj_descriptor_descriptors_2, k=6) 

But detector_DescriptorMatcher.knnMatch() only works with k=1 . If k has a value other than 1 , the following error is returned:

 OpenCV Error: Assertion failed (K == 1 && update == 0 && mask.empty()) in batchDistance, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-3.0.0/modules/core/src/stat.cpp, line 3682 Traceback (most recent call last): File "/Users/rui/main.py", line 191, in <module> matches = detector_DescriptorMatcher.knnMatch(obj_descriptor_descriptors, obj_descriptor_descriptors_movie_frame, k=6) cv2.error: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-3.0.0/modules/core/src/stat.cpp:3682: error: (-215) K == 1 && update == 0 && mask.empty() in function batchDistance 
+5
source share
1 answer

The error is caused by the configuration of BFMatcher using crossCheck = True . For k > 1 set crossCheck = False (default constructor).

From docs :

If crossCheck==true , then the knnMatch() method with k=1 will return pairs (i,j) , so for the i-th request descriptor, the j-th descriptor in the match collection is the closest and vice versa, i.e. BFMatcher will only return matched pairs. This technique usually gives the best results with the least amount of emissions with enough matches. This is an alternative to the attitude criterion used by D. Low in SIFT paper.

+7
source

Source: https://habr.com/ru/post/1235111/


All Articles