Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What etarion said is correct. </p> <p>To copy a column or row you always have to write:</p> <pre><code>Mat B = mat.col(i); A.copyTo(B); </code></pre> <p>The following program shows how to perform a PCA in OpenCV. It'll show the mean image and the first three Eigenfaces. The images I used in there are available from <a href="http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html">http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html</a>:</p> <pre><code>#include "cv.h" #include "highgui.h" using namespace std; using namespace cv; Mat normalize(const Mat&amp; src) { Mat srcnorm; normalize(src, srcnorm, 0, 255, NORM_MINMAX, CV_8UC1); return srcnorm; } int main(int argc, char *argv[]) { vector&lt;Mat&gt; db; // load greyscale images (these are from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html) db.push_back(imread("s1/1.pgm",0)); db.push_back(imread("s1/2.pgm",0)); db.push_back(imread("s1/3.pgm",0)); db.push_back(imread("s2/1.pgm",0)); db.push_back(imread("s2/2.pgm",0)); db.push_back(imread("s2/3.pgm",0)); db.push_back(imread("s3/1.pgm",0)); db.push_back(imread("s3/2.pgm",0)); db.push_back(imread("s3/3.pgm",0)); db.push_back(imread("s4/1.pgm",0)); db.push_back(imread("s4/2.pgm",0)); db.push_back(imread("s4/3.pgm",0)); int total = db[0].rows * db[0].cols; // build matrix (column) Mat mat(total, db.size(), CV_32FC1); for(int i = 0; i &lt; db.size(); i++) { Mat X = mat.col(i); db[i].reshape(1, total).col(0).convertTo(X, CV_32FC1, 1/255.); } // Change to the number of principal components you want: int numPrincipalComponents = 12; // Do the PCA: PCA pca(mat, Mat(), CV_PCA_DATA_AS_COL, numPrincipalComponents); // Create the Windows: namedWindow("avg", 1); namedWindow("pc1", 1); namedWindow("pc2", 1); namedWindow("pc3", 1); // Mean face: imshow("avg", pca.mean.reshape(1, db[0].rows)); // First three eigenfaces: imshow("pc1", normalize(pca.eigenvectors.row(0)).reshape(1, db[0].rows)); imshow("pc2", normalize(pca.eigenvectors.row(1)).reshape(1, db[0].rows)); imshow("pc3", normalize(pca.eigenvectors.row(2)).reshape(1, db[0].rows)); // Show the windows: waitKey(0); } </code></pre> <p>and if you want to build the matrix by row (like in your original question above) use this instead:</p> <pre><code>// build matrix Mat mat(db.size(), total, CV_32FC1); for(int i = 0; i &lt; db.size(); i++) { Mat X = mat.row(i); db[i].reshape(1, 1).row(0).convertTo(X, CV_32FC1, 1/255.); } </code></pre> <p>and set the flag in the PCA to:</p> <pre><code>CV_PCA_DATA_AS_ROW </code></pre> <p>Regarding machine learning. I wrote a document on machine learning with the OpenCV C++ API that has examples for most of the classifiers, including Support Vector Machines. Maybe you can get some inspiration there: <a href="http://www.bytefish.de/pdf/machinelearning.pdf">http://www.bytefish.de/pdf/machinelearning.pdf</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload