title: C++ OpenCV SVM Predict Not Working
body: <p>Trying to create a functional <code>SVM</code>. I have 114 training images, 60 Positive/54 Negative, and 386 testing images for the <code>SVM</code> to predict against.</p>
  
  <p>I read in the training image features to <code>float</code> like this:</p>
  
  <pre><code>trainingDataFloat[i][0] = trainFeatures.rows;
  trainingDataFloat[i][1] = trainFeatures.cols;
  </code></pre>
  
  <p>And the same for the testing images too:</p>
  
  <pre><code>testDataFloat[i][0] = testFeatures.rows;
  testDataFloat[i][2] = testFeatures.cols;
  </code></pre>
  
  <p>Then, using Micka's <a href="https://stackoverflow.com/questions/27981571/c-opencv-turn-a-mat-into-a-1-dimensional-array"><strong>answer to this question</strong></a>, I turn the <code>testDataFloat</code> into a 1 Dimensional Array, and feed it to a <code>Mat</code> like this so to predict on the <code>SVM</code>:</p>
  
  <pre><code>float* testData1D = (float*)testDataFloat;
  Mat testDataMat1D(height*width, 1, CV_32FC1, testData1D);
  float testPredict = SVMmodel.predict(testDataMat1D);
  </code></pre>
  
  <p>Once this was all in place, there is the Debug Error of:</p>
  
  <p><strong>Sizes of input arguments do not match (the sample size is different from what has been used for training) in cvPreparePredictData</strong></p>
  
  <p>Looking at <a href="https://stackoverflow.com/questions/25555556/java-opencv-training-svm-error-input-sample-must-be-1-dimensional-vector"><strong>this post</strong></a> I found (Thanks to berak) that:</p>
  
  <p><em>"all images (used in training &amp; prediction) have to be the same size"</em></p>
  
  <p>So I included a re-size function that would re-size the images to be all square at whatever size you wished (<code>100x100, 200x200, 1000, 1000 etc.</code>)</p>
  
  <p>Run it again with the images re-sized to a new directory that the program now loads the images in from, and I get the exact same error as before of:</p>
  
  <p><strong>Sizes of input arguments do not match (the sample size is different from what has been used for training) in cvPreparePredictData</strong></p>
  
  <p>I just have no idea anymore on what to do. Why is it still throwing that error?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I changed</p>
  
  <pre><code>Mat testDataMat1D(TestDFheight*TestDFwidth, 1, CV_32FC1, testData1D);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>Mat testDataMat1D(1, TestDFheight*TestDFwidth, CV_32FC1, testData1D);
  </code></pre>
  
  <p>and placed the <code>.predict</code> inside the loop that the <code>features</code> are given to the <code>float</code> so that each image is given to the <code>.predict</code> individually because of <a href="https://stackoverflow.com/questions/14694810/using-opencv-and-svm-with-images"><strong>this question</strong></a>. With the to <code>int</code> swapped so that <code>.cols</code> = 1 and <code>.rows</code> = <code>TestDFheight*TestDFwidth</code> the program seems to actually run, but then stops on image 160 (<code>.exe has stopped working</code>)... So that's a new concern.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Added a simple</p>
  
  <pre><code>std::cout &lt;&lt; testPredict;
  </code></pre>
  
  <p>To view the determined output of the SVM, and it seems to be positively matching everything until Image 160, where it stops running:</p>
  
  <p><img src="https://i.stack.imgur.com/gdU9V.png" alt=""></p>