title: Training images using SVM on OpenCV
body: <p>I am trying to do classification with images (next step I'll classify based on features but now just want to try whether I am doing it right or not)</p>
  
  <p>here is my code.</p>
  
  <pre><code>#include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/ml/ml.hpp&gt;
  
  using namespace cv;
  using namespace std;
  
  
  
  int main(){
  
  
      Mat image[2];
      image[0]= imread("image.jpg",0);
  
      image[1]= imread("wrongimage.jpg",0);
  
      Mat rotated = imread("image.jpg",0);
  
  
      image[0] = image[0].reshape(0, 1); //SINGLE LINE
      image[1] = image[1].reshape(0, 1); //SINGLE LINE
  
  
      //  image[0].convertTo(image[0], CV_32FC1); //CONVERT TO 32FC1
      //  image[1].convertTo(image[1], CV_32FC1); //CONVERT TO 32FC1
  
  
      Mat new_image(2,1,CV_32FC1,image); //CONVERT TO 32FC1
  
  
  
      float labels[2] = {1.0, -1.0};
      Mat labelsmat(2,1,CV_32FC1,labels); //correct labels 1
  
      labelsmat.convertTo(labelsmat, CV_32FC1);
  
  
  
      CvSVMParams params;
      params.svm_type = CvSVM::C_SVC;
      params.kernel_type = CvSVM::LINEAR;
      params.gamma = 3;
      params.degree = 3;
      CvSVM svm;
      svm.train(new_image, labelsmat, Mat(),Mat(),params);
  
      //    svm.train(training_mat2, labelsmat, Mat(),Mat(),params);
  
      // svm.train(training_mat2, labelsmat, Mat(), Mat(), params);
      svm.save("svm.xml"); // saving
  
  
      svm.load("svm.xml"); // loading
  
  
      rotated = rotated.reshape(0,1);
      rotated.convertTo(rotated, CV_32FC1);
  
  
       svm.predict(rotated);
  }
  </code></pre>
  
  <p>since training images with opencv svm is lack of documented I tried to manage something by reading <a href="https://stackoverflow.com/questions/14694810/using-opencv-and-svm-with-images">using OpenCV and SVM with images</a> and <a href="http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html" rel="nofollow noreferrer">http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html</a> </p>
  
  <p>somehow I manage to train my images but I stron I this train xml file is not correct because I didn't point out which image is correct (1) or false (-1) </p>
  
  <p>and also when I try to predict with the image I've trained to svm is gives me error </p>
  
  <blockquote>
    <p>OpenCV Error: Sizes of input arguments do not match (The sample size
    is different from what has been used for training) in
    cvPreparePredictData, file
    /tmp/opencv-DXLLi8/opencv-2.4.9/modules/ml/src/inner_functions.cpp,
    line 1114 libc++abi.dylib: terminating with uncaught exception of type
    cv::Exception:
    /tmp/opencv-DXLLi8/opencv-2.4.9/modules/ml/src/inner_functions.cpp:1114:
    error: (-209) The sample size is different from what has been used for
    training in function cvPreparePredictData</p>
  </blockquote>
  
  <p>also here xml generated by SVM.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;opencv_storage&gt;
  &lt;my_svm type_id="opencv-ml-svm"&gt;
    &lt;svm_type&gt;C_SVC&lt;/svm_type&gt;
    &lt;kernel&gt;&lt;type&gt;LINEAR&lt;/type&gt;&lt;/kernel&gt;
    &lt;C&gt;1.&lt;/C&gt;
    &lt;term_criteria&gt;&lt;epsilon&gt;1.1920928955078125e-07&lt;/epsilon&gt;
      &lt;iterations&gt;1000&lt;/iterations&gt;&lt;/term_criteria&gt;
    &lt;var_all&gt;1&lt;/var_all&gt;
    &lt;var_count&gt;1&lt;/var_count&gt;
    &lt;class_count&gt;2&lt;/class_count&gt;
    &lt;class_labels type_id="opencv-matrix"&gt;
      &lt;rows&gt;1&lt;/rows&gt;
      &lt;cols&gt;2&lt;/cols&gt;
      &lt;dt&gt;i&lt;/dt&gt;
      &lt;data&gt;
        -1 1&lt;/data&gt;&lt;/class_labels&gt;
    &lt;sv_total&gt;1&lt;/sv_total&gt;
    &lt;support_vectors&gt;
      &lt;_&gt;
        -1.56709105e-02&lt;/_&gt;&lt;/support_vectors&gt;
    &lt;decision_functions&gt;
      &lt;_&gt;
        &lt;sv_count&gt;1&lt;/sv_count&gt;
        &lt;rho&gt;-1.&lt;/rho&gt;
        &lt;alpha&gt;
          1.&lt;/alpha&gt;
        &lt;index&gt;
          0&lt;/index&gt;&lt;/_&gt;&lt;/decision_functions&gt;&lt;/my_svm&gt;
  &lt;/opencv_storage&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've changed my code with the suggestions by <strong>guneykayim</strong> but now I'm getting 
  EXC_BAD_ACCESS (code=1 address=...) error. My updated code is below. </p>
  
  <pre><code>#include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/ml/ml.hpp&gt;
  
  using namespace cv;
  using namespace std;
  
  
  
  int main(){
  
  
      Mat image[2];
      image[0]= imread("image.jpg",0);
  
      image[1]= imread("wrongimage.jpg",0);
  
      Mat rotated = imread("image.jpg",0);
  
  
     image[0] = image[0].reshape(0, 1); //SINGLE LINE
     image[1] = image[1].reshape(0, 1); //SINGLE LINE
  
   //   int size = sizeof(image)/sizeof(Mat);
      //  image[0].convertTo(image[0], CV_32FC1); //CONVERT TO 32FC1
      //  image[1].convertTo(image[1], CV_32FC1); //CONVERT TO 32FC1
  
      Mat new_image(2,341318,CV_32FC1,image); //CONVERT TO 32FC1
  
      float labels[2] = {1.0, -1.0};
      Mat labelsmat(2,1,CV_32FC1,labels); //correct labels 1
  
      labelsmat.convertTo(labelsmat, CV_32FC1);
  
  
      cout&lt;&lt;image[0].size()&lt;&lt;endl;
      cout&lt;&lt;new_image.size()&lt;&lt;endl;
  
  
  
      CvSVMParams params;
      params.svm_type = CvSVM::C_SVC;
      params.kernel_type = CvSVM::LINEAR;
      params.gamma = 3;
      params.degree = 3;
      CvSVM svm;
      svm.train_auto(new_image, labelsmat,Mat(),Mat(),params);
  
      //  svm.train_(new_image, labelsmat, Mat(),Mat(),params);
      //    svm.train(training_mat2, labelsmat, Mat(),Mat(),params);
  
      // svm.train(training_mat2, labelsmat, Mat(), Mat(), params);
      svm.save("svm.xml"); // saving
  
  
      svm.load("svm.xml"); // loading
  
  
      rotated = rotated.reshape(0,1);
      rotated.convertTo(rotated, CV_32FC1);
  
      cout&lt;&lt;svm.predict(rotated)&lt;&lt;endl;
  
  }
  </code></pre>
  
  <p>my image size is :    [170569 x 1]
  and new_image size is [341318 x 2]</p>