Ask Your Question

BOWKMeansTrainer and features extraction

asked 2018-04-07 06:40:47 -0500

lezan gravatar image

After some months, I start again to do some optimization with my code, but I forgot somethink, I think.

I need to perform Bag of Words to clusterize features extracted from images. Let's see some code.

Extract features and put them into a vector.

cv::Mat featuresVector;
for (int i = 0; i < numberImages; ++i) // <- first features extraction
    cv::Mat featuresExtracted = runExtractFeature(face, featuresExtractionAlgorithm);

Them, I want to clusterize them with BOWKMeansTrainer.

cv::BOWKMeansTrainer bowTrainer(dictionarySize, termCriteria, retries, centersFlags);
cv::Mat dictionary = bowTrainer.cluster();

Then, preare for bag of words in this way

cv::Ptr<cv::DescriptorMatcher> matcher = cv::FlannBasedMatcher::create();
cv::Ptr<cv::DescriptorExtractor> extractor = cv::xfeatures2d::SiftDescriptorExtractor::create(); // <-
cv::BOWImgDescriptorExtractor bowDE(extractor, matcher);

Now I can start bag of words

for (int i = 0; i < numberImages; ++i)
    cv::Mat face = faceMatVector[i]; // <- contains image read with imread.
    std::vector<cv::KeyPoint> keypoints;
    detector->detect(face, keypoints); <- // second one features extraction
    cv::Mat bowDescriptors;
    bowDE.compute(face, keypoints, bowDescriptors);

As you can see, in this way, I perform features extraction from image two time: first one with the first loop, and the second one with the last loop (see comments on code), because I need descriptors to clusterize with BOWKMeansTrainer, but I need keypoints to calculate bowDescriptors with BOWImgDescriptorExtractor (matching and so on).

My question is: Is this necessary or can I avoid that? I failed something? Can I take from keypoints from somewhere in the last loop without re-detect them? Can I just save keypoints detected in the first loop and then re-use them in the last loop to computer BOWImgDescriptorExtractor?

Thanks for your answer.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-09 03:01:04 -0500

berak gravatar image

if you kept the descriptors for your images around, you can use them without recalculating

edit flag offensive delete link more


So can I use descriptors saved into featuresVector? Seems awesome, did not see this overloading.

lezan gravatar imagelezan ( 2018-04-09 12:37:54 -0500 )edit

well, IF it's from the same images, yes.

berak gravatar imageberak ( 2018-04-09 12:40:17 -0500 )edit

Question Tools

1 follower


Asked: 2018-04-07 06:40:47 -0500

Seen: 109 times

Last updated: Apr 09 '18