title: How to train and predict using bag of words?
body: <p>I have a folder of images of a car from every angle. I want to use the bag of words approach to train the system in recognizing the car. Once the training is done, I want that if an image of that car is given it should be able to recognize it.</p>
  
  <p>I have been trying to learn the BOW function in opencv in order to make this work and have come at a level where I do not know what to do now and some guidance would be appreciated.</p>
  
  <p>Here is my code that I used to make the bag of words:</p>
  
  <pre><code>Ptr&lt;FeatureDetector&gt; features = FeatureDetector::create("SIFT");
      Ptr&lt;DescriptorExtractor&gt; descriptors = DescriptorExtractor::create("SIFT");
      Ptr&lt;DescriptorMatcher&gt; matcher = DescriptorMatcher::create("FlannBased");
  
      //defining terms for bowkmeans trainer
      TermCriteria tc(MAX_ITER + EPS, 10, 0.001);
      int dictionarySize = 1000;
      int retries = 1;
      int flags = KMEANS_PP_CENTERS;
      BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
  
      BOWImgDescriptorExtractor bowDE(descriptors, matcher);
  
      //training data now
      Mat features;
      Mat img = imread("c:\\1.jpg", 0);
      Mat img2 = imread("c:\\2.jpg", 0);
      vector&lt;KeyPoint&gt; keypoints, keypoints2;
      features-&gt;detect(img, keypoints);
      features-&gt;detect(img2,keypoints2);
      descriptor-&gt;compute(img, keypoints, features);
      Mat features2;
      descripto-&gt;compute(img2, keypoints2, features2);
      bowTrainer.add(features);
      bowTrainer.add(features2);
  
      Mat dictionary = bowTrainer.cluster();
      bowDE.setVocabulary(dictionary);
  </code></pre>
  
  <p>This is all based on the <a href="http://docs.opencv.org/modules/features2d/doc/object_categorization.html#bowtrainer-getdescriptors">BOW documentation</a>.</p>
  
  <p>I think at this stage my system is trained. and the next step is predicting.</p>
  
  <p>this is where I dont know what to do. If I use <code>SVM</code> or <code>NormalBayesClassifier</code> they both use the terms train and predict. </p>
  
  <p>How do I predict and train after this? any guidance would be much appreciated. How do I connect the training of the classifier to my `bowDE`` function?</p>