#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace NICE; using namespace OBJREC; int main(int argc, char **argv) { Config conf ( argc, argv ); string destForFeat = conf.gS("main", "destForFeat", "/tmp/"); double percentageOfPatchesForKMeans = conf.gD("main", "percentageOfPatchesForKMeans", 0.3); int nrOfClusters = conf.gI("main", "nrOfClusters", 200); bool verbose = conf.gB("main", "verbose", false); MultiDataset md ( &conf ); const LabeledSet & train = *(md["train"]); const LabeledSet & test = *(md["test"]); const ClassNames & classNamesTrain = md.getClassNames("train"); const ClassNames & classNamesTest = md.getClassNames("test"); LFonHSG GridForSIFT(&conf); std::vector featuresTrainset; LOOP_ALL_S ( train ) { EACH_S(classno, imgfilename); if (verbose) std::cerr << "imgfilename: " << imgfilename << std::endl; Image img (imgfilename); NICE::VVector positions; NICE::VVector features; // Extract the descriptor-Values from a given grayscale-Image. GridForSIFT.extractFeatures ( img, features, positions ); if (verbose) std::cerr << "features.size: " << features.size() << std::endl; featuresTrainset.push_back(features); } int trainsize(featuresTrainset.size()); int nrOfFivePercent(round(0.05*trainsize)); std::cerr << "Now sample randomly some patches (SIFT-features)" << std::endl; VVector x; x.clear(); int count(0); //randomly sampling of patches -- take percentageOfPatchesForKMeans % of the patches from each image for (int i = 0; i < trainsize; i++, count++) { //small stat bar if ( (count % nrOfFivePercent) == 0) std::cerr << "sampling for image nr: " << count << " / " << trainsize << std::endl; std::set indices; int currentSize(featuresTrainset[i].size()); for (int k = 0; k < (int) round(percentageOfPatchesForKMeans*currentSize); k++) { int idx( round((float)rand() * (currentSize-1)/ (float)(RAND_MAX)) ); while ( (indices.find(idx) != indices.end()) && (i > 0)) { idx = ( round((float)rand() * (currentSize-1)/ (float)(RAND_MAX)) ); } x.push_back(featuresTrainset[i][idx]); indices.insert(idx); } } std::cerr << "sampling done - final number of patches: " << x.size() << std::endl; std::cerr << "Apply k-Means on randomly sampled patches" << std::endl; VVector prototypes; std::vector weights; std::vector assignments; std::cerr << "nrOfClusters: " << nrOfClusters << std::endl; ClusterAlgorithm *clusteralg = new KMeans (nrOfClusters); clusteralg->cluster(x, prototypes, weights, assignments); delete clusteralg; std::cerr << "prototypes.size(): " << prototypes.size() << std::endl; CodebookPrototypes codebook ( prototypes ); std::cerr << "Now apply our k-means codebook to every image for computing the resulting features" << std::endl; FCCodebookHistBin featureGenerator(&conf, &GridForSIFT, "sum", &codebook); std::cerr << "codebook.size(): " << codebook.size() << std::endl; int cntTrain(0); LOOP_ALL_S ( train ) { //small stat bar if ( (cntTrain % nrOfFivePercent) == 0) std::cerr << "image nr: " << cntTrain << " / " << trainsize << std::endl; EACH_S(classno, imgfilename); std::string finalDest(classNamesTrain.text(classno) +imgfilename); size_t posOfEnding = finalDest.find_last_of("."); size_t lengthOfEnding = finalDest.size() - posOfEnding; finalDest = finalDest.erase(posOfEnding, lengthOfEnding)+".txt"; size_t posOfclassSlash = finalDest.find_last_of("/"); size_t posOfFolderSlash = finalDest.find_last_of("/", posOfclassSlash-1); finalDest = finalDest.erase(0, posOfFolderSlash+1); finalDest = destForFeat + finalDest; // std::cerr << "finalDest: " << finalDest<< std::endl; Image img (imgfilename); NICE::Vector vec; //compute the histogram featureGenerator.convert (img, vec ); std::filebuf fb; fb.open (finalDest.c_str(),ios::out); ostream outstream(&fb); outstream << vec; fb.close(); cntTrain++; } int testsize(0); LOOP_ALL_S ( test ) { testsize++; } int nrOfFivePercentTest(round(0.05*testsize)); int cntTest(0); LOOP_ALL_S ( test ) { //small stat bar if ( (cntTest % nrOfFivePercentTest) == 0) std::cerr << "image nr: " << cntTest << " / " << testsize << std::endl; EACH_S(classno, imgfilename); std::string finalDest(classNamesTest.text(classno) +imgfilename); size_t posOfEnding = finalDest.find_last_of("."); size_t lengthOfEnding = finalDest.size() - posOfEnding; finalDest = finalDest.erase(posOfEnding, lengthOfEnding)+".txt"; size_t posOfclassSlash = finalDest.find_last_of("/"); size_t posOfFolderSlash = finalDest.find_last_of("/", posOfclassSlash-1); finalDest = finalDest.erase(0, posOfFolderSlash+1); finalDest = destForFeat + finalDest; // std::cerr << "finalDest: " << finalDest<< std::endl; Image img (imgfilename); NICE::Vector vec; //compute the histogram featureGenerator.convert (img, vec ); std::filebuf fb; fb.open (finalDest.c_str(),ios::out); ostream outstream(&fb); outstream << vec; fb.close(); cntTest++; } return 0; }