void CV_KNearestTest::run( int /*start_from*/ )
{
    int sizesArr[] = { 500, 700, 800 };
    int pointsCount = sizesArr[0]+ sizesArr[1] + sizesArr[2];

    // train data
    Mat trainData( pointsCount, 2, CV_32FC1 ), trainLabels;
    vector<int> sizes( sizesArr, sizesArr + sizeof(sizesArr) / sizeof(sizesArr[0]) );
    vector<Mat> means, covs;
    defaultDistribs( means, covs );
    generateData( trainData, trainLabels, sizes, means, covs, CV_32FC1 );

    // test data
    Mat testData( pointsCount, 2, CV_32FC1 ), testLabels, bestLabels;
    generateData( testData, testLabels, sizes, means, covs, CV_32FC1 );

    int code = cvtest::TS::OK;
    KNearest knearest;
    knearest.train( trainData, trainLabels );
    knearest.find_nearest( testData, 4, &bestLabels );
    if( calcErr( bestLabels, testLabels, sizes, true ) > 0.01f )
    {
        ts->printf( cvtest::TS::LOG, "bad accuracy on test data" );
        code = cvtest::TS::FAIL_BAD_ACCURACY;
    }
    ts->set_failed_test_info( code );
}
Example #2
0
bool DigitRecognizer::train(char *trainPath, char *labelsPath)
{
	FILE *fp = fopen(trainPath, "rb");
	FILE *fp2 = fopen(labelsPath, "rb");

	if(!fp || !fp2)
		return false;

	// Read bytes in flipped order
	int magicNumber = readFlippedInteger(fp);
	numImages = readFlippedInteger(fp);
	numRows = readFlippedInteger(fp);
	numCols = readFlippedInteger(fp);

	// printf("Magic number: %4x\n", magicNumber);
	//printf("Number of images: %d\n", numImages);
	//printf("Number of rows: %d\n", numRows);
	//printf("Number of columns: %d\n", numCols);

	fseek(fp2, 0x08, SEEK_SET);

	if(numImages > MAX_NUM_IMAGES) numImages = MAX_NUM_IMAGES;

	//////////////////////////////////////////////////////////////////
	// Go through each training data entry and figure out a
	// center for each digit

	int size = numRows*numCols;
	CvMat *trainingVectors = cvCreateMat(numImages, size, CV_32FC1);
	CvMat *trainingClasses = cvCreateMat(numImages, 1, CV_32FC1);

	memset(trainingClasses->data.ptr, 0, sizeof(float)*numImages);

	jbyte *temp = new jbyte[size];
	jbyte tempClass=0;
	for(int i=0;i<numImages;i++)
	{
		fread((void*)temp, size, 1, fp);
		fread((void*)(&tempClass), sizeof(jbyte), 1, fp2);

		trainingClasses->data.fl[i] = tempClass;

		// Normalize the vector
		/*float sumofsquares = 0;
		for(int k=0;k<size;k++)
			sumofsquares+=temp[k]*temp[k];
		sumofsquares = sqrt(sumofsquares);*/

		for(int k=0;k<size;k++)
			trainingVectors->data.fl[i*size+k] = temp[k]; ///sumofsquares;
	}

	knn->train(trainingVectors, trainingClasses);
	fclose(fp);
	fclose(fp2);

	return true;
}
Example #3
0
    /** @function main */  
    int main(int argc, char** argv)  
    {  
      int descriptor_size;
     // start the timer
     clock_t time=clock(); 
      
//     CvMat* trainData = cvCreateMat(classes * train_samples,ImageSize, CV_32FC1);  
     CvMat* trainData;
     if (descriptor==1) descriptor_size=HOG1_size; 
     else if (descriptor==2) descriptor_size=ImageSize;
     else if (descriptor==3) descriptor_size=HOG3_size;
     else if (descriptor==4) descriptor_size=HOG4_size;
     
     trainData = cvCreateMat(classes * train_samples,descriptor_size, CV_32FC1);
     CvMat* trainClasses = cvCreateMat(classes * train_samples, 1, CV_32FC1);  
      
     namedWindow("single", CV_WINDOW_AUTOSIZE);  
     // namedWindow("all",CV_WINDOW_AUTOSIZE);  
      
     LearnFromImages(trainData, trainClasses);  
     
     KNearest knearest;
     CvSVM SVM;  
         
     switch (classifier) {
      case 1:
      {
       // Set up SVM's parameters
       CvSVMParams params;
      // params.svm_type    = CvSVM::C_SVC;
       params.kernel_type = CvSVM::LINEAR;
       params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

       // Train the SVM
       SVM.train(trainData, trainClasses, Mat(), Mat(), params);
       SVM.save("SVM_training_data");
       break;
      }
      case 2:
      {
       knearest.train(trainData, trainClasses);  
       break;
      }
     }
     
     time=clock()-time;
     float training_time=((float)time)/CLOCKS_PER_SEC;   //time for single run
     cout<<"Training Time "<<training_time<<"\n";     
     
     //RunSelfTest(knearest, SVM);  
     cout << "Testing\n";  
     
     time=clock();
     AnalyseImage(knearest, SVM); 
     time=clock()-time;
     float run_time=((float)time)/CLOCKS_PER_SEC;   //time for single run
     cout<<"Run Time "<<run_time<<"\n"; 
          
     waitKey(0);
     return 0;  
      
    }