double Distance(FV &distr_1, FV &distr_2) { FV m(distr_1.size()); for (int i = 0; i < distr_1.size(); ++i) { m[i] = (distr_1[i] + distr_2[i]) / 2.0; } return (KL(distr_1, m) + KL(distr_2, m)) / 2; }
void solve() { Zero(num); dig = 0; while (n != 0) { FVi it = upper_bound(fis.begin(), fis.end(), Fi(n, 0)); --it; if (dig == 0) dig = it->d; num[it->d - 1] = 1; n -= it->f; } }
void prepare() { int a, b, c; fis.push_back(Fi(1, 1)); fis.push_back(Fi(2, 2)); a = 1, b = 2; int d = 3; while (true) { c = a + b; fis.push_back(Fi(c, d++)); if (c > MAXN) break; a = b, b = c; } }
double KL(FV &p, FV &q) { double kl = 0.0; double denom1 = 0.0; double denom2 = 0.0; for (int i = 0; i < p.size(); ++i) { // kl += p[i] * log(p[i] / q[i]); kl += abs(p[i] - q[i]); denom1 = p[i] * p[i]; denom2 = q[i] * q[i]; } return kl; }
int main(int argc, char **argv) { vector<FV> all_nodes_trans; ifstream ifs (argv[1]); string line; double dbl; while (getline(ifs, line)) { istringstream iss(line); FV node; while (iss >> dbl) { node.push_back(dbl); } // end while node.pop_back(); node.pop_back(); node.pop_back(); all_nodes_trans.push_back(node); } // end of model file ifs.close(); vector<FV> all_nodes(all_nodes_trans[0].size()); // as many words for (int i = 0; i < all_nodes_trans[0].size(); ++i) { FV word_vec(all_nodes_trans.size()); double sum = 0; for (int j = 0; j < all_nodes_trans.size(); ++j) { word_vec[j] = all_nodes_trans[j][i]; sum += word_vec[j]; } for (int j = 0; j < all_nodes_trans.size(); ++j) { word_vec[j] /= sum; } all_nodes[i] = word_vec; } cerr << all_nodes.size() << " number of words" << endl; cerr << all_nodes[0].size() << " dimensions" << endl; vector<double> js_div(all_nodes.size()); map<int, map<int, double> > memoize; set<int> not_done; int starter = 0; double super_max = -1000000; for (int i = 0; i < all_nodes.size(); ++i) { not_done.insert(i); for (int j = 0; j < all_nodes.size(); ++j) { if (j == i) { js_div[j] = 0; continue; } double this_js_div; memoize[i][j] = KL(all_nodes[i], all_nodes[j]); js_div[j] = this_js_div; cerr << memoize[i][j] << " "; } // end candidate nodes cerr << endl; double max_distance = *max_element(js_div.begin(), js_div.end()); if (max_distance > super_max) { super_max = max_distance; starter = i; } } // end nodes do { cout << starter; not_done.erase(starter); double min_distance = 100000000; int closest = 1; for (set<int>::iterator i = not_done.begin(); i != not_done.end(); ++i) { if (memoize[starter][*i] < min_distance) { min_distance = memoize[starter][*i]; closest = *i; } } starter = closest; cout << " " << min_distance << endl; } while(not_done.size()); return 0; }
//---------------------------------------------------------------------------------- int main(int argc,char* argv[]) { ofstream fut; fut.open("panProcess.dat", ios::binary | ios::out); fut.write((char*)'1', sizeof(char)); fut.close(); exit(1); Point matchLoc; char winname[] = "window"; //^template matching params Mat background; Mat image; String file(argv[0]); /*cout << "enter the file name to be analyzed \t"; cin >> file;*/ file += argv[1]; imgSample = imread(file, CV_LOAD_IMAGE_UNCHANGED); imgOriginal = imread(file, CV_LOAD_IMAGE_UNCHANGED); if (imgSample.data == NULL){ cout << "hi there"; return -1; } if (imgOriginal.data == NULL){ cout << "hi there"; return -1; } winarr[0] = 'w';//global window array winarr[1] = 'i'; winarr[2] = 'n'; winarr[3] = 't'; winarr[4] = 't'; winarr[5] = '.'; winarr[6] = 'p'; winarr[7] = 'n'; winarr[8] = 'g'; winarr[9] = '\0'; // //if (choice == 2){ // system("cls"); // cout << "---------PAN CARD dataset creation-------"; // vector<vector<Point>> namecont; // //create templates // cout << "--------CREATE Templates---------"; // ::i = 0; // winarr[3] = '5'; // namedWindow(winname, WINDOW_AUTOSIZE); // setMouseCallback(winname, mcallback, NULL); // imshow(winname, imgOriginal); // waitKey(0); // Mat passer = imgOriginal.clone(); // for (int c = 0; c < ::i; c++){ // Mat imag = imgOriginal(Rect(points[c][0], points[c][2])); // //fillConvexPoly(passer, points[c], NUMPTS, Scalar(255, 255, 255)); // winarr[4] = c + 48; // imwrite(winarr, imag); // } // system("pause"); // cout << "creating the background interface data\n"; // //getbkgd(passer, background, 1, 37, 31, 20); // //getcontours(passer, namecont); // cout << "got it"; // // } Methods S; datafile testout; PAN pan; Mat panimg = (imgOriginal.clone()); PAN::Image output; vector<vector<Point>> contours; //detect text is required //local vars pan.init(&panimg, 0, panimg.cols, 0, panimg.rows, Point2f(0, 0)); Mat snipped = pan.getcontours(contours, TEXT);//u get only text contours contours = pan.mergecontours(contours); OCRit(snipped, contours, testout.data, snipped, "eng");//global function pan.authenticate(argv[2]); testout.authenticity = pan.getauthenticity(); //extract signature snipped = pan.getcontours(contours, SIGN); Mat sign = pan.extractSIGN(contours); imshow("sign", sign); waitKey(); pan.showpan(); //will use fake database too after sometime but not now //String FakeDatabase = CWD + "\\fake\\sign1" + (char)48 + ".png"; //cout << OrigDatabase << "\n\n-----------\n\n"; //cout << FakeDatabase << "\n\n-----------\n\n"; //--------------------------------------------------------------------- //ifstream sigfile("sig.dat", ios::binary | ios::in); vector<Methods::Image> *std = new vector<Methods::Image >(); vector<FV::feature> *reference = new vector<FV::feature >(); FV refV; FV tesV; float tolerance = (float)0.1; /*if (sigfile.is_open()){ refV.load("sig.dat"); } else*/{ //Mat sign=imread("sign10.png"); S.preprocess(&sign, std); S.partitionImage(&sign, NUMPART, std); S.calculateCOG(std); S.calculateCOGDistances(std); S.calculateCOGAngles(std); //S.showSubImages(std, &sign, "sign10.png"); refV.add(std, reference); } float result = 0, count = 0, sum = 0; for (int i = 10; i < 19; i++){ Methods T; vector<Methods::Image> *signtest = new vector<Methods::Image >(); vector<FV::feature> *test = new vector<FV::feature >(); String imagename(argv[0]); imagename =imagename + "signature//sign"; imagename = imagename + (char)((i / 10) + 48) + (char)((i % 10) + 48) + ".png"; Mat testsign = imread(imagename); //cout << OrigDatabase + imagename << "\n-------------------------\n"; T.preprocess(&testsign, signtest); T.partitionImage(&testsign, NUMPART, signtest); T.calculateCOG(signtest); T.calculateCOGDistances(signtest); T.calculateCOGAngles(signtest); tesV.add(signtest, test); //T.showSubImages(signtest, &testsign, imagename); result = refV.compareNupdate(tesV.standard, tolerance, 70); if (result >= 70){ sum += result; count++; } } if (count != 0){ sum /= count; cout << "good matches were " << count<<"\n"; testout.sigverif = sum; } else{ testout.sigverif = 0; } //--------------------------------------------------------------------- //write testout to a file ofstream fout; fout.open("panProcess.dat", ios::binary | ios::out); fout.write((char*)&testout, sizeof(testout)); fout.close(); }