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 ); }
void RunSelfTest(KNearest& knn2) { Mat img; CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1); // SelfTest char file[255]; int z = 0; while (z++ < 10) { int iSecret = rand() % 10; //cout << iSecret; sprintf(file, "%s/%d.png", pathToImages, iSecret); img = imread(file, 1); Mat stagedImage; PreProcessImage(&img, &stagedImage, sizex, sizey); for (int n = 0; n < ImageSize; n++) { sample2->data.fl[n] = stagedImage.data[n]; } float detectedClass = knn2.find_nearest(sample2, 1); if (iSecret != (int) ((detectedClass))) { exit(1); } sprintf(file, "/sdcard/exartisan_pics/%d.png", iSecret); imwrite(file,img); } }
void RunSelfTest(KNearest& knn2) { Mat img; CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1); // SelfTest char file[255]; int z = 0; while (z++ < 10) { int iSecret = rand() % 10; //cout << iSecret; sprintf(file, "%s/%d.png", pathToImages, iSecret); img = imread(file, 1); Mat stagedImage; PreProcessImage(&img, &stagedImage, sizex, sizey); for (int n = 0; n < ImageSize; n++) { sample2->data.fl[n] = stagedImage.data[n]; } float detectedClass = knn2.find_nearest(sample2, 1); if (iSecret != (int) ((detectedClass))) { cout << "Falsch. Ist " << iSecret << " aber geraten ist " << (int) ((detectedClass)); exit(1); } cout << "Richtig " << (int) ((detectedClass)) << "\n"; imshow("single", img); waitKey(0); } }
float DigitRecognizer::classify(cv::Mat img) { imwrite("/sdcard/digit/classify.png",img); // Mat cloneImg = preprocessImage(img); // return knn->find_nearest(Mat_<float>(cloneImg), 1); return knn->find_nearest(img, 1); }
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; }
void analyseImage(char filename[], KNearest knearest){ CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1); Mat image, gray, blur, thresh; vector < vector<Point> > contours; image = imread(filename, 1); if(image.empty()){ cout << "Error: File \"" << filename << "\" not found." << endl; exit(0); } imshow(filename,image); //Preprocessing cvtColor(image, gray, COLOR_BGR2GRAY); GaussianBlur(gray, blur, Size(5, 5), 2, 2); adaptiveThreshold(blur, thresh, 255, 1, 1, 11, 2); findContours(thresh, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); float charsequence[20]; float orderedsequence[20]; int char_x[20]; //the x-coordinate of the characters; used in sorting initializeArray(char_x); int counter=0; for (size_t i = 0; i < contours.size(); i++){ vector < Point > cnt = contours[i]; Rect rec = boundingRect(cnt); float ar_min = 0.30; //aspect_ratio_min float ar_max = 0.90; //aspect_ratio_max //cout << "rec width: " << rec.width; //cout << "rec hieght: " << rec.height << " \n "; float rec_ar = ((float)rec.width/(float)rec.height); //cout << "rec ar: " << rec_ar; if (rec.height > 28 && (rec_ar > ar_min && rec_ar < ar_max)){ Mat roi = image(rec); Mat stagedImage; preProcessImage(&roi, &stagedImage, sizex, sizey); for (int n = 0; n < ImageSize; n++){ sample2->data.fl[n] = stagedImage.data[n]; } float result = knearest.find_nearest(sample2, 1); charsequence[counter]=result; char_x[counter] = rec.x; counter++; rectangle(image, Point(rec.x, rec.y), Point(rec.x + rec.width, rec.y + rec.height), Scalar(0, 0, 255), 2); } } imshow("License Plate Number Recognizer", image); cout << "---------------------------------" << endl; cout << "License Plate Number: "; string license_plate = ""; orderCharacters(orderedsequence, charsequence, char_x); for (int a=0; a<counter; a++){ license_plate = license_plate + decodeCharacter(orderedsequence[a]); } cout << license_plate; cout << endl << "---------------------------------" << endl; }
void AnalyseImage(KNearest knearest, CvSVM SVM) { //CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1); CvMat* sample2; Mat _image,image, gray, blur, thresh; vector < vector<Point> > contours; _image = imread("./images/37.png", 1); //image = imread("./images/all_4.png", 1); resize(_image,image,Size(2*sizex,1.2*sizey)); cvtColor(image, gray, COLOR_BGR2GRAY); GaussianBlur(gray, blur, Size(5, 5), 2, 2); adaptiveThreshold(blur, thresh, 255, 1, 1, 11, 2); findContours(thresh, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); float digits[contours.size()]; float number; for (size_t i = 0; i < contours.size(); i++) { vector < Point > cnt = contours[i]; if (contourArea(cnt) > 50) { Rect rec = boundingRect(cnt); if (rec.height > 28) { Mat roi = image(rec); Mat stagedImage; // Descriptor switch (descriptor) { case 1: {// HOG descriptor HOGDescriptor hog; vector<float> ders; vector<Point> locs; resize(roi,stagedImage,Size(64,128)); hog.compute(stagedImage,ders,Size(0,0),Size(0,0),locs); //cout<<ders.size()<<"\n"; sample2 = cvCreateMat(1, ders.size(), CV_32FC1); for (int n = 0; n < ders.size(); n++) { sample2->data.fl[n] = ders.at(n); } break; } case 2: { // Image Data descriptor sample2 = cvCreateMat(1, ImageSize, CV_32FC1); PreProcessImage(&roi, &stagedImage, sizex, sizey); for (int n = 0; n < ImageSize; n++) { sample2->data.fl[n] = stagedImage.data[n]; } break; } case 3: {// HOG3 detector resize(roi,stagedImage,Size(sizex,sizey)); IplImage copy = stagedImage; IplImage* img2 = © vector<float> ders; HOG3(img2,ders); sample2 = cvCreateMat(1, ders.size(), CV_32FC1); for (int n = 0; n < ders.size(); n++) { sample2->data.fl[n] = ders.at(n); } break; } case 4: { // Histogram as descriptor resize(roi,stagedImage,Size(sizex,sizey)); // Establish the number of bins int histSize = 9; // Set the ranges (for grayscale values) float range[] = { 0, 256 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; Mat ders; // histogram descriptor int ders_size=ders.rows*ders.cols; sample2 = cvCreateMat(1, ders_size, CV_32FC1); // Compute the histograms: calcHist( &stagedImage, 1, 0, Mat(), ders, 1, &histSize, &histRange, uniform, accumulate ); normalize( ders, ders, 0, 1, NORM_MINMAX, -1, Mat() ); for (int n = 0; n < ders_size; n++) { sample2->data.fl[n] = ders.data[n]; } break; } } // Classifier float result; switch (classifier) { case 1: { result = SVM.predict(sample2); break; } case 2: { result = knearest.find_nearest(sample2, 1); break; } } digits[contours.size()-i-1]=result; rectangle(image, Point(rec.x, rec.y), Point(rec.x + rec.width, rec.y + rec.height), Scalar(0, 0, 255), 2); imshow("all", image); cout << result << "\n"; imshow("single", stagedImage); waitKey(0); } } } number=digits[0]*10+digits[1]; cout<< "number is "<<number<<"\n"; }
void RunSelfTest(KNearest& knn2, CvSVM& SVM2) { Mat img; //CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1); CvMat* sample2; // SelfTest char file[255]; int z = 0; while (z++ < 10) { int iSecret = rand() % classes; //cout << iSecret; sprintf(file, "%s/%d/%d.png", pathToImages, iSecret, rand()%train_samples); img = imread(file, 1); Mat stagedImage; switch (descriptor) { case 1: {// HOG descriptor HOGDescriptor hog; vector<float> ders; vector<Point> locs; resize(img,stagedImage,Size(64,128)); hog.compute(stagedImage,ders,Size(0,0),Size(0,0),locs); //cout<<ders.size()<<"\n"; sample2 = cvCreateMat(1, ders.size(), CV_32FC1); for (int n = 0; n < ders.size(); n++) { sample2->data.fl[n] = ders.at(n); } break; } case 2: { // Image data as descriptor sample2 = cvCreateMat(1, ImageSize, CV_32FC1); PreProcessImage(&img, &stagedImage, sizex, sizey); for (int n = 0; n < ImageSize; n++) { sample2->data.fl[n] = stagedImage.data[n]; } break; } case 3: { // HOG3 descriptor resize(img,stagedImage,Size(sizex,sizey)); IplImage copy = stagedImage; IplImage* img2 = © vector<float> ders; HOG3(img2,ders); sample2 = cvCreateMat(1, ders.size(), CV_32FC1); for (int n = 0; n < ders.size(); n++) { sample2->data.fl[n] = ders.at(n); } break; } case 4: { // Histogram as descriptor resize(img,stagedImage,Size(sizex,sizey)); // Establish the number of bins int histSize = 9; // Set the ranges (for grayscale values) float range[] = { 0, 256 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; Mat ders; // histogram descriptor int ders_size=ders.rows*ders.cols; sample2 = cvCreateMat(1, ders_size, CV_32FC1); // Compute the histograms: calcHist( &stagedImage, 1, 0, Mat(), ders, 1, &histSize, &histRange, uniform, accumulate ); normalize( ders, ders, 0, 1, NORM_MINMAX, -1, Mat() ); for (int n = 0; n < ders_size; n++) { sample2->data.fl[n] = ders.data[n]; } break; } } float detectedClass; switch (classifier) { case 1: { detectedClass = SVM2.predict(sample2); break; } case 2: { detectedClass = knn2.find_nearest(sample2, 1); break; } } if (iSecret != (int) ((detectedClass))) { cout << "False " << iSecret << " matched with " << (int) ((detectedClass)); exit(1); } cout << "Right " << (int) ((detectedClass)) << "\n"; imshow("single", stagedImage); waitKey(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; }
bool AnalyseImage(KNearest knearest) { CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1); Mat image, gray, blur, thresh; vector < vector<Point> > contours; image = imread("/sdcard/exartisan_pics/answer_number_code.jpg", 1); if(!image.data) return 0; char file[255]; Mat text_img(image.size(), CV_8U); rectangle(text_img,Rect(0,0,text_img.cols,text_img.rows),Scalar(255,255,255,255),CV_FILLED); cvtColor(image, gray, COLOR_BGR2GRAY); GaussianBlur(gray, blur, Size(5, 5), 2, 2); adaptiveThreshold(blur, thresh, 255, 1, 1, 11, 2); findContours(thresh, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); for (size_t i = 0; i < contours.size(); i++) { vector < Point > cnt = contours[i]; if (contourArea(cnt) > 50) { Rect rec = boundingRect(cnt); if ((rec.height > 27)&&(rec.height < 35)&&(rec.width < 28)) { Mat roi = image(rec); Mat stagedImage; PreProcessImage(&roi, &stagedImage, sizex, sizey); for (int n = 0; n < ImageSize; n++) { sample2->data.fl[n] = stagedImage.data[n]; } // bug in here..... float result = knearest.find_nearest(sample2, 1); // float result = dr->classify(sample2); rectangle(image, Point(rec.x, rec.y), Point(rec.x + rec.width, rec.y + rec.height), Scalar(0, 0, 255), 2); if((rec.height/rec.width)>3) result = 1; text_img.data[(10)*text_img.cols+rec.x] = round(result); } } } if (abc ==1) imwrite("/sdcard/exartisan_pics/abc_1.png",image); int m=0;char tt; for (int k=0;k<text_img.rows;k++) for (int l=0;l<text_img.cols;l++) { if (text_img.data[k*text_img.cols+l]<10) { sprintf(&tt,"%d",text_img.data[k*text_img.cols+l]); text[m] = tt; m++; } } text[m] = '\0'; putText(image, text, Point(50, 30), FONT_HERSHEY_PLAIN, 1.5, Scalar(255,100,100,255), 2.0); imwrite("/sdcard/exartisan_pics/answer_number_code_result.jpg", image); cvReleaseMat( &sample2 ); if (m != 16) { // sprintf(text,"recognition failed!"); return false; } return true; }