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.
9 // This file originates from the openFABMAP project:
10 // [http://code.google.com/p/openfabmap/]
12 // For published work which uses all or part of OpenFABMAP, please cite:
13 // [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
15 // Original Algorithm by Mark Cummins and Paul Newman:
16 // [http://ijr.sagepub.com/content/27/6/647.short]
17 // [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942]
18 // [http://ijr.sagepub.com/content/30/9/1100.abstract]
22 // Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
23 // Will Maddern [w.maddern@qut.edu.au], all rights reserved.
26 // Redistribution and use in source and binary forms, with or without modification,
27 // are permitted provided that the following conditions are met:
29 // * Redistribution's of source code must retain the above copyright notice,
30 // this list of conditions and the following disclaimer.
32 // * Redistribution's in binary form must reproduce the above copyright notice,
33 // this list of conditions and the following disclaimer in the documentation
34 // and/or other materials provided with the distribution.
36 // * The name of the copyright holders may not be used to endorse or promote products
37 // derived from this software without specific prior written permission.
39 // This software is provided by the copyright holders and contributors "as is" and
40 // any express or implied warranties, including, but not limited to, the implied
41 // warranties of merchantability and fitness for a particular purpose are disclaimed.
42 // In no event shall the Intel Corporation or contributors be liable for any direct,
43 // indirect, incidental, special, exemplary, or consequential damages
44 // (including, but not limited to, procurement of substitute goods or services;
45 // loss of use, data, or profits; or business interruption) however caused
46 // and on any theory of liability, whether in contract, strict liability,
47 // or tort (including negligence or otherwise) arising in any way out of
48 // the use of this software, even if advised of the possibility of such damage.
52 #include "precomp.hpp"
53 #include "opencv2/contrib/openfabmap.hpp"
59 BOWMSCTrainer::BOWMSCTrainer(double _clusterSize) :
60 clusterSize(_clusterSize) {
63 BOWMSCTrainer::~BOWMSCTrainer() {
66 Mat BOWMSCTrainer::cluster() const {
67 CV_Assert(!descriptors.empty());
69 for(size_t i = 0; i < descriptors.size(); i++)
70 descCount += descriptors[i].rows;
72 Mat mergedDescriptors(descCount, descriptors[0].cols,
73 descriptors[0].type());
74 for(size_t i = 0, start = 0; i < descriptors.size(); i++)
76 Mat submut = mergedDescriptors.rowRange((int)start,
77 (int)(start + descriptors[i].rows));
78 descriptors[i].copyTo(submut);
79 start += descriptors[i].rows;
81 return cluster(mergedDescriptors);
84 Mat BOWMSCTrainer::cluster(const Mat& _descriptors) const {
86 CV_Assert(!_descriptors.empty());
88 // TODO: sort the descriptors before clustering.
91 Mat icovar = Mat::eye(_descriptors.cols,_descriptors.cols,_descriptors.type());
93 std::vector<Mat> initialCentres;
94 initialCentres.push_back(_descriptors.row(0));
95 for (int i = 1; i < _descriptors.rows; i++) {
96 double minDist = DBL_MAX;
97 for (size_t j = 0; j < initialCentres.size(); j++) {
98 minDist = std::min(minDist,
99 cv::Mahalanobis(_descriptors.row(i),initialCentres[j],
102 if (minDist > clusterSize)
103 initialCentres.push_back(_descriptors.row(i));
106 std::vector<std::list<cv::Mat> > clusters;
107 clusters.resize(initialCentres.size());
108 for (int i = 0; i < _descriptors.rows; i++) {
109 int index = 0; double dist = 0, minDist = DBL_MAX;
110 for (size_t j = 0; j < initialCentres.size(); j++) {
111 dist = cv::Mahalanobis(_descriptors.row(i),initialCentres[j],icovar);
112 if (dist < minDist) {
117 clusters[index].push_back(_descriptors.row(i));
120 // TODO: throw away small clusters.
123 Mat centre = Mat::zeros(1,_descriptors.cols,_descriptors.type());
124 for (size_t i = 0; i < clusters.size(); i++) {
126 for (std::list<cv::Mat>::iterator Ci = clusters[i].begin(); Ci != clusters[i].end(); Ci++) {
129 centre /= (double)clusters[i].size();
130 vocabulary.push_back(centre);