Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.


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.

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.

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.