1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
49 BOWTrainer::BOWTrainer()
52 BOWTrainer::~BOWTrainer()
55 void BOWTrainer::add( const Mat& _descriptors )
57 CV_Assert( !_descriptors.empty() );
58 if( !descriptors.empty() )
60 CV_Assert( descriptors[0].cols == _descriptors.cols );
61 CV_Assert( descriptors[0].type() == _descriptors.type() );
62 size += _descriptors.rows;
66 size = _descriptors.rows;
69 descriptors.push_back(_descriptors);
72 const vector<Mat>& BOWTrainer::getDescriptors() const
77 int BOWTrainer::descripotorsCount() const
79 return descriptors.empty() ? 0 : size;
82 void BOWTrainer::clear()
87 BOWKMeansTrainer::BOWKMeansTrainer( int _clusterCount, const TermCriteria& _termcrit,
88 int _attempts, int _flags ) :
89 clusterCount(_clusterCount), termcrit(_termcrit), attempts(_attempts), flags(_flags)
92 Mat BOWKMeansTrainer::cluster() const
94 CV_Assert( !descriptors.empty() );
97 for( size_t i = 0; i < descriptors.size(); i++ )
98 descCount += descriptors[i].rows;
100 Mat mergedDescriptors( descCount, descriptors[0].cols, descriptors[0].type() );
101 for( size_t i = 0, start = 0; i < descriptors.size(); i++ )
103 Mat submut = mergedDescriptors.rowRange((int)start, (int)(start + descriptors[i].rows));
104 descriptors[i].copyTo(submut);
105 start += descriptors[i].rows;
107 return cluster( mergedDescriptors );
110 BOWKMeansTrainer::~BOWKMeansTrainer()
113 Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const
115 Mat labels, vocabulary;
116 kmeans( _descriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
121 BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& _dextractor,
122 const Ptr<DescriptorMatcher>& _dmatcher ) :
123 dextractor(_dextractor), dmatcher(_dmatcher)
126 BOWImgDescriptorExtractor::~BOWImgDescriptorExtractor()
129 void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary )
132 vocabulary = _vocabulary;
133 dmatcher->add( vector<Mat>(1, vocabulary) );
136 const Mat& BOWImgDescriptorExtractor::getVocabulary() const
141 void BOWImgDescriptorExtractor::compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
142 vector<vector<int> >* pointIdxsOfClusters, Mat* _descriptors )
144 imgDescriptor.release();
146 if( keypoints.empty() )
149 int clusterCount = descriptorSize(); // = vocabulary.rows
151 // Compute descriptors for the image.
152 Mat descriptors = _descriptors ? *_descriptors : Mat();
153 dextractor->compute( image, keypoints, descriptors );
155 // Match keypoint descriptors to cluster center (to vocabulary)
156 vector<DMatch> matches;
157 dmatcher->match( descriptors, matches );
159 // Compute image descriptor
160 if( pointIdxsOfClusters )
162 pointIdxsOfClusters->clear();
163 pointIdxsOfClusters->resize(clusterCount);
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++ )
170 int queryIdx = matches[i].queryIdx;
171 int trainIdx = matches[i].trainIdx; // cluster index
172 CV_Assert( queryIdx == (int)i );
174 dptr[trainIdx] = dptr[trainIdx] + 1.f;
175 if( pointIdxsOfClusters )
176 (*pointIdxsOfClusters)[trainIdx].push_back( queryIdx );
179 // Normalize image descriptor.
180 imgDescriptor /= descriptors.rows;
183 int BOWImgDescriptorExtractor::descriptorSize() const
185 return vocabulary.empty() ? 0 : vocabulary.rows;
188 int BOWImgDescriptorExtractor::descriptorType() const