title: OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in predict
body: <p>I'm trying to use OpenCV SVM classifier in cocos2d-x game. Here's a simple test function:</p>
  
  <pre><code>void HelloWorld::testOpenCV(){
      // Load SVM classifier
      auto classifierPath = FileUtils::getInstance()-&gt;fullPathForFilename("classifier.yml");
      cv::Ptr&lt;cv::ml::SVM&gt; svm = cv::ml::StatModel::load&lt;cv::ml::SVM&gt;(classifierPath);
  
      string filename = "test.jpg";
  
      auto img = new Image();
      img-&gt;initWithImageFile(filename);
  
      int imageSize = (int)img-&gt;getDataLen();
      int imageXW = img-&gt;getWidth();
      int imageYW = img-&gt;getHeight();
      unsigned char * srcData = img-&gt;getData();
  
      CCLOG("imageXW=%d, imageYW=%d", imageXW, imageYW);
      int ch = imageSize/(imageXW*imageYW);
      CCLOG("image=%dch raw data...", ch);
  
      cv::Mat testMat = createCvMatFromRaw(srcData, imageXW, imageYW, ch);
      testMat.convertTo(testMat, CV_32F);
  
      // try to predict which number has been drawn
      try{
          int predicted = svm-&gt;predict(testMat);
  
          CCLOG("Recognizing following number -&gt; %d", predicted);
  
      }catch(cv::Exception ex){
  
      }
  }
  </code></pre>
  
  <p>And it gives an output:</p>
  
  <pre><code>imageXW=28, imageYW=28
  image=3ch raw data...
  OpenCV Error: Assertion failed (samples.cols == var_count &amp;&amp; samples.type() == CV_32F) in predict, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/ml/src/svm.cpp, line 1930
  </code></pre>
  
  <p>It is based on this tutorial:</p>
  
  <p><a href="https://www.simplicity.be/article/recognizing-handwritten-digits/" rel="nofollow noreferrer">https://www.simplicity.be/article/recognizing-handwritten-digits/</a></p>
  
  <p>Especially on this method:</p>
  
  <pre><code>// Standard library
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  
  // OpenCV
  #include &lt;opencv2/core.hpp&gt;
  #include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/highgui.hpp&gt;
  #include &lt;opencv2/ml.hpp&gt;
  
  // POSIX
  #include &lt;unistd.h&gt;
  
  /**
   * main
   **/ 
  int main( int argc, char** argv )
  {
  
      //
      // Load SVM classifier 
      cv::Ptr&lt;cv::ml::SVM&gt; svm = cv::ml::StatModel::load&lt;cv::ml::SVM&gt;("classifier.yml");
  
  
      // read image file (grayscale)
      cv::Mat imgMat = cv::imread("test.jpg", 0);
  
      // convert 2d to 1d 
      cv::Mat testMat = imgMat.clone().reshape(1,1);
      testMat.convertTo(testMat, CV_32F);
  
      // try to predict which number has been drawn
      try{
          int predicted = svm-&gt;predict(testMat);
  
          std::cout &lt;&lt; std::endl  &lt;&lt; "Recognizing following number -&gt; " &lt;&lt; predicted &lt;&lt; std::endl &lt;&lt; std::endl;
  
          std::string notifyCmd = "notify-send -t 1000 Recognized: " + std::to_string(predicted);
          system(notifyCmd.c_str());
  
      }catch(cv::Exception ex){
  
      }
  
  }
  </code></pre>
  
  <p>I've ran it in terminal and it worked. </p>
  
  <p>Here's an implementation of createCvMatFromRaw:</p>
  
  <pre><code>cv::Mat HelloWorld::createCvMatFromRaw(unsigned char *rawData, int rawXW, int rawYW, int ch)
  {
      cv::Mat cvMat( rawYW, rawXW, CV_8UC4); // 8 bits per component, 4 channels
  
      for (int py=0; py&lt;rawYW; py++) {
          for (int px=0; px&lt;rawXW; px++) {
              int nBasePos = ((rawXW * py)+px) * ch;
              cvMat.at&lt;cv::Vec4b&gt;(py, px) = cv::Vec4b(rawData[nBasePos + 0],
                                                      rawData[nBasePos + 1],
                                                      rawData[nBasePos + 2],
                                                      0xFF);
  
          }
      }
  
      return cvMat;
  }
  </code></pre>
  
  <p>I've found it here:</p>
  
  <p><a href="http://blog.szmake.net/archives/845" rel="nofollow noreferrer">http://blog.szmake.net/archives/845</a></p>
  
  <p>What does this assert mean? Can someone explain it to me? How can I fix this?</p>