Assertion failed queryDescriptors.type trainDescCollection0.type in knnMatchImpl,
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
This OpenCV assertion means the two descriptor matrices passed into knnMatch do not have the same underlying OpenCV type. In practice, the failure usually happens when one side contains binary descriptors such as ORB output and the other side contains floating-point descriptors such as SIFT output, or when one array has been converted incorrectly.
Why knnMatch rejects mixed descriptor types
Feature matching only works when the matcher knows how to compare like with like. OpenCV stores descriptors in matrices with specific dtypes and expects the query and train sets to match. Typical combinations are:
- ORB, BRISK, and AKAZE binary descriptors with
uint8 - SIFT and SURF style descriptors with
float32
If you compute descriptors with different algorithms, the shapes may both look valid, but the matcher still cannot compare them directly. That is what this assertion is protecting against.
A second source of trouble is loading saved descriptors from disk and accidentally producing float64 NumPy arrays. OpenCV commonly expects float32 for float descriptors, so even "almost right" can still fail.
Use matching detector and matcher pairs
The safest fix is to use the same descriptor extractor on both images and pair it with the correct matcher norm.
For ORB:
For SIFT:
Notice that the fix is not "force everything to one type no matter what." Binary and float descriptors represent different things, so converting ORB bytes to floats does not magically make them compatible with SIFT.
Diagnose the actual type mismatch
Before changing code, inspect the descriptors directly:
If one side is uint8 and the other is float32, the extraction pipeline is inconsistent. If both come from the same algorithm but one is float64, a simple cast may be enough:
Only do that when the descriptors are supposed to be floating-point descriptors already.
You should also check for None. If no keypoints were found in one image, detectAndCompute returns None for descriptors, and that failure can be misdiagnosed as a matching problem.
Keep the whole matching pipeline consistent
Descriptor type issues are usually symptoms of a broader pipeline inconsistency. Make sure the following stay aligned:
- both images use the same extractor when direct matching is intended
- matcher norm matches the descriptor family
- saved descriptors are reloaded with the original dtype
- preprocessing is similar enough that both images still yield keypoints
If you really need to compare features from different models, direct knnMatch is usually the wrong tool. You would need a different representation or a learned mapping, not a quick dtype conversion.
Common Pitfalls
The most common mistake is mixing ORB descriptors from one image with SIFT descriptors from the other. Those descriptors are not interchangeable, even if they both came from OpenCV.
Another mistake is using BFMatcher(cv2.NORM_L2) with ORB. ORB is binary and should normally use Hamming distance. A wrong norm may not always trigger this exact assertion, but it still produces bad matches.
Developers also run into trouble after saving descriptors with NumPy and loading them back as float64. That is easy to miss because the data "looks numeric" while still violating OpenCV's type requirements.
Finally, do not forget the empty-descriptor case. If one image has no detectable features, fix the image pipeline or handle the edge case before matching.
Summary
- '
knnMatchrequires query and train descriptors to have the same OpenCV type.' - Use the same descriptor extractor on both sides unless you have a more advanced cross-model pipeline.
- Pair ORB-like descriptors with Hamming distance and SIFT-like descriptors with L2 distance.
- Inspect
dtype, shape, andNonevalues before assuming the matcher is broken. - Do not blindly cast binary descriptors to floats just to silence the assertion.
Related reading
- Automatically add watermark to an image
- Automatically recognize patterns in images
- Bag Of Visual Words Implementation in Python is giving terrible accuracy
- Best strategy to reduce false positives Google's new Object Detection API on Satellite Imagery
- Best strategy to reduce false positives Google's new Object Detection API on Satellite Imagery
- Bi-Cubic Interpolation Algorithm for Image Scaling
- Bilinear interpolation implementations in Tensorflow and OpenCV
- Bilinear upsample in tensorflow?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.