title: Support Vector Machine image Classifier in OpenCV
body: <p>I am developing a image classifier using SVM. Just for development and debugging reasons I am using 16 images at the moment 8 each of 2 different classes. The problem in my program is that when I determine which class the object belong to it gives me always one class only. For example my labels for 16 images are {-1 and 1}. I always get 1 even when I predict my training data with that of label -1. </p>
  
  <p>My algorithm:
  I am using bag of word model to create dictionary with 1000 clusters in each.
  For feature extraction I use SURF.</p>
  
  <pre><code>Ptr&lt;DescriptorMatcher&gt; matcher = DescriptorMatcher::create("FlannBased");
  Ptr&lt;DescriptorExtractor&gt; extractor = DescriptorExtractor::create("SURF");
  Ptr&lt;FeatureDetector&gt; detector = FeatureDetector::create("SURF");
  
  int cluster_size = 1000;
  
  BOWKMeansTrainer bowTrainer(cluster_size, TermCriteria(CV_TERMCRIT_ITER, 10, 0.001), 1, KMEANS_PP_CENTERS);
  BOWImgDescriptorExtractor bowDE(extractor, matcher);
  </code></pre>
  
  <p>Next I read the images from the training set... (I skip the reading code here..) but for each image that the program reads: </p>
  
  <pre><code>detector-&gt;detect(image, keypoints);
  extractor-&gt;compute(image, keypoints, descriptors);
  bowTrainer.add(descriptors);
  </code></pre>
  
  <p>is done to create the dictionary.</p>
  
  <p>The training data path and labels are read from the text file and as I stated above i use -1 and 1 as labels since i just have 2 classes.</p>
  
  <p>Now I will talk about my SVM part:</p>
  
  <pre><code>SVMParams param = SVMParams () ;
  param . svm_type = SVM :: C_SVC ;
  param . kernel_type = SVM :: LINEAR ;
  param . degree = 10; 
  param . gamma = 20; 
  param . coef0 = 0; 
  param . C = 7;
  param . nu = 0.5; 
  param . p = 0.5; 
  param . class_weights = NULL ; // for CV_SVM_C_SVC
  param . term_crit . type = CV_TERMCRIT_ITER  ;
  param . term_crit . max_iter = 100;
  param . term_crit . epsilon = 1e-6;
  </code></pre>
  
  <p>Note some params I have twicked just to see if I get any closer to response but it not help. The initial parameters were extracted from the libsvm library and I changed it to see it I get any results. But it didn't help. </p>
  
  <p>Finally I train and than predict.
      SVM svm;
      svm.train(train__,labels, Mat(), Mat(), param);</p>
  
  <p>Now again my problem is that I dont get the right results. To be extact if I test data from class of -1, the svm predict gives 1 and for 1 it gives 1 as well. Can someone explain to me what is the step I have missed that is stopping my program from getting the correct response. </p>
  
  <p>Thank you and sorry for long question.</p>