bagofwords.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "precomp.hpp"
  42. namespace cv
  43. {
  44. BOWTrainer::BOWTrainer() : size(0)
  45. {}
  46. BOWTrainer::~BOWTrainer()
  47. {}
  48. void BOWTrainer::add( const Mat& _descriptors )
  49. {
  50. CV_Assert( !_descriptors.empty() );
  51. if( !descriptors.empty() )
  52. {
  53. CV_Assert( descriptors[0].cols == _descriptors.cols );
  54. CV_Assert( descriptors[0].type() == _descriptors.type() );
  55. size += _descriptors.rows;
  56. }
  57. else
  58. {
  59. size = _descriptors.rows;
  60. }
  61. descriptors.push_back(_descriptors);
  62. }
  63. const std::vector<Mat>& BOWTrainer::getDescriptors() const
  64. {
  65. return descriptors;
  66. }
  67. int BOWTrainer::descriptorsCount() const
  68. {
  69. return descriptors.empty() ? 0 : size;
  70. }
  71. void BOWTrainer::clear()
  72. {
  73. descriptors.clear();
  74. }
  75. BOWKMeansTrainer::BOWKMeansTrainer( int _clusterCount, const TermCriteria& _termcrit,
  76. int _attempts, int _flags ) :
  77. clusterCount(_clusterCount), termcrit(_termcrit), attempts(_attempts), flags(_flags)
  78. {}
  79. Mat BOWKMeansTrainer::cluster() const
  80. {
  81. CV_INSTRUMENT_REGION();
  82. CV_Assert( !descriptors.empty() );
  83. Mat mergedDescriptors( descriptorsCount(), descriptors[0].cols, descriptors[0].type() );
  84. for( size_t i = 0, start = 0; i < descriptors.size(); i++ )
  85. {
  86. Mat submut = mergedDescriptors.rowRange((int)start, (int)(start + descriptors[i].rows));
  87. descriptors[i].copyTo(submut);
  88. start += descriptors[i].rows;
  89. }
  90. return cluster( mergedDescriptors );
  91. }
  92. BOWKMeansTrainer::~BOWKMeansTrainer()
  93. {}
  94. Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const
  95. {
  96. CV_INSTRUMENT_REGION();
  97. Mat labels, vocabulary;
  98. kmeans( _descriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
  99. return vocabulary;
  100. }
  101. BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& _dextractor,
  102. const Ptr<DescriptorMatcher>& _dmatcher ) :
  103. dextractor(_dextractor), dmatcher(_dmatcher)
  104. {}
  105. BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& _dmatcher ) :
  106. dmatcher(_dmatcher)
  107. {}
  108. BOWImgDescriptorExtractor::~BOWImgDescriptorExtractor()
  109. {}
  110. void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary )
  111. {
  112. dmatcher->clear();
  113. vocabulary = _vocabulary;
  114. dmatcher->add( std::vector<Mat>(1, vocabulary) );
  115. }
  116. const Mat& BOWImgDescriptorExtractor::getVocabulary() const
  117. {
  118. return vocabulary;
  119. }
  120. void BOWImgDescriptorExtractor::compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
  121. std::vector<std::vector<int> >* pointIdxsOfClusters, Mat* descriptors )
  122. {
  123. CV_INSTRUMENT_REGION();
  124. imgDescriptor.release();
  125. if( keypoints.empty() )
  126. return;
  127. // Compute descriptors for the image.
  128. Mat _descriptors;
  129. dextractor->compute( image, keypoints, _descriptors );
  130. compute( _descriptors, imgDescriptor, pointIdxsOfClusters );
  131. // Add the descriptors of image keypoints
  132. if (descriptors) {
  133. *descriptors = _descriptors.clone();
  134. }
  135. }
  136. int BOWImgDescriptorExtractor::descriptorSize() const
  137. {
  138. return vocabulary.empty() ? 0 : vocabulary.rows;
  139. }
  140. int BOWImgDescriptorExtractor::descriptorType() const
  141. {
  142. return CV_32FC1;
  143. }
  144. void BOWImgDescriptorExtractor::compute( InputArray keypointDescriptors, OutputArray _imgDescriptor, std::vector<std::vector<int> >* pointIdxsOfClusters )
  145. {
  146. CV_INSTRUMENT_REGION();
  147. CV_Assert( !vocabulary.empty() );
  148. CV_Assert(!keypointDescriptors.empty());
  149. int clusterCount = descriptorSize(); // = vocabulary.rows
  150. // Match keypoint descriptors to cluster center (to vocabulary)
  151. std::vector<DMatch> matches;
  152. dmatcher->match( keypointDescriptors, matches );
  153. // Compute image descriptor
  154. if( pointIdxsOfClusters )
  155. {
  156. pointIdxsOfClusters->clear();
  157. pointIdxsOfClusters->resize(clusterCount);
  158. }
  159. _imgDescriptor.create(1, clusterCount, descriptorType());
  160. _imgDescriptor.setTo(Scalar::all(0));
  161. Mat imgDescriptor = _imgDescriptor.getMat();
  162. float *dptr = imgDescriptor.ptr<float>();
  163. for( size_t i = 0; i < matches.size(); i++ )
  164. {
  165. int queryIdx = matches[i].queryIdx;
  166. int trainIdx = matches[i].trainIdx; // cluster index
  167. CV_Assert( queryIdx == (int)i );
  168. dptr[trainIdx] = dptr[trainIdx] + 1.f;
  169. if( pointIdxsOfClusters )
  170. (*pointIdxsOfClusters)[trainIdx].push_back( queryIdx );
  171. }
  172. // Normalize image descriptor.
  173. imgDescriptor /= keypointDescriptors.size().height;
  174. }
  175. }