Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/features2d/src/bagofwords.cpp
16337 views
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
47
BOWTrainer::BOWTrainer() : size(0)
48
{}
49
50
BOWTrainer::~BOWTrainer()
51
{}
52
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
70
const std::vector<Mat>& BOWTrainer::getDescriptors() const
71
{
72
return descriptors;
73
}
74
75
int BOWTrainer::descriptorsCount() const
76
{
77
return descriptors.empty() ? 0 : size;
78
}
79
80
void BOWTrainer::clear()
81
{
82
descriptors.clear();
83
}
84
85
BOWKMeansTrainer::BOWKMeansTrainer( int _clusterCount, const TermCriteria& _termcrit,
86
int _attempts, int _flags ) :
87
clusterCount(_clusterCount), termcrit(_termcrit), attempts(_attempts), flags(_flags)
88
{}
89
90
Mat BOWKMeansTrainer::cluster() const
91
{
92
CV_INSTRUMENT_REGION();
93
94
CV_Assert( !descriptors.empty() );
95
96
Mat mergedDescriptors( descriptorsCount(), descriptors[0].cols, descriptors[0].type() );
97
for( size_t i = 0, start = 0; i < descriptors.size(); i++ )
98
{
99
Mat submut = mergedDescriptors.rowRange((int)start, (int)(start + descriptors[i].rows));
100
descriptors[i].copyTo(submut);
101
start += descriptors[i].rows;
102
}
103
return cluster( mergedDescriptors );
104
}
105
106
BOWKMeansTrainer::~BOWKMeansTrainer()
107
{}
108
109
Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const
110
{
111
CV_INSTRUMENT_REGION();
112
113
Mat labels, vocabulary;
114
kmeans( _descriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
115
return vocabulary;
116
}
117
118
119
BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& _dextractor,
120
const Ptr<DescriptorMatcher>& _dmatcher ) :
121
dextractor(_dextractor), dmatcher(_dmatcher)
122
{}
123
124
BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& _dmatcher ) :
125
dmatcher(_dmatcher)
126
{}
127
128
BOWImgDescriptorExtractor::~BOWImgDescriptorExtractor()
129
{}
130
131
void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary )
132
{
133
dmatcher->clear();
134
vocabulary = _vocabulary;
135
dmatcher->add( std::vector<Mat>(1, vocabulary) );
136
}
137
138
const Mat& BOWImgDescriptorExtractor::getVocabulary() const
139
{
140
return vocabulary;
141
}
142
143
void BOWImgDescriptorExtractor::compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
144
std::vector<std::vector<int> >* pointIdxsOfClusters, Mat* descriptors )
145
{
146
CV_INSTRUMENT_REGION();
147
148
imgDescriptor.release();
149
150
if( keypoints.empty() )
151
return;
152
153
// Compute descriptors for the image.
154
Mat _descriptors;
155
dextractor->compute( image, keypoints, _descriptors );
156
157
compute( _descriptors, imgDescriptor, pointIdxsOfClusters );
158
159
// Add the descriptors of image keypoints
160
if (descriptors) {
161
*descriptors = _descriptors.clone();
162
}
163
}
164
165
int BOWImgDescriptorExtractor::descriptorSize() const
166
{
167
return vocabulary.empty() ? 0 : vocabulary.rows;
168
}
169
170
int BOWImgDescriptorExtractor::descriptorType() const
171
{
172
return CV_32FC1;
173
}
174
175
void BOWImgDescriptorExtractor::compute( InputArray keypointDescriptors, OutputArray _imgDescriptor, std::vector<std::vector<int> >* pointIdxsOfClusters )
176
{
177
CV_INSTRUMENT_REGION();
178
179
CV_Assert( !vocabulary.empty() );
180
CV_Assert(!keypointDescriptors.empty());
181
182
int clusterCount = descriptorSize(); // = vocabulary.rows
183
184
// Match keypoint descriptors to cluster center (to vocabulary)
185
std::vector<DMatch> matches;
186
dmatcher->match( keypointDescriptors, matches );
187
188
// Compute image descriptor
189
if( pointIdxsOfClusters )
190
{
191
pointIdxsOfClusters->clear();
192
pointIdxsOfClusters->resize(clusterCount);
193
}
194
195
_imgDescriptor.create(1, clusterCount, descriptorType());
196
_imgDescriptor.setTo(Scalar::all(0));
197
198
Mat imgDescriptor = _imgDescriptor.getMat();
199
200
float *dptr = imgDescriptor.ptr<float>();
201
for( size_t i = 0; i < matches.size(); i++ )
202
{
203
int queryIdx = matches[i].queryIdx;
204
int trainIdx = matches[i].trainIdx; // cluster index
205
CV_Assert( queryIdx == (int)i );
206
207
dptr[trainIdx] = dptr[trainIdx] + 1.f;
208
if( pointIdxsOfClusters )
209
(*pointIdxsOfClusters)[trainIdx].push_back( queryIdx );
210
}
211
212
// Normalize image descriptor.
213
imgDescriptor /= keypointDescriptors.size().height;
214
}
215
216
}
217
218