Ask Your Question
0

PCA calculation error.

asked 2013-05-22 03:24:35 -0600

Nihad gravatar image

I tried to calculate PCA, but I failed. Whats wrong with this approach? Here is error msg and code segment.

OpenCV Error:

 Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si
ze.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channel
s()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3
) - 1))*4) & 15) == elemSize1()) in unknown function, file c:\program files (x86
)\opencv2.2\include\opencv2\core\mat.hpp, line 517
exception caught: c:\program files (x86)\opencv2.2\include\opencv2\core\mat.hpp:
517: error: (-215) dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (u
nsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((s
izeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) &
15) == elemSize1()

Code segement:

int main(..)
{
.......Read read_feature two dimensional array////////////////
number_of_lines=907;
feature_vector_size=300;
Mat input_feature_vector(number_of_lines,feature_vector_size,CV_32F);

    for(i=0;i<number_of_lines;i++)
    {
        for(j=0;j<feature_vector_size;j++)
        {
            input_feature_vector.at<float>(i,j)=read_feature[i][j];

        }

    }

    cout<<input_feature_vector<<endl;

    Mat projection_result;

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

    /*cout<<"PCA Mean:"<<endl;
    cout<<pca.mean<<endl;*/

    pca.project(input_feature_vector,projection_result);

    //cout<<"PCA Projection Result:"<<endl;
    //cout<<projection_result<<endl;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-05-22 13:41:19 -0600

Guanta gravatar image

updated 2013-05-22 13:48:06 -0600

I just tested your code with 2.4.0 (not my work pc), filled read_feature with random values, and it worked like a charm, so maybe an update to a current OpenCV-version will fix your problem. The code I used for testing:

int number_of_lines=907;
int feature_vector_size=300;
cv::Mat input_feature_vector(number_of_lines, feature_vector_size, CV_32F);

srand (time(NULL));
for( int y = 0; y < number_of_lines; y++ ) {
    for( int x = 0; x < feature_vector_size; x++ ) {
        input_feature_vector.at<float>(y,x) = (float)(rand() % 100);
    }
}
cv::PCA pca(input_feature_vector,cv::Mat(),CV_PCA_DATA_AS_ROW, 0);
cv::Mat projection_result;
pca.project(input_feature_vector,projection_result);
std::cout << "PCA Projection Result:\n" << projection_result << std::endl;

Side note: copying is expensive, maybe you can create a matrix header on top of your data directly, i.e. instead:

Mat input_feature_vector(number_of_lines,feature_vector_size,CV_32F);

for(i=0;i<number_of_lines;i++)
{
    for(j=0;j<feature_vector_size;j++)
    {
        input_feature_vector.at<float>(i,j)=read_feature[i][j];

    }
}

you can write:

cv::Mat input_feature_vector(number_of_lines, feature_vector_size, CV_32F, read_feature);

Be aware, your read_feature-matrix has to lie continuous in memory to do that (so your method is probably safer). Alternatively use a cv::Mat directly when reading your features to avoid the copying.

edit flag offensive delete link more

Comments

1

did the same, could not reproduce the error (2.4.9)

berak gravatar imageberak ( 2013-05-22 13:47:31 -0600 )edit

thnx. ur code is now working well. just change read_feature type double to float.

Nihad gravatar imageNihad ( 2013-05-23 00:01:13 -0600 )edit

^^ I haven't used double anywhere

Guanta gravatar imageGuanta ( 2013-05-23 03:30:58 -0600 )edit

actually, i used double when i tried to write it in file. That code segment i did not include here. Everything is ok now.

Nihad gravatar imageNihad ( 2013-05-23 03:41:38 -0600 )edit

Sweet, glad I could help!

Guanta gravatar imageGuanta ( 2013-05-23 04:02:14 -0600 )edit

Question Tools

Stats

Asked: 2013-05-22 03:24:35 -0600

Seen: 1,506 times

Last updated: May 22 '13