//Rate a location on how likely it is to be a bubble double rateBubble(Mat& det_img_gray, Point bubble_location, PCA& my_PCA){ Mat query_pixels, pca_components; getRectSubPix(det_img_gray, Point(14,18), bubble_location, query_pixels); query_pixels.reshape(0,1).convertTo(query_pixels, CV_32F); pca_components = my_PCA.project(query_pixels); //The rating is the SSD of query pixels and their back projection Mat out = my_PCA.backProject(pca_components)- query_pixels; return sum(out.mul(out)).val[0]; }
int predict(PCA &pca, Mat &train, Mat face, double threshold = 0.3, double distance = 44000){ Mat w = pca.project(face); Mat predicted = pca.backProject(w); int label = -1; double min = distance * threshold; for(int i = 0; i < 15; i++){ for(int j = 0; j < 8; j++){ double d = norm(predicted, train.row((i*8) + j)); if(d < min){ min = d; label = i; } } } return label; }
void FMR_FNMR(PCA &pca, Mat &train, Mat &test, double step = 0.05, double distance = 44000.0){ struct stat { double total{0}, match{0}; double res() { return match/total; } }; Mat w, r; cout << "Threshold;" << "FMR;" << "FNMR;" << "ROC TP;" << "ROC FP" << endl; for(double t = 0.0; t <= 1.000001; t += step) { double min = distance * t; stat fmr, fnmr; for(int i = 0; i < 15; i++){ for(int j = 0; j < 3; j++){ Mat face = test.row((i*3) + j); pca.project(face, w); pca.backProject(w, r); for(int k = 0; k < 15; k++) { if(k == i){ // FNMR for(int l = 0; l < 8; l++) { double d = norm(r, train.row((k*8) + l)); if(d > min){ fnmr.match++; } // else { // ROC TP // } fnmr.total++; } } else { // FMR for(int l = 0; l < 8; l++){ double d = norm(r, train.row((k*8) + l)); if(d < min){ fmr.match++; // ROC FP } fmr.total++; } } } } } cout << 1-t << ";" << fmr.res() << ";" << fnmr.res() << ";" << ((fnmr.total-fnmr.match)/fnmr.total) << ";" << fmr.res() << endl; } }