1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 
5 /*
6  * MIT License
7  *
8  * Copyright (c) 2018 Pedro Diamel Marrero Fernández
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in all
18  * copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  */
28 
29 #include "precomp.hpp"
30 #include "graph_cluster.hpp"
31 
32 namespace cv
33 {
34 namespace mcc
35 {
CB0cluster()36 CB0cluster::CB0cluster()
37 {
38 }
39 
~CB0cluster()40 CB0cluster::~CB0cluster()
41 {
42 }
43 
44 void CB0cluster::
group()45     group()
46 {
47 
48     size_t n = X.size();
49     G.clear();
50     G.resize(n);
51 
52     for (int i = 0; i < (int)n - 1; i++)
53     {
54         std::vector<double> Y;
55         Y.clear();
56         Y.resize(n - i);
57         Y[0] = 0;
58 
59         // 1. group similar blobs
60         double dist, w, y;
61         for (int j = i + 1, k = 1; j < (int)n; j++, k++)
62         {
63             //dist(X_i,X_j)
64             dist = norm(X[i] - X[j]);
65 
66             //heuristic to combine two sub charts, This is pretty ugly at the moment,
67             //Looking for somthing better
68             w = min(abs(W[i] - W[j]) / (W[i] + W[j]),
69                     abs(max(W[i], W[j]) - 24 / 11.0f * min(W[i], W[j])) / (max(W[i], W[j]) + 24 / 11.0f * min(W[i], W[j])));
70             w = (w < 0.1);
71 
72             y = w * dist;
73             Y[k] = (y < B0[i]) * y;
74             ;
75         }
76 
77         if (!G[i])
78             G[i] = i + 1;
79 
80         std::vector<int> pos_b0;
81         find(Y, pos_b0);
82 
83         size_t m = pos_b0.size();
84         if (!m)
85             continue;
86 
87         std::vector<int> pos_nz, pos_z;
88         for (int j = 0; j < (int)m; j++)
89         {
90             pos_b0[j] = pos_b0[j] + i;
91             if (G[pos_b0[j]])
92                 pos_nz.push_back(j);
93             else
94                 pos_z.push_back(j);
95         }
96 
97         for (int j = 0; j < (int)pos_z.size(); j++)
98         {
99             pos_z[j] = pos_b0[pos_z[j]];
100             G[pos_z[j]] = G[i];
101         }
102 
103         if (!pos_nz.size())
104             continue;
105 
106         std::vector<int> g;
107         for (size_t j = 0; j < pos_nz.size(); j++)
108         {
109             pos_nz[j] = pos_b0[pos_nz[j]];
110             g.push_back(G[pos_nz[j]]);
111         }
112 
113         unique(g, g);
114         for (size_t k = 0; k < g.size(); k++)
115         {
116             int gk = g[k];
117             for (size_t j = 0; j < G.size(); j++)
118                 if (G[j] == gk)
119                     G[j] = G[i];
120         }
121     }
122 
123     if (!G[n - 1])
124         G[n - 1] = (int)n;
125 
126     std::vector<int> S;
127     S = G;
128     unique(S, S);
129     for (int k = 0; k < (int)S.size(); k++)
130     {
131         int gk = S[k];
132         for (int j = 0; j < (int)G.size(); j++)
133             if (G[j] == gk)
134                 G[j] = k;
135     }
136 }
137 
138 } // namespace mcc
139 } // namespace cv
140