/
Java Sourcecode k-means
233 lines (206 loc) · 8.32 KB
/
Java Sourcecode k-means
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.gloomyfish.segmentation.kmeans;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.gloomyfish.filter.study.AbstractBufferedImageOp;
import com.gloomyfish.segmentation.fuzzycmeans.ClusterPoint;
public class KMeansProcessor extends AbstractBufferedImageOp {
private List<ClusterCenter> clusterCenterList;
private List<ClusterPoint> pointList;
private int numOfCluster;
public KMeansProcessor(int clusters)
{
this.numOfCluster = clusters;
pointList = new ArrayList<ClusterPoint>();
this.clusterCenterList = new ArrayList<ClusterCenter>();
}
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dest) {
// initialization the pixel data
int width = src.getWidth();
int height = src.getHeight();
int[] inPixels = new int[width*height];
src.getRGB( 0, 0, width, height, inPixels, 0, width );
int index = 0;
//Create random points to use a the cluster center
Random random = new Random();
for (int i = 0; i < numOfCluster; i++)
{
int randomNumber1 = random.nextInt(width);
int randomNumber2 = random.nextInt(height);
index = randomNumber2 * width + randomNumber1;
ClusterCenter cc = new ClusterCenter(randomNumber1, randomNumber2, inPixels[index]);
cc.setcIndex(i);
clusterCenterList.add(cc);
}
// create all cluster point
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
index = row * width + col;
int color = inPixels[index];
pointList.add(new ClusterPoint(row, col, color));
}
}
// initialize the clusters for each point
double[] clusterDisValues = new double[clusterCenterList.size()];
for(int i=0; i<pointList.size(); i++)
{
for(int j=0; j<clusterCenterList.size(); j++)
{
clusterDisValues[j] = calculateEuclideanDistance(pointList.get(i), clusterCenterList.get(j));
}
pointList.get(i).setClusterIndex(getCloserCluster(clusterDisValues));
}
// calculate the old summary
// assign the points to cluster center
// calculate the new cluster center
// computation the delta value
// stop condition--
double[] oldClusterCenterColors = reCalculateClusterCenters();
while(true)
{
stepClusters();
double[] newClusterCenterColors = reCalculateClusterCenters();
if(isStop(oldClusterCenterColors, newClusterCenterColors))
{
break;
}
else
{
oldClusterCenterColors = newClusterCenterColors;
}
}
//update the result image
dest = createCompatibleDestImage(src, null );
index = 0;
int[] outPixels = new int[width*height];
for (int j = 0; j < pointList.size(); j++)
{
for (int i = 0; i < clusterCenterList.size(); i++)
{
ClusterPoint p = this.pointList.get(j);
if (clusterCenterList.get(i).getcIndex() == p.getClusterIndex())
{
int row = (int)p.getX(); // row
int col = (int)p.getY(); // column
index = row * width + col;
outPixels[index] = clusterCenterList.get(i).getPixelColor();
}
}
}
// fill the pixel data
setRGB( dest, 0, 0, width, height, outPixels );
return dest;
}
private boolean isStop(double[] oldClusterCenterColors, double[] newClusterCenterColors) {
for(int i=0; i<oldClusterCenterColors.length; i++)
{
System.out.println("cluster " + i + " old : " + oldClusterCenterColors[i] + ", new : " + newClusterCenterColors[i]);
if(oldClusterCenterColors[i] != newClusterCenterColors[i])
{
return false;
}
}
System.out.println();
return true;
}
/**
* update the cluster index by distance value
*/
private void stepClusters()
{
// initialize the clusters for each point
double[] clusterDisValues = new double[clusterCenterList.size()];
for(int i=0; i<pointList.size(); i++)
{
for(int j=0; j<clusterCenterList.size(); j++)
{
clusterDisValues[j] = calculateEuclideanDistance(pointList.get(i), clusterCenterList.get(j));
}
pointList.get(i).setClusterIndex(getCloserCluster(clusterDisValues));
}
}
/**
* using cluster color of each point to update cluster center color
*
* @return
*/
private double[] reCalculateClusterCenters() {
// clear the points now
for(int i=0; i<clusterCenterList.size(); i++)
{
clusterCenterList.get(i).setNumOfPoints(0);
}
// recalculate the sum and total of points for each cluster
double[] redSums = new double[3];
double[] greenSum = new double[3];
double[] blueSum = new double[3];
for(int i=0; i<pointList.size(); i++)
{
int cIndex = (int)pointList.get(i).getClusterIndex();
clusterCenterList.get(cIndex).addPoints();
int ta = (pointList.get(i).getPixelColor() >> 24) & 0xff;
int tr = (pointList.get(i).getPixelColor() >> 16) & 0xff;
int tg = (pointList.get(i).getPixelColor() >> 8) & 0xff;
int tb = pointList.get(i).getPixelColor() & 0xff;
ta = 255;
redSums[cIndex] += tr;
greenSum[cIndex] += tg;
blueSum[cIndex] += tb;
}
double[] oldClusterCentersColors = new double[clusterCenterList.size()];
for(int i=0; i<clusterCenterList.size(); i++)
{
double sum = clusterCenterList.get(i).getNumOfPoints();
int cIndex = clusterCenterList.get(i).getcIndex();
int red = (int)(greenSum[cIndex]/sum);
int green = (int)(greenSum[cIndex]/sum);
int blue = (int)(blueSum[cIndex]/sum);
System.out.println("red = " + red + " green = " + green + " blue = " + blue);
int clusterColor = (255 << 24) | (red << 16) | (green << 8) | blue;
clusterCenterList.get(i).setPixelColor(clusterColor);
oldClusterCentersColors[i] = clusterColor;
}
return oldClusterCentersColors;
}
/**
*
* @param clusterDisValues
* @return
*/
private double getCloserCluster(double[] clusterDisValues)
{
double min = clusterDisValues[0];
int clusterIndex = 0;
for(int i=0; i<clusterDisValues.length; i++)
{
if(min > clusterDisValues[i])
{
min = clusterDisValues[i];
clusterIndex = i;
}
}
return clusterIndex;
}
/**
*
* @param point
* @param cluster
* @return distance value
*/
private double calculateEuclideanDistance(ClusterPoint p, ClusterCenter c)
{
// int pa = (p.getPixelColor() >> 24) & 0xff;
int pr = (p.getPixelColor() >> 16) & 0xff;
int pg = (p.getPixelColor() >> 8) & 0xff;
int pb = p.getPixelColor() & 0xff;
// int ca = (c.getPixelColor() >> 24) & 0xff;
int cr = (c.getPixelColor() >> 16) & 0xff;
int cg = (c.getPixelColor() >> 8) & 0xff;
int cb = c.getPixelColor() & 0xff;
return Math.sqrt(Math.pow((pr - cr), 2.0) + Math.pow((pg - cg), 2.0) + Math.pow((pb - cb), 2.0));
}
}