0
votes

I am trying to perform PCA on a data set currently I have 8 sets of data and for each piece of data I have 618 pieces of information about it. Below is the code I have tried so far:

        double[,] RawData = new double[8, 618];

        for (int i = 0; i < 8; i++)//Copies Data to Emgu Matrix
        {
            for (int j = 0; j < 618; j++)
            {
                double val = Convert.ToDouble(DataList[i][j]);
                RawData.SetValue(val, i, j);
            }
        }

        Matrix<Double> DataMatrix = new Matrix<Double>(RawData);
        Matrix<Double> Mean = new Matrix<Double>(1, 618);
        Matrix<Double> EigenValues = new Matrix<Double>(1, 618);
        Matrix<Double> EigenVectors = new Matrix<Double>(618, 618);

        CvInvoke.cvCalcPCA(DataMatrix, Mean, EigenValues, EigenVectors, Emgu.CV.CvEnum.PCA_TYPE.CV_PCA_DATA_AS_COL);

        Matrix<Double> PC1 = new Matrix<Double>(1, 618);
        for (int i = 0; i < 618; i++)
             PC1[0, i] = EigenVectors[0, i];

        Matrix<Double> Results = new Matrix<Double>(8, 1);

        CvInvoke.cvProjectPCA(DataMatrix, Mean, PC1, Results);

        TestStatus.Items.Add("PCA Projection Results = ");
        for (int i = 0; i < 8; i++)
        {
            TestStatus.Items.Add(Convert.ToString(DataMatrix[i, 0]));
        }

Emgu.CV.Util.CvException: OpenCV: (evals0.cols == 1 || evals0.rows == 1) && ecount0 <= ecount && evects0.cols == evects.cols && evects0.rows == ecount0

at Emgu.CV.CvInvoke.CvErrorHandler(Int32 status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userData)

at Emgu.CV.CvInvoke.cvCalcPCA(IntPtr data, IntPtr avg, IntPtr eigenvalues, IntPtr eigenvectors, PCA_TYPE flags) at Project_3._0.MainWindow.Capture_3D_Face_Click(Object sender, RoutedEventArgs e) in f:\Project\Project 3.0\Project 3.0\MainWindow.xaml.cs:line 516

Which is being caused by the line: (which is in the code above)

        CvInvoke.cvCalcPCA(DataMatrix, Mean, EigenValues, EigenVectors, Emgu.CV.CvEnum.PCA_TYPE.CV_PCA_DATA_AS_COL);

How do I set up matrices correctly for the PCA to function?

2

2 Answers

1
votes

In the code you provided, shouldn't it be

Emgu.CV.CvEnum.PCA_TYPE.CV_PCA_DATA_AS_ROW

instead of

Emgu.CV.CvEnum.PCA_TYPE.CV_PCA_DATA_AS_COL

(note the last 3 chars of each sencence)

0
votes

You could look into source code of emguCV and see which of the conditions in the line mentioned in error is triggered. As I have only OpenCV, I cannot help more, because my version of PCA does not have such an assertion.

There might be also some magic like "You must use float not double" involved.