Professional Documents
Culture Documents
#include <stdio.h>
/******************************************************************************/
#define NUMBER_OF_CLASSES 26
/******************************************************************************/
// loads the sample database from file (which is a CSV text file)
fclose(f);
return 1; // all OK
}
/******************************************************************************/
// define training data storage matrices (one for attribute examples, one
// for classifications)
CvMat* training_data =
cvCreateMat(NUMBER_OF_TRAINING_SAMPLES, ATTRIBUTES_PER_SAMPLE,
CV_32FC1);
CvMat* training_classifications =
cvCreateMat(NUMBER_OF_TRAINING_SAMPLES, 1, CV_32FC1);
CvMat* testing_data =
cvCreateMat(NUMBER_OF_TESTING_SAMPLES, ATTRIBUTES_PER_SAMPLE,
CV_32FC1);
CvMat* testing_classifications =
cvCreateMat(NUMBER_OF_TESTING_SAMPLES, 1, CV_32FC1);
//
termination criteria for learning algorithm
);
printf( "(OpenCV 2.0 'grid search' => may take some time!)");
fflush(NULL);
svm->train_auto(training_data, training_classifications,
0, 0, params, 10);
params = svm->get_params();
printf( "\nUsing optimal parameters degree %f, gamma %f, ceof0
%f\n\t C %f, nu %f, p %f\n Training ..",
params.degree, params.gamma, params.coef0, params.C,
params.nu, params.p);
#else
svm->train(training_data, training_classifications,
0, 0, params);
#endif
printf( ".... Done\n");
CvMat test_sample;
int correct_class = 0;
int wrong_class = 0;
int false_positives [NUMBER_OF_CLASSES];
char class_labels[NUMBER_OF_CLASSES];
float result;
result = svm->predict(&test_sample);
if (fabs(result - CV_MAT_ELEM(*testing_classifications,
float, tsample, 0))
>= FLT_EPSILON)
{
// if they differ more than floating point error
=> wrong class
wrong_class++;
false_positives[((int)
CV_MAT_ELEM(*testing_classifications, float, tsample, 0)) - 1]++;
} else {
// otherwise correct
correct_class++;
}
}
cvReleaseMat( &training_data );
cvReleaseMat( &training_classifications );
cvReleaseMat( &testing_data );
cvReleaseMat( &testing_classifications );
return 0;
}
return -1;
}
/******************************************************************************/