LocalFeatureSiftGPU.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file LocalFeaturesiftGPU.cpp
  3. * @brief A Sift-Implementation, which uses the GPU.
  4. * @author Eric Bach
  5. * @date 11/20/2011
  6. */
  7. #include "LocalFeatureSiftGPU.h"
  8. #include "core/basics/StringTools.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. using namespace NICE;
  12. LocalFeatureSiftGPU::LocalFeatureSiftGPU (const Config* conf) : LocalFeatureSift (conf)
  13. {
  14. const string section = "SIFTGPU";
  15. gpuLanguage = conf->gS (section, "language", "cuda");
  16. multiGPU = conf->gB (section, "multigpu", false);
  17. numOfGPU = conf->gI (section, "numgpu" , 1);
  18. // for multiGPU its neccessary, that you got more the one devices
  19. /*if( multiGPU && ( numOfGPU < 2 ) )
  20. fthrow( Exception, "For multiple GPU, you need more the one device!");
  21. // fill deviceStat: all devices are unused (=free)
  22. for( unsigned int i=0; i < numOfGPU; i++ )
  23. deviceStat.insert(pair<unsigned int, bool>(i, true));*/
  24. }
  25. LocalFeatureSiftGPU::~LocalFeatureSiftGPU ()
  26. {
  27. }
  28. int LocalFeatureSiftGPU::getNextDeviceID()
  29. {
  30. StatTable::iterator it = deviceStat.begin();
  31. for (; it != deviceStat.end(); ++it) {
  32. bool free = it->second;
  33. if (free) {
  34. it->second = !free;
  35. return it->first;
  36. }
  37. }
  38. // no free device was found
  39. return -1;
  40. }
  41. void LocalFeatureSiftGPU::releaseDevice (int id)
  42. {
  43. StatTable::iterator it = deviceStat.find (id);
  44. // if given id is associated with a device, set this gpu on free
  45. if (it != deviceStat.end()) it->second = true;
  46. }
  47. void LocalFeatureSiftGPU::computeDesc (const NICE::Image & img, VVector & positions, VVector & descriptors) const
  48. {
  49. // fill the parameter for
  50. //char device[2] = {'0' + deviceID, '\0'};
  51. char* argv[] = {
  52. // First octave to detect DOG keypoints
  53. "-fo" , const_cast<char*> (StringTools::convertToString<int> (first_octave).c_str()),
  54. // Maximum number of Octaves
  55. "-no" , const_cast<char*> (StringTools::convertToString<int> (octaves).c_str()),
  56. // Number of DOG levels in an octave.
  57. "-d" , const_cast<char*> (StringTools::convertToString<int> (levels).c_str()),
  58. // Write unnormalized descriptor if specified.
  59. const_cast<char*> (normalizeFeature ? "" : "-unn"),
  60. // Descriptor grid size factor (magnif ??)
  61. "-dw" , const_cast<char*> (StringTools::convertToString<float> (magnif).c_str()),
  62. // use cuda / which device TODO
  63. const_cast<char*> (gpuLanguage == "cuda" ? "-cuda" : "-pack"), const_cast<char*> (gpuLanguage == "cuda" ? "0" : ""),
  64. // verbose levels
  65. "-v", "0"
  66. };
  67. int argc = sizeof (argv) / sizeof (char*);
  68. // sift Instanz
  69. SiftGPU sift;
  70. // give parameter to sift
  71. sift.ParseParam (argc, argv);
  72. // check, whether siftgpu is full supported
  73. const int support = sift.CreateContextGL();
  74. // const int support = sift.VerifyContextGL();
  75. /*if( support == SiftGPU::SIFTGPU_PARTIAL_SUPPORTED )
  76. clog << "·[log] LocalFeatureSift: SIFTGPU spartial supported" << endl;*/
  77. if ( support != SiftGPU::SIFTGPU_FULL_SUPPORTED) {
  78. fthrow( Exception, "LocalFeatureSift: Its not able to use SIFTGPU, because it is not supported." );
  79. }
  80. else{ clog << "[log] LocalFeatureSift: SIFTGPU full supported" << endl; }
  81. // set keypoints
  82. const int numberOfKeypoints = positions.size();
  83. SiftGPU::SiftKeypoint keys[numberOfKeypoints];
  84. // copy the NICEKeypoints into SiftKeypoints
  85. for (int i = 0; i < numberOfKeypoints; i++) {
  86. keys[i].x = positions[i][0];
  87. keys[i].y = positions[i][1];
  88. keys[i].s = positions[i][2];
  89. keys[i].o = positions[i][3];
  90. }
  91. sift.SetKeypointList (numberOfKeypoints, keys);
  92. // run SIFT
  93. const int imageWidth = img.width();
  94. const int imageHeight = img.height();
  95. const unsigned char* rawImageData = img.getPixelPointer();
  96. sift.RunSIFT (imageWidth, imageHeight, rawImageData, GL_LUMINANCE, GL_UNSIGNED_BYTE);
  97. // get descriptors
  98. const int descr_size = 128;
  99. const int numberOfDescriptors = sift.GetFeatureNum();
  100. float desc[descr_size * numberOfDescriptors];
  101. //vector<float> desc (descr_size * numberOfDescriptors);
  102. sift.GetFeatureVector (keys, desc);
  103. // TODO: kopieren durch Iterator erledigen
  104. Vector localDesc (descr_size);
  105. // copy the SiftDescriptors into NICEDescriptors
  106. for (int i = 0; i < numberOfDescriptors; i++) {
  107. for (int j = 0; j < descr_size; j++) {
  108. localDesc[j] = (integerValues ? (int) (512 * desc[i*descr_size+j]) : desc[i*descr_size+j]);
  109. }
  110. descriptors.push_back (localDesc);
  111. }
  112. }
  113. int LocalFeatureSiftGPU::getDescriptors (const NICE::Image & img, VVector & positions, VVector & descriptors) const
  114. {
  115. sortPositions (positions);
  116. computeDesc (img, positions, descriptors);
  117. return 0;
  118. }