Ask Your Question
0

How can I calculate PCA?

asked 2013-03-23 22:37:43 -0600

Nihad gravatar image

updated 2013-03-23 23:02:35 -0600

I am trying to use PCA, but it gives memory access violation error. Here is my sample code.

int main(...)
{
.................
vector<float>input_array;

    for(i=0;i<number_of_lines;i++)
    {
        for(j=0;j<feature_vector_size;j++)
        {
            input_array.push_back(read_feature[i][j]);  
        }

        Mat input_feature_vector(input_array);

        Mat projection_result;


        PCA pca(input_feature_vector,Mat(),CV_PCA_DATA_AS_ROW, 0);/////error memory access

        pca.project(input_feature_vector,projection_result);

        for(k=0;k<feature_vector_size;k++)
        {

           fprintf(pca_output_file,"%lf ",projection_result.at <float>(k,0));


        }

                 fprintf(pca_output_file,"\n");
        input_array.clear ();

    }
.............

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-03-24 03:46:42 -0600

berak gravatar image
Mat input_feature_vector(input_array);

will make a 1-col, n-row Mat.

either change the flag to CV_PCA_DATA_AS_COL ,

or do :

Mat input_as_row = input_feature_vector.reshape(1,1); // no fear, it's cheap
PCA pca(input_as_row,Mat(),CV_PCA_DATA_AS_ROW, 0);
edit flag offensive delete link more

Comments

Thnx for your try. Sorry to say that still same problem arise.

Nihad gravatar imageNihad ( 2013-03-24 06:03:20 -0600 )edit

sounds stupid, but please check, if any of the feature vectors is empty()

berak gravatar imageberak ( 2013-03-24 08:59:30 -0600 )edit

I have checked. I have also surprised to see that.

Nihad gravatar imageNihad ( 2013-03-24 20:04:21 -0600 )edit

Question Tools

Stats

Asked: 2013-03-23 22:37:43 -0600

Seen: 1,053 times

Last updated: Mar 24 '13