 <p>I also did something similar based on this code:<br></p> <p><a href="https://github.com/royshil/FoodcamClassifier/blob/master/training_common.cpp" rel="nofollow">https://github.com/royshil/FoodcamClassifier/blob/master/training_common.cpp</a></p> <p>But the above part is after the clustering has finished. What you have to do is to train using your ML(I used SVM) and your cluster centers, the visual bag of words that you have. More, you need to find all the "closest" points to your clustered points and train them using histograms. Next, you will have a histogram of frequencies(bag of key points) that you need to train.</p> <pre><code>Ptr&lt;ifstream&gt; ifs(new ifstream("training.txt")); int total_samples_in_file = 0; vector&lt;string&gt; classes_names; vector&lt;string&gt; lines; //read from the file - ifs and put into a vector for(int i=0;i&lt;lines.size();i++) { vector&lt;KeyPoint&gt; keypoints; Mat response_hist; Mat img; string filepath; string line(lines[i]); istringstream iss(line); iss &gt;&gt; filepath; string class_to_train; iss &gt;&gt; class_to_train; class_ml = "class_" + class_to_train; if(class_ml.size() == 0) continue; img = imread(filepath); detector-&gt;detect(img,keypoints); bowide.compute(img, keypoints, response_hist); cout &lt;&lt; "."; cout.flush(); //here create the logic for the class to train(class_0, e.g) and the data you need to train. } </code></pre> <p>More you can find at this git project:<br> <a href="https://github.com/royshil/FoodcamClassifier" rel="nofollow">https://github.com/royshil/FoodcamClassifier</a> <br> Documentation here:<br> <a href="http://www.morethantechnical.com/2011/08/25/a-simple-object-classifier-with-bag-of-words-using-opencv-2-3-w-code/" rel="nofollow">http://www.morethantechnical.com/2011/08/25/a-simple-object-classifier-with-bag-of-words-using-opencv-2-3-w-code/</a></p>