title: opencv C++ neural network predict() function throws "Bad argument" error
body: <p>I have managed to train a neural network to recognize numbers in an image and have saved the network parameters to an .xml file.</p>
  
  <p>However, when testing the network against a new image the code fails at the predict() stage with the error:</p>
  
  <blockquote>
    <p>OpenCV Error: Bad argument (Both input and output must be floating-point matrices of the same type and have the same number of rows) in CvANN_MLP::predict, file ........\opencv\modules\ml\src\ann_mlp.cpp, line 279.</p>
  </blockquote>
  
  <p>ann_mlp.cpp line 279 is:</p>
  
  <pre><code>if( !CV_IS_MAT(_inputs) || !CV_IS_MAT(_outputs) ||
      !CV_ARE_TYPES_EQ(_inputs,_outputs) ||
      (CV_MAT_TYPE(_inputs-&gt;type) != CV_32FC1 &amp;&amp;
      CV_MAT_TYPE(_inputs-&gt;type) != CV_64FC1) ||
      _inputs-&gt;rows != _outputs-&gt;rows )
      CV_Error( CV_StsBadArg, "Both input and output must be floating-point matrices "
                              "of the same type and have the same number of rows" );
  </code></pre>
  
  <p>I have checked input rows by running this code:</p>
  
  <pre><code>cv::Size s = newVec.size();
      int rows = s.height;
      int cols = s.width;
      cout &lt;&lt; "newVec dimensions: " &lt;&lt; rows &lt;&lt; " x " &lt;&lt; cols &lt;&lt; endl;
  </code></pre>
  
  <p>...and it comes out with the expected 1 x 900 vector / matrix.</p>
  
  <p>I have set the input and output matrices to be CV_32FC1 as per the error dialog like this:</p>
  
  <p>Input matrix</p>
  
  <pre><code>cv::Mat newVec(1, 900, CV_32FC1);
      newVec = crop_img.reshape(0, 1); //reshape / unroll image to vector
      CvMat n = newVec;
      newVec = cv::Mat(&amp;n);
  </code></pre>
  
  <p>Output matrix</p>
  
  <pre><code>    cv::Mat classOut = cvCreateMatHeader(1, CLASSES, CV_32FC1);
  </code></pre>
  
  <p>And I try to run the prediction function like this:</p>
  
  <pre><code>CvANN_MLP* nnetwork = new CvANN_MLP;
  nnetwork-&gt;load("nnetwork.xml", "nnetwork");
  
  int maxIndex = 0;
  cv::Mat classOut = cvCreateMatHeader(1, CLASSES, CV_32FC1);
  
  //prediction
  nnetwork-&gt;predict(newVec, classOut);
      float value;
      float maxValue = classOut.at&lt;float&gt;(0, 0);
      for (int index = 1; index&lt;CLASSES; index++)
      {
          value = classOut.at&lt;float&gt;(0, index);
          if (value&gt;maxValue)
          {
              maxValue = value;
              maxIndex = index;
          }
      }
  </code></pre>
  
  <p>Any ideas? Much appreciated...</p>