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