c++opencvimage-processingcomputer-visionfeature-descriptor

Re-using descriptors with BOWImgDescriptorExtractor


I have the following code which is intended to cluster a set of images via their SIFT feature descriptors.

cv::BOWKMeansTrainer trainer = cv::BOWKMeansTrainer(n_clusters);

for (Image* image : get_images()) {
    trainer.add(image->get_descriptors());
}

cv::Mat vocabulary = trainer.cluster();
cv::BOWImgDescriptorExtractor extractor(Image::get_extractor(), Image::get_matcher());
extractor.setVocabulary(vocabulary);

for (Image* image : get_images()) {
    cv::Mat bow_descriptor;
    extractor.compute(image->get_data(), image->get_key_points(), bow_descriptor);

    // Determine which cluster the image matches best, via bow_descriptor..
}

The problem I have, is that I have already computed the descriptors for the images at the point I call BowImgDescriptorExtractor::compute, and so it would be ideal if I could provide these rather than BowImgDescriptorExtractor::compute re-calculating them. As you can see, I am able to provide the key-points, but not able to find a way to provide the descriptors.

Is there any way for me to re-use the descriptors I have already created here?


Solution

  • I have resorted to writing my own version of BOWImgDescriptorExtractor, which allows me to directly pass in the descriptors rather than having to re-compute them.

    I simply re-used the existing source code, but changed the method signature to allow me to pass in the descriptors, rather than the image data and key points, and also of course removed the unnecessary calculations in the method body.

    Note: I'm currently running version 2.4.9 of OpenCV, but it appears that in version 3.0.0 (which is not yet released), they have overloaded compute to address this issue.