how to use SIFT features for bag of words in opencv?

4.4k views Asked by At

I have read a lot of articles about implementing bag of words after taking sift features of an image, but I'm still confused what to do next. What do i specifically do?

Thank you so much in advance for the guidance.

This is the code that i have so far.

cv::Mat mat_img = cropped.clone();
Mat grayForML;
cvtColor(mat_img, grayForML, CV_BGR2GRAY);
IplImage grayImageForML = grayForML.operator IplImage();


//create another copy of iplGray
IplImage *input = cvCloneImage(&grayImageForML);
Mat matInput = cvarrToMat(input);
//  Mat matInput = copy_gray.clone();
cv::SiftFeatureDetector detector;
std::vector<cv::KeyPoint> keyPoints;
detector.detect(input, keyPoints);
//add results to image and save.
cv::Mat output;
cv::drawKeypoints(input, keyPoints, output);    //SIFT OUTPUT RESULT


//resize and display
cv::Mat output_reduced;
cv::resize(output, output_reduced, cv::Size2i(output.cols / 2, output.rows / 2));


imshow("SIFT result", output_reduced);
1

There are 1 answers

7
Mido On BEST ANSWER

Training a bag of words system goes as follows:

  1. Compute the features for each image of the training set
  2. Cluster those features
  3. Label each cluster with the images that have features in that cluster

At this point the training is done and you can start with the testing as follows:

  1. Compute the features of the test image
  2. For each feature, find the nearest cluster
  3. Add a tick for each training image that belong to this cluster
  4. Repeat for all features of the test image
  5. The image that has the highest number of ticks is the best match and the image with the second highest number of ticks is the second best match and so on

As you can notice, there is no restriction to using SIFT. You can try different feature extractors and descriptors.