Ask Your Question
0

Calculate Eigen Vector and Eigen Value for PCA

asked 2017-01-20 21:45:22 -0500

Shabrina gravatar image

I want to get eigenvalue and eigenvector. But it breaks. this is my part of code.

Mat covmat=((matdev*matdev.t())/3); //data float matrix 5*5, this's working. but when I add with PCA function. It's break.
PCA pt_pca(covmat, Mat(), CV_PCA_DATA_AS_ROW, 0);
            Mat pt_eig_vals = pt_pca.eigenvalues;
            for (int i = 0; i < 4; ++i)
            cout << pt_eig_vals.at<float>(i, 0) <<endl;

This is the error. OpenCV error: Assertion Failed <data.channerls&lt;&gt; =="1"> in unknown function, file ......\src\opencv\modules\core\src\matmul.cpp

Please somebody help me :(. Thanks

edit retag flag offensive close merge delete

Comments

what is matdev ? (please show, how you construct that)

can you try again to retrieve the correct error msg ? data.channels()==1 would make sense, data.channels<>==1 is a syntax error.

berak gravatar imageberak ( 2017-01-21 01:02:51 -0500 )edit
1

matdev is matrix data from txtfile like this.

%YAML:1.0
Point: !!opencv-matrix
   rows: 5
   cols: 5
   dt: "2f"
   data: [ 0., 0., 1., 0., -1., 0., 0., 0., 0., 0., 0., 0., -1., 0., -1.,
       0., 0., 0., -1., 0., 0., 0., 0., 0., 1., 0., -1., 0., 0., 0., 1.,
       0., 1., 0., -1., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0. ]
belan gravatar imagebelan ( 2017-01-21 01:14:45 -0500 )edit

maybe it helps, if you could also explain, how you got this, and what is inside your data ?

berak gravatar imageberak ( 2017-01-21 01:41:45 -0500 )edit

The data is vector x y -> 25 data. and I change/ reshape it to nxn = 5x5 (this is int). after that I convert to 32F. and that the result. any i idea for change it to 1 channel matrix with row n column same . thank you

belan gravatar imagebelan ( 2017-01-21 08:02:25 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-01-21 01:24:31 -0500

berak gravatar image

updated 2017-01-21 01:34:53 -0500

unfortunately, opencv's PCA (as well as eigen()) can only handle Mat's with a single channel as input, your matdev has 2.

(then, also note, that the covariance Matrix is already calculated internally, so you would not have to do that manually before using PCA)

edit flag offensive delete link more

Comments

So, how to make it, to be single channel? I'm a beginner in opencv.

belan gravatar imagebelan ( 2017-01-21 01:30:11 -0500 )edit

you could either split it into 2 planes, and handle those seperately:

Mat planes[2];
split(matdev, planes);
PCA pca1(planes[0],...);
PCA pca2(planes[1],...);

or try to reshape it to a 1channel, [10x5] mat:

Mat matdev1 = matdev.reshape(1,5);

but i have no idea, if any of this makes sense with your data. maybe you have to go back, andfind out, why matdev looks like it is, and if this is really the desired format.

berak gravatar imageberak ( 2017-01-21 01:40:22 -0500 )edit

Ok, thank you, I will try it.

belan gravatar imagebelan ( 2017-01-21 06:14:26 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-01-20 21:45:22 -0500

Seen: 825 times

Last updated: Jan 21 '17