/
pfcm.cpp
202 lines (174 loc) · 5 KB
/
pfcm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "pfcm.h"
#include "math.h"
#include "fcm.h"
#include "QDebug"
PFCM::PFCM(int parClusterCount,
PixelRgb **parPixels,
int parWidth,
int parHeight,
double parDegreeMF,
double parDegreeTF,
int parMaxIterationCount,
double parPrecision)
:KMeans(parClusterCount,
parPixels,
parWidth,
parHeight,
parMaxIterationCount,
parPrecision),
FCM(parClusterCount,
parPixels,
parWidth,
parHeight,
parDegreeMF,
parMaxIterationCount,
parPrecision),
PCM(parClusterCount,
parPixels,
parWidth,
parHeight,
parDegreeTF,
parMaxIterationCount,
parPrecision)
{
}
void PFCM::Init()
{
//Инициализация массива номеров кластеров для каждого пикселя
_PixelLabels = new int*[_Width];
for (int i=0; i < _Width; i++)
{
_PixelLabels[i] = new int[_Height];
}
FCM fcm(_ClusterCount,
_Pixels,
_Width,
_Height,
_DegreeMF,
_MaxIterationCount,
_Precision);
if (_ClusterCenters == NULL)
{
fcm.FindClusterCenters();
_ClusterCenters = fcm.ClusterCenters();
}
else
{
fcm.SetClusterCenters(_ClusterCenters);
}
_BandWidth = new double[_ClusterCount];
for (int k=0; k<_ClusterCount; k++)
{
double numerator=0;
double denominator=0;
for (int i=0; i<_Width; i++)
{
for (int j=0; j<_Height; j++)
{
double mf = pow(fcm.MembershipFunction(k,i,j),_DegreeMF);
numerator+=mf*pow(fcm.Distance(k,i,j),2);
denominator+=mf;
}
}
_BandWidth[k]=sqrt(numerator/denominator);
}
}
/*double PFCM::MembershipFunction(int parClusterIndex, int parColumnIndex, int parRowIndex)
{
double sum = 0;
double distCurrentCluster = Distance(parClusterIndex, parColumnIndex, parRowIndex);
if (distCurrentCluster == 0)
{
return 1;
}
for (int i = 0; i < _ClusterCount; i++)
{
double distOtherCluster = Distance(i, parColumnIndex, parRowIndex);
if (distOtherCluster == 0)
{
return 0;
}
else
{
sum+=pow(distCurrentCluster
/distOtherCluster,
2.0/(_DegreeMF-1));
}
}
return 1.0/sum;
}*/
PixelRgb *PFCM::NewCenterPositions()
{
//Новые координаты центроида
PixelRgb* newCenterPositioins = new PixelRgb[_ClusterCount];
//Для каждого кластера
for (int k=0; k < _ClusterCount; k++)
{
double numeratorX=0;
double numeratorY=0;
double numeratorRed=0;
double numeratorGreen=0;
double numeratorBlue=0;
double denominator=0;
for (int i=0; i < _Width; i++)
{
for (int j=0; j < _Height; j++)
{
double sf = pow(MembershipFunction(k,i,j),_DegreeMF)+pow(TypicalityFunction(k,i,j),_DegreeTF);
numeratorX+=sf*_Pixels[i][j].X;
numeratorY+=sf*_Pixels[i][j].Y;
numeratorRed+=sf*_Pixels[i][j].Red;
numeratorGreen+=sf*_Pixels[i][j].Green;
numeratorBlue+=sf*_Pixels[i][j].Blue;
denominator+=sf;
}
}
newCenterPositioins[k].X = numeratorX/denominator;
newCenterPositioins[k].Y = numeratorY/denominator;
newCenterPositioins[k].Red = numeratorRed/denominator;
newCenterPositioins[k].Green = numeratorGreen/denominator;
newCenterPositioins[k].Blue = numeratorBlue/denominator;
}
return newCenterPositioins;
}
double PFCM::ObjectiveFunction()
{
double sum = 0;
for (int k=0; k<_ClusterCount; k++)
{
for (int i=0; i<_Width; i++)
{
for (int j=0; j<_Height; j++)
{
sum+=(pow(MembershipFunction(k,i,j),_DegreeMF)
+pow(TypicalityFunction(k,i,j), _DegreeTF))
*pow(Distance(k,i,j),2);
}
}
}
return sum;
}
void PFCM::PixelClustering()
{
for (int i=0; i<_Width; i++)
{
for (int j=0; j<_Height; j++)
{
double maxMF = MembershipFunction(0,i,j);
_PixelLabels[i][j] = 0;
for (int k=1; k<_ClusterCount; k++)
{
double currentMF = MembershipFunction(k,i,j);
if (currentMF > maxMF)
{
maxMF = currentMF;
_PixelLabels[i][j] = k;
}
}
}
}
}
/*double PFCM::TypicalityFunction(int parClusterIndex, int parColumnIndex, int parRowIndex)
{
return 1/(1+pow(Distance(parClusterIndex,parColumnIndex,parRowIndex)/_BandWidth[parClusterIndex],2/(_DegreeTF-1)));
}*/