title: openCV error:"Sizes of input arguments do not match" while trying to detect objects using HoG features and SVM
body: <p>I am working on object detection using HoG Features and SVM. I am calculating HoG features for 64X128 images using the code in <a href="http://smsoftdev-solutions.blogspot.in/2009/08/integral-histogram-for-fast-calculation.html" rel="nofollow">this (Integral Histogram for fast Calculation of HOG Features)  </a>  and <a href="http://smsoftdev-solutions.blogspot.in/2009/10/object-detection-using-opencv-ii.html" rel="nofollow">this (Calculation of Hog Features)</a> for both positive(pos.xml) and Negative(neg.xml) images.  I am training the SVM with the pos.xml and neg.xml . When I try to detect using the xml file saved using SVM training , it gives me following error. (image size used for detection is 64X128)</p>
  
  <blockquote>
    <p>OpenCV Error: Sizes of input arguments do not match (The sample size
    is differen t from what has been used for training) in
    cvPreparePredictData, file .......
    .\opencv\modules\ml\src\inner_functions.cpp, line 1114</p>
  </blockquote>
  
  <p>My code for detecting images.</p>
  
  <pre><code>    Mat img= imread("path\\a91.jpg"); // 64X128 pixels
      CvSVM svm;
      svm.load("svm1.xml");
      Mat example;
      cvtColor(img,example,CV_RGB2GRAY);
      example = example.reshape(0,1);
      //cout  &lt;&lt;type2str(example.type()) &lt;&lt;endl;
  
  
  
      example.convertTo(example, CV_32FC1);
  
      //cout  &lt;&lt;type2str(example.type()) &lt;&lt;endl;
      cout&lt;&lt;svm.predict(example)&lt;&lt;endl;
  </code></pre>
  
  <p>SVM training code : </p>
  
  <pre><code>void trainSVM(CvMat* pos_mat, CvMat* neg_mat, char *savexml, char *pos_file = NULL, char *neg_file = NULL) 
  {
  
  
      /* Read the feature vectors for positive samples */
      if (pos_file != NULL) 
          {
          printf("positive loading...\n");
          pos_mat = (CvMat*) cvLoad(pos_file);
          printf("positive loaded\n");
      }
  
      /* Read the feature vectors for negative samples */
      if (neg_file != NULL)
      {
          neg_mat = (CvMat*) cvLoad(neg_file);
          printf("negative loaded\n");
      }
  
      int n_positive, n_negative;
      n_positive = pos_mat-&gt;rows;
      n_negative = neg_mat-&gt;rows; 
      int feature_vector_length = pos_mat-&gt;cols;
      int total_samples;
      total_samples = n_positive + n_negative;
  
      CvMat* trainData = cvCreateMat(total_samples,feature_vector_length, CV_32FC1);
  
      CvMat* trainClasses = cvCreateMat(total_samples,1, CV_32FC1 );
  
      CvMat trainData1, trainData2, trainClasses1,
      trainClasses2;
  
      printf("Number of positive Samples : %d\n",pos_mat-&gt;rows);
  
      /*Copy the positive feature vectors to training
      data*/
  
      cvGetRows(trainData, &amp;trainData1, 0, n_positive);
      cvCopy(pos_mat, &amp;trainData1);
      cvReleaseMat(&amp;pos_mat);
  
      /*Copy the negative feature vectors to training
      data*/
  
      cvGetRows(trainData, &amp;trainData2, n_positive,total_samples);
  
      cvCopy(neg_mat, &amp;trainData2);
      cvReleaseMat(&amp;neg_mat);
  
      printf("Number of negative Samples : %d\n",trainData2.rows);
  
      /*Form the training classes for positive and
      negative samples. Positive samples belong to class
      1 and negative samples belong to class 2 */
  
      cvGetRows(trainClasses, &amp;trainClasses1, 0, n_positive);
      cvSet(&amp;trainClasses1, cvScalar(1));
  
      cvGetRows(trainClasses, &amp;trainClasses2, n_positive, total_samples);
  
      cvSet(&amp;trainClasses2, cvScalar(2));
  
  
      /* Train a linear support vector machine to learn from
      the training data. The parameters may played and
      experimented with to see their effects*/
  
      CvSVM svm(trainData, trainClasses, 0, 0, CvSVMParams(CvSVM::C_SVC, CvSVM::LINEAR, 0, 0, 0, 2, 0, 0, 0, cvTermCriteria(CV_TERMCRIT_EPS,0, 0.01)));
  
      printf("SVM Training Complete!!\n");
  
      /*Save the learnt model*/
  
      if (savexml != NULL) {
          svm.save(savexml);
      }
      cvReleaseMat(&amp;trainClasses);
      cvReleaseMat(&amp;trainData);
  
  }
  </code></pre>
  
  <p>Please help me with this. I am stuck here. Thank you.</p>