Ask Your Question

Revision history [back]

PCA returns NaN

Hi everybody,

I'm attempting to classify 10 classes of hand pictures using a specific feature space. Training data is converted to a Mat object containing 240 rows (number of training pictures) and 480 cols (number of features) normalized between 0 and 1. The type of training data mat is CV_64FC1. I'm going to pass this matrix to PCA in order to extract useful features. After that I want to pass PCA result to SVM in order to train a classifier.

The PCA object returns all -NaN and Inf after creating the object:

PCA pca(training_data_mat, Mat(), CV_PCA_DATA_AS_ROW);

training_data_mat is the matrix of the training data with 240 rows and 450 cols for 240 training images and 450 features. It is created in the following line:

training_data_mat.push_back(extract_features_mat(descriptor,hog_ders)) ;

the extract_features_mat function is written such that returns a 1 row Mat of type CV_64FC1 containing features of the image. The following line is return value of the extract_features_mat function:

return Mat (1,feature_size, CV_64FC1, feature_value) ;

and feature_value is a double array defined and filled in this function.

I'm totally confused what is wrong!

PCA returns NaN

Hi everybody,

I'm attempting to classify 10 classes of hand pictures using a specific feature space. Training data is converted to a Mat object containing 240 rows (number of training pictures) and 480 cols (number of features) normalized between 0 and 1. The type of training data mat is CV_64FC1. I'm going to pass this matrix to PCA in order to extract useful features. After that I want to pass PCA result to SVM in order to train a classifier.

The PCA object returns all -NaN and Inf after creating the object:

PCA pca(training_data_mat, Mat(), CV_PCA_DATA_AS_ROW);

training_data_mat is the matrix of the training data with 240 rows and 450 cols for 240 training images and 450 features. It is created in the following line:

training_data_mat.push_back(extract_features_mat(descriptor,hog_ders)) ;

the extract_features_mat function is written such that returns a 1 row Mat of type CV_64FC1 containing features of the image. The following line is return value of the extract_features_mat function:

return Mat (1,feature_size, CV_64FC1, feature_value) ;

and feature_value is a double array defined and filled in this function.

I'm totally confused what is wrong!

This is my extract_features_mat function:

Mat extract_features_mat(Mat descriptor, std::vector<float> hog_ders)
{
        int feature_size = descriptor.cols + hog_ders.size() ;
        double feature_value[feature_size] ;
        double gamma = 0.05 ;
        for(int j = 0 ; j < descriptor.cols ; j++)
        {
            feature_value[j] = 0 ;
            for(int h = 0 ; h < descriptor.rows ; h++)
            {
                double to_be_added_value = (descriptor.at<double>(h,j)/pow(10,15) ) ;
                feature_value[j] += to_be_added_value;
                if(feature_value[j] > 1)
                    feature_value[j] = 1 ;
            }   
        }

        float max = 0 ;
        for(int j = 0 ; j < descriptor.cols ; j++)
        {
            if(feature_value[j] < 0.1 )
                feature_value[j] = 0.1 ;
            feature_value[j] = 1 * pow(feature_value[j],gamma) ;
            if(max < feature_value[j])
                max = feature_value[j] ;
        }

        for(int j =0 ; j < descriptor.cols ; j++)
            feature_value[j] = feature_value[j] / max ; 
        max = 0 ;
        for(int j = descriptor.cols ; j < descriptor.cols + hog_ders.size() ; j++)
        {
            feature_value[j] = hog_ders.at(j - descriptor.cols) ;
            if(max < feature_value[j])
                max = feature_value[j] ;
        }   
        for(int j = descriptor.cols ; j < descriptor.cols + hog_ders.size() ; j++)
            feature_value[j] = feature_value[j] / max ; 

        return Mat(1,feature_size, CV_64FC1, feature_value) ;
}