void GLinearRegressor_linear_test(GRand& prng) { // Train GMatrix features1(0, 3); GMatrix labels1(0, 1); for(size_t i = 0; i < 1000; i++) { GVec& vec = features1.newRow(); vec[0] = prng.uniform(); vec[1] = prng.uniform(); // irrelevant attribute vec[2] = prng.uniform(); labels1.newRow()[0] = 0.3 * vec[0] + 2.0 * vec[2] + 5.0; } GLinearRegressor lr; lr.train(features1, labels1); // Check some values if(lr.beta()->rows() != 1 || lr.beta()->cols() != 3) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[0] - 0.3) > 1e-6) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[1] - 0.0) > 1e-6) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[2] - 2.0) > 1e-6) throw Ex("failed"); if(std::abs(lr.epsilon()[0] - 5.0) > 1e-6) throw Ex("failed"); // Test GMatrix features2(0, 3); GMatrix labels2(0, 1); for(size_t i = 0; i < 1000; i++) { GVec& vec = features2.newRow(); vec[0] = prng.uniform(); vec[1] = prng.uniform(); // irrelevant attribute vec[2] = prng.uniform(); labels2.newRow()[0] = 0.3 * vec[0] + 2.0 * vec[2] + 5.0; } double rmse = sqrt(lr.sumSquaredError(features2, labels2) / features2.rows()); if(rmse > 0.0224) throw Ex("failed"); }
int main(int argc, char *argv[]) { if (argc != 4) { cout << "Usage: " << endl; cout << "ACCV2009.exe Folder imfn1 imfn2" << endl; return -1; } string folder = argv[1]; string imfn1 = argv[2]; string imfn2 = argv[3]; string outfolder = "../output"; _mkdir(outfolder.c_str()); Mat im1 = imread(folder + imfn1 + ".png", 1); Mat im2 = imread(folder + imfn2 + ".png", 1); int width = im1.cols; int height = im1.rows; //颜色空间转换 Mat im1_H, im1_S, im1_I; Mat im2_H, im2_S, im2_I; //CvtColorBGR2HSI(im1, im1_H, im1_S, im1_I); //CvtColorBGR2HSI(im2, im2_H, im2_S, im2_I); cv_CvtColorBGR2HSI(im1, im1_H, im1_S, im1_I); cv_CvtColorBGR2HSI(im2, im2_H, im2_S, im2_I); int sigmaS = 5; float sigmaR = 10; int minR = 800; vector<int> labels1(0), labels2(0); //#define RQ_DEBUG #ifdef RQ_DEBUG int regionum1, regionum2; DoMeanShift(im1, sigmaS, sigmaR, minR, labels1, regionum1); DoMeanShift(im2, sigmaS, sigmaR, minR, labels2, regionum2); cout << endl; saveLabels(labels1, width, height, outfolder+"/labels_" + imfn1 + ".txt"); saveLabels(labels2, width, height, outfolder+"/labels_" + imfn2 + ".txt"); #else int regionum1, regionum2; readLabels(outfolder+"/labels_" + imfn1 + ".txt", width, height, labels1, regionum1); readLabels(outfolder+"/labels_" + imfn2 + ".txt", width, height, labels2, regionum2); #endif string siftmatchfn = folder + "matches_sift.txt"; vector<Point2f> features1(0), features2(0); ReadSiftMatches(siftmatchfn, features1, features2); vector<vector<Point2f>> matches1(0), matches2(0); vector<vector<Point2f>> pixelTable2(0); ComputeRegionMatches(labels2, regionum2, width, features1, features2, matches1, matches2); cout << "all regions have matches." << endl << endl; //显示每个区域的matches : 显示一次就好 vector<float> DeltaH(0), DeltaS(0), DeltaI(0); RegionDeltaColor(im1_H, im2_H, matches1, matches2, DeltaH); RegionDeltaColor(im1_S, im2_S, matches1, matches2, DeltaS); RegionDeltaColor(im1_I, im2_I, matches1, matches2, DeltaI); Mat new_im2_H, new_im2_S, new_im2_I; CorrectColor(im2_H, labels2, DeltaH, new_im2_H); CorrectColor(im2_S, labels2, DeltaS, new_im2_S); CorrectColor(im2_I, labels2, DeltaI, new_im2_I); Mat new_im2; //CvtColorHSI2BGR(new_im2_H, new_im2_S, new_im2_I, new_im2); cv_CVtColorHSI2BGR(new_im2_H, new_im2_S, new_im2_I, new_im2); cout << "done." << endl; imshow("new im2", new_im2); waitKey(0); destroyAllWindows(); string savefn = outfolder + "/accv2009_" + imfn2 + ".png"; cout << "save " << savefn << endl; imwrite(savefn, new_im2); }
void classify(){ cv::HOGDescriptor hog, hog2; cv::Mat image, im; std::string name; const cv::Size trainingPadding = cv::Size(0,0); const cv::Size winStride = cv::Size(8,8);//Strid 16= no overlap std::vector<cv::Point> locations; std::vector<float> featureVector, featureVector2; std::vector<int> voting(3); CvSVM SVM_pen, SVM_scissors, SVM_pen_scissors; float classification_result1, classification_result2, classification_result3; cv::Mat circle= cv::Mat::zeros(cv::Size(RES_X,RES_Y),CV_8U); int thickness=-1, lineType=8; int max; cv::circle(circle, cv::Size(circle.cols/2, circle.rows/2),circle.rows/2, 255, thickness, lineType ); //filling vector for voting std::fill( voting.begin(), voting.end(), 0 ); hog.blockSize= cv::Size(16,16); hog.winSize= cv::Size(RES_X,RES_Y); hog.blockStride=cv::Size(8,8); hog.cellSize= cv::Size(8,8); hog2.blockSize= cv::Size(16,16); hog2.winSize= cv::Size(RES_X,RES_Y); hog2.blockStride=cv::Size(8,8); hog2.cellSize= cv::Size(16,16); //Loading support vector machines SVM_scissors.load("svm_scissors"); SVM_pen.load("svm_pen"); SVM_pen_scissors.load("pen_scissors_svm"); image=extracted_image; cv::resize(image, im, cv::Size(RES_X,RES_Y)); im.copyTo(image, circle); //Finding features hog.compute(image, featureVector, winStride, trainingPadding, locations); hog2.compute(image, featureVector2, winStride, trainingPadding, locations); cv::Mat features(1, featureVector.size(), CV_32F); cv::Mat features2(1, featureVector2.size(), CV_32F); for(int j=0; j<featureVector.size(); j++){ features.at<float>(0,j)= featureVector.at(j); } for(int j=0; j<featureVector2.size(); j++){ features2.at<float>(0,j)= featureVector2.at(j); } //Evaluating features in the support vector machine classification_result1= SVM_scissors.predict(features); classification_result2= SVM_pen.predict(features); classification_result3= SVM_pen_scissors.predict(features2); std::cout<<"Output of scissors classifier:"<<classification_result1<<std::endl; std::cout<<"Output of pen classifier:"<<classification_result2<<std::endl; std::cout<<"Output of scissors/pen classifier:"<<classification_result3<<std::endl; //Deciding the classification result classification_result3= SVM_pen_scissors.predict(features2); if( classification_result3==1){ classification_result1= SVM_scissors.predict(features); if(classification_result1==1) { std::cout<<"IT IS A SCISSORS."<<std::endl; c= SCISSORS; } else{ std::cout<<"IT IS NEITHER A SCISSOPRS NOR A PEN"<<std::endl; c= NO_IDENTIFIED; } } else { classification_result2= SVM_pen.predict(features); if(classification_result2==1) { std::cout<<"IT IS A PEN."<<std::endl; c= PEN; } else{ std::cout<<"IT IS NEITHER A SCISSORS NOR A PEN."<<std::endl; c= NO_IDENTIFIED; } } }