-
Notifications
You must be signed in to change notification settings - Fork 0
/
new 3
183 lines (164 loc) · 5.99 KB
/
new 3
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
public DistribPack(Image_Project img)
{
distance = ND;
norm = Brightness;
volume_limit = 0.005;
pack = new List<IStats>();
List<IColor> prevcenters = CentersLoad(img);
var tmplist = GetClustMask(img, prevcenters);
for (int i = 0; i < tmplist.Count; i++)
{
pack.Add(new StatsForDistrib(img, tmplist[i], norm));
}
pack= UniteSame(img, pack);
}
public IPack Create(Image_Project img, Parts parts)
{
return new DistribPack(img, parts);
}
public DistribPack(Image_Project img, Parts parts)
{
distance = DN;
norm = Evcklid;
volume_limit = 0.015;
var size = new Point(img.GetImage.Width, img.GetImage.Height);
pack = new List<IStats>();
int row = parts.Row;
int column = parts.Column;
IPack[,] tmppack = new IPack[row, column];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
tmppack[i, j] = new DistribPack().Create(parts[i, j]);
}
}
}
List<IStats> UniteSame(Image_Project img, List<IStats> clust)
{
var res = clust;
for (int i = 0; i < res.Count - 1; i++)
{
var s1 = res[i] as StatsForDistrib;
for (int j = i + 1; j < res.Count; j++)
{
var s2 = res[j] as StatsForDistrib;
// if(DispersionLimit(s1)&&DispersionLimit(s2))
if (Statistic_test(s1, s2))
{
res.Add(new StatsForDistrib().Create(img, UniteMaps(s1.GetMap, s2.GetMap), norm));
res.RemoveAt(j);
res.RemoveAt(i);
i=-1;
break;
}
}
}
return res;
}
List<Mask> GetClustMask(Image_Project img, List<IColor> center)
{
var empty = new List<bool>();
var clustvol = new List<int>();
var res = new List<Mask>();
int w = img.GetImage.Width;
int h = img.GetImage.Height;
int length = img.GetImage.GetLength();
int numb_clust = 0;
float dist = int.MaxValue;
for (int i = 0; i < center.Count; i++)
{
clustvol.Add(0);
empty.Add(true);
res.Add(new Mask(w,h,true));
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
Pixel tmp = img.GetImage[i,j];
numb_clust = -1;
dist = int.MaxValue;
for (int n = 0; n < center.Count; n++)
{
float tmpd = (float) distance(tmp.GetColor, center[n], norm);
if (tmpd < dist)
{
dist = tmpd;
numb_clust = n;
}
}
res[numb_clust].GetMask[i,j] = true;
clustvol[numb_clust]++;
empty[numb_clust] = false;
}
}
EmptyCleaner(empty, center, clustvol, res);
//SmallClear(res, center, clustvol, img.GetImage.GetLength());
return res;
}
void SmallClear(List<Mask> maps, List<IColor> center, List<int> clustv, int length)
{
for (int i = 0; i < maps.Count; i++)
{
if (( (double) clustv[i] / (double) length ) < volume_limit)
{
int tm = NearestCenter(i, center, DN);////////
Mask tmp = UniteMaps(maps[i], maps[tm]);
int tmpl = ( tm < i ) ? tm : i;
int tmpr = ( tm >= i ) ? tm : i;
maps.Add(tmp);
maps.RemoveAt(tmpr);
maps.RemoveAt(tmpl);
clustv.Add(clustv[tm] + clustv[i]);
clustv.RemoveAt(tmpr);
clustv.RemoveAt(tmpl);
center.Add(center[tm]);
center.RemoveAt(tmpr);
center.RemoveAt(tmpl);
i = -1;
}
}
}
void EmptyCleaner(List<bool> empty, List<IColor> center, List<int> clustv, List<Mask> maps)
{
for (int i = 0; i < empty.Count; i++)
{
if (empty[i])
{
maps.RemoveAt(i);
clustv.RemoveAt(i);
center.RemoveAt(i);
empty.RemoveAt(i);
i--;
}
}
}
List<IColor> CentersUpdate(Image_Project img,List<Mask> Mask)
{
var res = new List<IColor>();
for (int i = 0; i < Mask.Count; i++)
{
var tmp = new float[3];
tmp[0] = 0;
tmp[1] = 0;
tmp[2] = 0;
int l = 0;
for (int j = 0; j < img.GetImage.Height; j++)
for (int n = 0; n < img.GetImage.Width; n++)
{
if (Mask[i][j, n])
{
l++;
tmp[0] += img.GetImage[j, n].GetColor.GetVal1;
tmp[1] += img.GetImage[j, n].GetColor.GetVal2;
tmp[2] += img.GetImage[j, n].GetColor.GetVal3;
}
}
tmp[0] /= l;
tmp[1] /= l;
tmp[2] /= l;
res.Add(img.GetState.ColorState.Create(tmp));
}
return res;
}