title: PCA + SVM using C++ Syntax in OpenCV 2.2
body: <p>I'm having problems getting PCA and Eigenfaces working using the latest C++ syntax with the Mat and PCA classes. The older C syntax took an array of IplImage* as a parameter to perform its processing and the current API only takes a Mat that is formatted by Column or Row. I took the Row approach using the reshape function to fit my image's matrix to fit in a single row. I eventually want to take this data and then use the SVM algorithm to perform detection, but when I do that all my data is just a stream of 0s. Can someone please help me out? What am I doing wrong? Thanks!</p>
  
  <p>I saw <a href="https://stackoverflow.com/questions/4661903/pca-in-opencv-using-the-new-c-interface">this question</a> and it's somewhat related, but I'm not sure what the solution is.</p>
  
  <p>This is basically what I have:</p>
  
  <pre><code>vector&lt;Mat&gt; images; //This variable will be loaded with a set of images to perform PCA on.
  Mat values(images.size(), 1, CV_32SC1); //Values are the corresponding values to each of my images.
  
  int nEigens = images.size() - 1; //Number of Eigen Vectors.
  
  //Load the images into a Matrix
  Mat desc_mat(images.size(), images[0].rows * images[0].cols, CV_32FC1);
  for (int i=0; i&lt;images.size(); i++) {
    desc_mat.row(i) = images[i].reshape(1, 1);
  }
  
  Mat average;
  PCA pca(desc_mat, average, CV_PCA_DATA_AS_ROW, nEigens);
  
  Mat data(desc_mat.rows, nEigens, CV_32FC1); //This Mat will contain all the Eigenfaces that will be used later with SVM for detection
  
  //Project the images onto the PCA subspace
  for(int i=0; i&lt;images.size(); i++) {
    Mat projectedMat(1, nEigens, CV_32FC1);
    pca.project(desc_mat.row(i), projectedMat);
  
    data.row(i) = projectedMat.row(0);
  }
  
  CvMat d1 = (CvMat)data;
  CvMat d2 = (CvMat)values;
  
  CvSVM svm;
  svm.train(&amp;d1, &amp;d2);
  svm.save("svmdata.xml");
  </code></pre>