I can\'t get this function to work. Basically I am doing feature detection and matching with a reference image. I want to overlay the matched features on top of my input ima
First thing that I've noticed is that you are converting images from one color space to another. It's confusing a little bit. If you want to draw matches on greyscale images, it is not necessary to perform this image conversion. Other thing, you are detecting keypoints on RGB image and then extracting descriptors from original image(as I can see it is greyscale)
Also I suggest you to check inside of your function if images are of the same type. Just print in log inputImage
object and see if both are of type CV_8UC3
or equivalent. Basically your code should be like this:
Mat imageOut = inputImage.clone();
Features2d.drawMatches(inputImage, keypoints, templateImage, templateKeypoints, matches, imageOut);
Highgui.imwrite("result_match.jpeg", imageOut);
Answering to another your questions:
Q:I'm unsure how I would go about converting MatOfDMatch
matches to a MatOfKeypoint
object.
A:You do not need to do it, because drawMatches
function receives MatOfKeypoint
of input image and also of template image. Then when you passing MatOfDMatch
it draws matches between matched received kypoints.
Q:Also I'm not even sure if that's how the MatOfDMatch
works - doesn't it have some associativity inside of it corresponding to the descriptors?
A:Yes it have. If you will do matches.toList(0).queryIdx
and matches.toList(0).trainIdx
you will get the index of inputImage
's keypoint, which matches with templateImage
's keypoint of the first match.