-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMeans.java
More file actions
163 lines (150 loc) · 4.85 KB
/
Copy pathKMeans.java
File metadata and controls
163 lines (150 loc) · 4.85 KB
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
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.HashMap;
import java.lang.Math;
public class KMeans {
static int maximumIterations = 100;
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: Kmeans <input-image> <k> <output-image>");
return;
}
try {
BufferedImage originalImage = ImageIO.read(new File(args[0]));
int k = Integer.parseInt(args[1]);
BufferedImage kmeansJpg = kmeans_helper(originalImage, k);
ImageIO.write(kmeansJpg, "jpg", new File(args[2]));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private static BufferedImage kmeans_helper(BufferedImage originalImage, int k) {
int w = originalImage.getWidth();
int h = originalImage.getHeight();
BufferedImage kmeansImage = new BufferedImage(w, h, originalImage.getType());
Graphics2D g = kmeansImage.createGraphics();
g.drawImage(originalImage, 0, 0, w, h, null);
// Read rgb values from the image
int[] rgb = new int[w * h];
int count = 0;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
rgb[count++] = kmeansImage.getRGB(i, j);
}
}
// Call kmeans algorithm: update the rgb values
kmeans(rgb, k);
// Write the new rgb values to the image
count = 0;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
kmeansImage.setRGB(i, j, rgb[count++]);
}
}
return kmeansImage;
}
// Your k-means code goes here
// Update the array rgb by assigning each entry in the rgb array to its cluster
// center
private static void kmeans(int[] rgb, int k) {
int[][] centroids = getInitialCentroids(k, rgb);
HashMap<Integer, ArrayList<Integer>> clusterMap = new HashMap<Integer, ArrayList<Integer>>();
for (int i = 0; i < centroids.length; i++) {
clusterMap.put(i, new ArrayList<>());
}
int iterations = 0;
while (true) {
for (int i = 0; i < rgb.length; i++) {
int[] rgbValue = getRGBValue(rgb[i]);
int minDistance = calculateEuclideanDistance(rgbValue, centroids[0]);
int clusterIndex = 0;
for (int j = 1; j < centroids.length; j++) {
int distance = calculateEuclideanDistance(rgbValue, centroids[j]);
if (distance < minDistance) {
minDistance = distance;
clusterIndex = j;
}
}
clusterMap.get(clusterIndex).add(Integer.valueOf(i));
}
iterations++;
if (iterations == maximumIterations) {
break;
}
centroids = calculateNewCentroids(rgb, centroids, clusterMap);
}
updatePixelValues(rgb, centroids, clusterMap);
// System.out.println(Arrays.toString(getRGBValue(rgb[0])));
}
private static void updatePixelValues(int[] rgb, int[][] centroids,
HashMap<Integer, ArrayList<Integer>> clusterMap) {
for (Integer key : clusterMap.keySet()) {
int centroidColor = getColorBit(centroids[key]);
for (Integer index : clusterMap.get(key)) {
rgb[index] = centroidColor;
}
}
}
private static int[][] getInitialCentroids(int k, int[] pixels) {
int[][] centroids = new int[k][3];
// int seed = 350;
for (int i = 0; i < k; i++) {
Random r = new Random();
int randomlySelectedPixel = r.nextInt((pixels.length - 1) + 0) + 0; // 0 => minimum value
System.out.println(randomlySelectedPixel);
int[] rgbValue = getRGBValue(pixels[randomlySelectedPixel]);
for (int j = 0; j < rgbValue.length; j++) {
centroids[i][j] = rgbValue[j];
}
System.out.println(Arrays.toString(rgbValue));
// seed += 15;
}
return centroids;
}
private static int[][] calculateNewCentroids(int[] rgb, int[][] centroids,
HashMap<Integer, ArrayList<Integer>> clusterMap) {
int[][] newCentroids = new int[centroids.length][3];
for (Integer key : clusterMap.keySet()) {
int red = 0, green = 0, blue = 0;
for (Integer index : clusterMap.get(key)) {
int[] pixelRGB = getRGBValue(rgb[index]);
red += pixelRGB[0];
green += pixelRGB[1];
blue += pixelRGB[2];
}
int size = clusterMap.get(key).size();
if (size == 0) {
size = 1;
}
int[] centroidValue = { red / size, green / size, blue / size };
newCentroids[key] = centroidValue;
}
return newCentroids;
}
private static int calculateEuclideanDistance(int[] pixelA, int[] pixelB) {
int distance = 0;
for (int i = 0; i < pixelA.length; i++) {
distance += Math.pow((pixelA[i] - pixelB[i]), 2);
}
return (int) Math.sqrt(distance);
}
private static int[] getRGBValue(int color) {
int[] rgb = new int[3];
rgb[0] = (color & 0xff0000) >> 16; // Red color
rgb[1] = (color & 0xff00) >> 8; // Green color
rgb[2] = (color & 0xff); // Blue color
return rgb;
}
private static int getColorBit(int[] rgb) {
int color = (rgb[0]) << 16 | (rgb[1]) << 8 | (rgb[2]);
return color;
}
}