PCACompute Opencv返回特征向量= 0

时间:2012-02-02 10:53:35

标签: android opencv pca

在Android Opencv2.3.1中有PCACompute这个问题,因为当我打电话给PCACompute时,我的特征向量都是0.所以,我为每个人拍了10张照片,然后把它保存到100X100的Mat中。 在那之后,我用一个Mat 1X10000将我的100X100 Mat转换成这个代码:

double [] elem = null; 
 for(int riga=0;riga<m.rows();riga++)
  {
   for(int colonna=0;colonna<m.cols();colonna++)
   {
    elem = m.get(riga, colonna);
      mrow.put(0,((riga*100)+colonna), elem[0]);
    }//for colonna
 }//for riga

之后,当我拍摄10张照片时,我将所有照片的垫子插入到一张垫子中,并带有以下代码:

double b[] = null;
  for (int i = 0; i< listafoto.size(); i++)
   {
    Mat t = listafoto.get(i);
      for(int riga = 0;riga<t.rows();riga++)
       {
        for(int colonna =0;colonna<t.cols();colonna++)
        {
           b = t.get(riga, colonna);
           datiOriginali.put(i, colonna, b[0]);
        }//for colonna
    }//for riga
 }//for lista e contemporaneamente riga datiOriginali

之后,我用这段代码调用PCACompute:`

 org.opencv.core.Core.PCACompute(datiOriginali,mean, eigenvectors, 10);`

因此,datiOriginali是10行和10000列的输入Mat,均值和特征向量是输出矩阵。平均矩阵给我一个结果,但是特征向量给了我全部0.你能帮我解决这个问题吗? 在此先感谢.MArco

1 个答案:

答案 0 :(得分:2)

我的代码基于http://www.bytefish.de/blog/pca_in_opencv的示例。 我是这样做的:

    Vector trainingImages = new Vector();;
    trainingImages.add(Highgui.imread("/sdcard/facedatabase/s1/1.pgm",0));
    trainingImages.add(Highgui.imread("/sdcard/facedatabase/s1/2.pgm",0));

    Mat x = (Mat) trainingImages.get(0);
    int total = x.rows() * x.cols();

    // build matrix (column)
    // This matrix will have one col for each image and imagerows x imagecols rows
        Mat mat = new Mat(total, trainingImages.size(), CvType.CV_32FC1);
        for(int i = 0; i < trainingImages.size(); i++) {
            Mat X = mat.col(i);
            Mat c = (Mat) trainingImages.get(i);
            c.reshape(1,total).convertTo(X, CvType.CV_32FC1);
        }

    Mat eigenVectors = new Mat();
    Mat mean = new Mat();
    Core.PCACompute(mat, mean, eigenVectors);