Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / contrib / src / bowmsctrainer.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 // This file originates from the openFABMAP project:
10 // [http://code.google.com/p/openfabmap/]
11 //
12 // For published work which uses all or part of OpenFABMAP, please cite:
13 // [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
14 //
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]
19 //
20 //                           License Agreement
21 //
22 // Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
23 //                    Will Maddern [w.maddern@qut.edu.au], all rights reserved.
24 //
25 //
26 // Redistribution and use in source and binary forms, with or without modification,
27 // are permitted provided that the following conditions are met:
28 //
29 //   * Redistribution's of source code must retain the above copyright notice,
30 //     this list of conditions and the following disclaimer.
31 //
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.
35 //
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.
38 //
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.
49 //
50 //M*/
51
52 #include "precomp.hpp"
53 #include "opencv2/contrib/openfabmap.hpp"
54
55 namespace cv {
56
57 namespace of2 {
58
59 BOWMSCTrainer::BOWMSCTrainer(double _clusterSize) :
60     clusterSize(_clusterSize) {
61 }
62
63 BOWMSCTrainer::~BOWMSCTrainer() {
64 }
65
66 Mat BOWMSCTrainer::cluster() const {
67     CV_Assert(!descriptors.empty());
68     int descCount = 0;
69     for(size_t i = 0; i < descriptors.size(); i++)
70     descCount += descriptors[i].rows;
71
72     Mat mergedDescriptors(descCount, descriptors[0].cols,
73         descriptors[0].type());
74     for(size_t i = 0, start = 0; i < descriptors.size(); i++)
75     {
76         Mat submut = mergedDescriptors.rowRange((int)start,
77             (int)(start + descriptors[i].rows));
78         descriptors[i].copyTo(submut);
79         start += descriptors[i].rows;
80     }
81     return cluster(mergedDescriptors);
82 }
83
84 Mat BOWMSCTrainer::cluster(const Mat& _descriptors) const {
85
86     CV_Assert(!_descriptors.empty());
87
88     // TODO: sort the descriptors before clustering.
89
90
91     Mat icovar = Mat::eye(_descriptors.cols,_descriptors.cols,_descriptors.type());
92
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],
100                 icovar));
101         }
102         if (minDist > clusterSize)
103             initialCentres.push_back(_descriptors.row(i));
104     }
105
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) {
113                 minDist = dist;
114                 index = (int)j;
115             }
116         }
117         clusters[index].push_back(_descriptors.row(i));
118     }
119
120     // TODO: throw away small clusters.
121
122     Mat vocabulary;
123     Mat centre = Mat::zeros(1,_descriptors.cols,_descriptors.type());
124     for (size_t i = 0; i < clusters.size(); i++) {
125         centre.setTo(0);
126         for (std::list<cv::Mat>::iterator Ci = clusters[i].begin(); Ci != clusters[i].end(); Ci++) {
127             centre += *Ci;
128         }
129         centre /= (double)clusters[i].size();
130         vocabulary.push_back(centre);
131     }
132
133     return vocabulary;
134 }
135
136 }
137
138 }