computeSIFTFeatures.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <core/vector/VectorT.h>
  5. #include <core/vector/MatrixT.h>
  6. #include <core/image/ImageT.h>
  7. #include <core/basics/Config.h>
  8. #include <core/vector/VVector.h>
  9. #include <vislearning/cbaselib/MultiDataset.h>
  10. #include <vislearning/features/localfeatures/LFonHSG.h>
  11. #include <vislearning/features/localfeatures/GenericLocalFeatureSelection.h>
  12. #include <vislearning/features/simplefeatures/FCCodebookHistBin.h>
  13. #include <vislearning/features/simplefeatures/CodebookPrototypes.h>
  14. #include <vislearning/math/cluster/KMeans.h>
  15. #include <set>
  16. #include <vislearning/baselib/Globals.h>
  17. using namespace std;
  18. using namespace NICE;
  19. using namespace OBJREC;
  20. int main(int argc, char **argv)
  21. {
  22. Config conf ( argc, argv );
  23. string destForFeat = conf.gS("main", "destForFeat", "/tmp/");
  24. double percentageOfPatchesForKMeans = conf.gD("main", "percentageOfPatchesForKMeans", 0.3);
  25. int nrOfClusters = conf.gI("main", "nrOfClusters", 200);
  26. bool verbose = conf.gB("main", "verbose", false);
  27. MultiDataset md ( &conf );
  28. const LabeledSet & train = *(md["train"]);
  29. const LabeledSet & test = *(md["test"]);
  30. const ClassNames & classNamesTrain = md.getClassNames("train");
  31. const ClassNames & classNamesTest = md.getClassNames("test");
  32. LFonHSG GridForSIFT(&conf);
  33. std::vector<VVector> featuresTrainset;
  34. LOOP_ALL_S ( train )
  35. {
  36. EACH_S(classno, imgfilename);
  37. if (verbose)
  38. std::cerr << "imgfilename: " << imgfilename << std::endl;
  39. Image img (imgfilename);
  40. NICE::VVector positions;
  41. NICE::VVector features;
  42. // Extract the descriptor-Values from a given grayscale-Image.
  43. GridForSIFT.extractFeatures ( img, features, positions );
  44. if (verbose)
  45. std::cerr << "features.size: " << features.size() << std::endl;
  46. featuresTrainset.push_back(features);
  47. }
  48. int trainsize(featuresTrainset.size());
  49. int nrOfFivePercent(round(0.05*trainsize));
  50. std::cerr << "Now sample randomly some patches (SIFT-features)" << std::endl;
  51. VVector x;
  52. x.clear();
  53. int count(0);
  54. //randomly sampling of patches -- take percentageOfPatchesForKMeans % of the patches from each image
  55. for (int i = 0; i < trainsize; i++, count++)
  56. {
  57. //small stat bar
  58. if ( (count % nrOfFivePercent) == 0)
  59. std::cerr << "sampling for image nr: " << count << " / " << trainsize << std::endl;
  60. std::set<int> indices;
  61. int currentSize(featuresTrainset[i].size());
  62. for (int k = 0; k < (int) round(percentageOfPatchesForKMeans*currentSize); k++)
  63. {
  64. int idx( round((float)rand() * (currentSize-1)/ (float)(RAND_MAX)) );
  65. while ( (indices.find(idx) != indices.end()) && (i > 0))
  66. {
  67. idx = ( round((float)rand() * (currentSize-1)/ (float)(RAND_MAX)) );
  68. }
  69. x.push_back(featuresTrainset[i][idx]);
  70. indices.insert(idx);
  71. }
  72. }
  73. std::cerr << "sampling done - final number of patches: " << x.size() << std::endl;
  74. std::cerr << "Apply k-Means on randomly sampled patches" << std::endl;
  75. VVector prototypes;
  76. std::vector<double> weights;
  77. std::vector<int> assignments;
  78. std::cerr << "nrOfClusters: " << nrOfClusters << std::endl;
  79. ClusterAlgorithm *clusteralg = new KMeans (nrOfClusters);
  80. clusteralg->cluster(x, prototypes, weights, assignments);
  81. delete clusteralg;
  82. std::cerr << "prototypes.size(): " << prototypes.size() << std::endl;
  83. CodebookPrototypes codebook ( prototypes );
  84. std::cerr << "Now apply our k-means codebook to every image for computing the resulting features" << std::endl;
  85. FCCodebookHistBin featureGenerator(&conf, &GridForSIFT, "sum", &codebook);
  86. std::cerr << "codebook.size(): " << codebook.size() << std::endl;
  87. int cntTrain(0);
  88. LOOP_ALL_S ( train )
  89. {
  90. //small stat bar
  91. if ( (cntTrain % nrOfFivePercent) == 0)
  92. std::cerr << "image nr: " << cntTrain << " / " << trainsize << std::endl;
  93. EACH_S(classno, imgfilename);
  94. std::string finalDest(classNamesTrain.text(classno) +imgfilename);
  95. size_t posOfEnding = finalDest.find_last_of(".");
  96. size_t lengthOfEnding = finalDest.size() - posOfEnding;
  97. finalDest = finalDest.erase(posOfEnding, lengthOfEnding)+".txt";
  98. size_t posOfclassSlash = finalDest.find_last_of("/");
  99. size_t posOfFolderSlash = finalDest.find_last_of("/", posOfclassSlash-1);
  100. finalDest = finalDest.erase(0, posOfFolderSlash+1);
  101. finalDest = destForFeat + finalDest;
  102. // std::cerr << "finalDest: " << finalDest<< std::endl;
  103. Image img (imgfilename);
  104. NICE::Vector vec;
  105. //compute the histogram
  106. featureGenerator.convert (img, vec );
  107. std::filebuf fb;
  108. fb.open (finalDest.c_str(),ios::out);
  109. ostream outstream(&fb);
  110. outstream << vec;
  111. fb.close();
  112. cntTrain++;
  113. }
  114. int testsize(0);
  115. LOOP_ALL_S ( test )
  116. {
  117. testsize++;
  118. }
  119. int nrOfFivePercentTest(round(0.05*testsize));
  120. int cntTest(0);
  121. LOOP_ALL_S ( test )
  122. {
  123. //small stat bar
  124. if ( (cntTest % nrOfFivePercentTest) == 0)
  125. std::cerr << "image nr: " << cntTest << " / " << testsize << std::endl;
  126. EACH_S(classno, imgfilename);
  127. std::string finalDest(classNamesTest.text(classno) +imgfilename);
  128. size_t posOfEnding = finalDest.find_last_of(".");
  129. size_t lengthOfEnding = finalDest.size() - posOfEnding;
  130. finalDest = finalDest.erase(posOfEnding, lengthOfEnding)+".txt";
  131. size_t posOfclassSlash = finalDest.find_last_of("/");
  132. size_t posOfFolderSlash = finalDest.find_last_of("/", posOfclassSlash-1);
  133. finalDest = finalDest.erase(0, posOfFolderSlash+1);
  134. finalDest = destForFeat + finalDest;
  135. // std::cerr << "finalDest: " << finalDest<< std::endl;
  136. Image img (imgfilename);
  137. NICE::Vector vec;
  138. //compute the histogram
  139. featureGenerator.convert (img, vec );
  140. std::filebuf fb;
  141. fb.open (finalDest.c_str(),ios::out);
  142. ostream outstream(&fb);
  143. outstream << vec;
  144. fb.close();
  145. cntTest++;
  146. }
  147. return 0;
  148. }