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 
42 #include "precomp.hpp"
43 
44 namespace cv
45 {
46 
BOWTrainer()47 BOWTrainer::BOWTrainer() : size(0)
48 {}
49 
~BOWTrainer()50 BOWTrainer::~BOWTrainer()
51 {}
52 
add(const Mat & _descriptors)53 void BOWTrainer::add( const Mat& _descriptors )
54 {
55     CV_Assert( !_descriptors.empty() );
56     if( !descriptors.empty() )
57     {
58         CV_Assert( descriptors[0].cols == _descriptors.cols );
59         CV_Assert( descriptors[0].type() == _descriptors.type() );
60         size += _descriptors.rows;
61     }
62     else
63     {
64         size = _descriptors.rows;
65     }
66 
67     descriptors.push_back(_descriptors);
68 }
69 
getDescriptors() const70 const std::vector<Mat>& BOWTrainer::getDescriptors() const
71 {
72     return descriptors;
73 }
74 
descriptorsCount() const75 int BOWTrainer::descriptorsCount() const
76 {
77     return descriptors.empty() ? 0 : size;
78 }
79 
clear()80 void BOWTrainer::clear()
81 {
82     descriptors.clear();
83 }
84 
BOWKMeansTrainer(int _clusterCount,const TermCriteria & _termcrit,int _attempts,int _flags)85 BOWKMeansTrainer::BOWKMeansTrainer( int _clusterCount, const TermCriteria& _termcrit,
86                                     int _attempts, int _flags ) :
87     clusterCount(_clusterCount), termcrit(_termcrit), attempts(_attempts), flags(_flags)
88 {}
89 
cluster() const90 Mat BOWKMeansTrainer::cluster() const
91 {
92     CV_Assert( !descriptors.empty() );
93 
94     int descCount = 0;
95     for( size_t i = 0; i < descriptors.size(); i++ )
96         descCount += descriptors[i].rows;
97 
98     Mat mergedDescriptors( descCount, descriptors[0].cols, descriptors[0].type() );
99     for( size_t i = 0, start = 0; i < descriptors.size(); i++ )
100     {
101         Mat submut = mergedDescriptors.rowRange((int)start, (int)(start + descriptors[i].rows));
102         descriptors[i].copyTo(submut);
103         start += descriptors[i].rows;
104     }
105     return cluster( mergedDescriptors );
106 }
107 
~BOWKMeansTrainer()108 BOWKMeansTrainer::~BOWKMeansTrainer()
109 {}
110 
cluster(const Mat & _descriptors) const111 Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const
112 {
113     Mat labels, vocabulary;
114     kmeans( _descriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
115     return vocabulary;
116 }
117 
118 
BOWImgDescriptorExtractor(const Ptr<DescriptorExtractor> & _dextractor,const Ptr<DescriptorMatcher> & _dmatcher)119 BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& _dextractor,
120                                                       const Ptr<DescriptorMatcher>& _dmatcher ) :
121     dextractor(_dextractor), dmatcher(_dmatcher)
122 {}
123 
BOWImgDescriptorExtractor(const Ptr<DescriptorMatcher> & _dmatcher)124 BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& _dmatcher ) :
125     dmatcher(_dmatcher)
126 {}
127 
~BOWImgDescriptorExtractor()128 BOWImgDescriptorExtractor::~BOWImgDescriptorExtractor()
129 {}
130 
setVocabulary(const Mat & _vocabulary)131 void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary )
132 {
133     dmatcher->clear();
134     vocabulary = _vocabulary;
135     dmatcher->add( std::vector<Mat>(1, vocabulary) );
136 }
137 
getVocabulary() const138 const Mat& BOWImgDescriptorExtractor::getVocabulary() const
139 {
140     return vocabulary;
141 }
142 
compute(InputArray image,std::vector<KeyPoint> & keypoints,OutputArray imgDescriptor,std::vector<std::vector<int>> * pointIdxsOfClusters,Mat * descriptors)143 void BOWImgDescriptorExtractor::compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
144                                          std::vector<std::vector<int> >* pointIdxsOfClusters, Mat* descriptors )
145 {
146     imgDescriptor.release();
147 
148     if( keypoints.empty() )
149         return;
150 
151     // Compute descriptors for the image.
152     Mat _descriptors;
153     dextractor->compute( image, keypoints, _descriptors );
154 
155     compute( _descriptors, imgDescriptor, pointIdxsOfClusters );
156 
157     // Add the descriptors of image keypoints
158     if (descriptors) {
159         *descriptors = _descriptors.clone();
160     }
161 }
162 
descriptorSize() const163 int BOWImgDescriptorExtractor::descriptorSize() const
164 {
165     return vocabulary.empty() ? 0 : vocabulary.rows;
166 }
167 
descriptorType() const168 int BOWImgDescriptorExtractor::descriptorType() const
169 {
170     return CV_32FC1;
171 }
172 
compute(InputArray keypointDescriptors,OutputArray _imgDescriptor,std::vector<std::vector<int>> * pointIdxsOfClusters)173 void BOWImgDescriptorExtractor::compute( InputArray keypointDescriptors, OutputArray _imgDescriptor, std::vector<std::vector<int> >* pointIdxsOfClusters )
174 {
175     CV_Assert( !vocabulary.empty() );
176 
177     int clusterCount = descriptorSize(); // = vocabulary.rows
178 
179     // Match keypoint descriptors to cluster center (to vocabulary)
180     std::vector<DMatch> matches;
181     dmatcher->match( keypointDescriptors, matches );
182 
183     // Compute image descriptor
184     if( pointIdxsOfClusters )
185     {
186         pointIdxsOfClusters->clear();
187         pointIdxsOfClusters->resize(clusterCount);
188     }
189 
190     _imgDescriptor.create(1, clusterCount, descriptorType());
191     _imgDescriptor.setTo(Scalar::all(0));
192 
193     Mat imgDescriptor = _imgDescriptor.getMat();
194 
195     float *dptr = imgDescriptor.ptr<float>();
196     for( size_t i = 0; i < matches.size(); i++ )
197     {
198         int queryIdx = matches[i].queryIdx;
199         int trainIdx = matches[i].trainIdx; // cluster index
200         CV_Assert( queryIdx == (int)i );
201 
202         dptr[trainIdx] = dptr[trainIdx] + 1.f;
203         if( pointIdxsOfClusters )
204             (*pointIdxsOfClusters)[trainIdx].push_back( queryIdx );
205     }
206 
207     // Normalize image descriptor.
208     imgDescriptor /= keypointDescriptors.size().height;
209 }
210 
211 }
212