1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.samples.clustering;
23
24 import java.awt.BorderLayout;
25 import java.awt.Color;
26 import java.awt.Component;
27 import java.awt.Dimension;
28 import java.awt.FlowLayout;
29 import java.awt.Graphics;
30 import java.awt.GridLayout;
31 import java.awt.image.BufferedImage;
32 import java.awt.image.Raster;
33 import java.awt.image.WritableRaster;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import javax.imageio.ImageIO;
39 import javax.swing.BorderFactory;
40 import javax.swing.Box;
41 import javax.swing.ImageIcon;
42 import javax.swing.JButton;
43 import javax.swing.JLabel;
44 import javax.swing.JPanel;
45 import javax.swing.JSpinner;
46 import javax.swing.SpinnerNumberModel;
47
48 import org.hipparchus.clustering.CentroidCluster;
49 import org.hipparchus.clustering.Clusterable;
50 import org.hipparchus.clustering.KMeansPlusPlusClusterer;
51 import org.hipparchus.samples.ExampleUtils;
52 import org.hipparchus.samples.ExampleUtils.ExampleFrame;
53
54
55
56
57
58 @SuppressWarnings("serial")
59 public class ImageClusteringExample {
60
61
62
63
64
65
66
67
68 public ImageClusteringExample() {
69
70 }
71
72
73 public static class Display extends ExampleFrame {
74
75
76 private BufferedImage referenceImage;
77
78
79 private BufferedImage clusterImage;
80
81
82 private Raster referenceRaster;
83
84
85 private ImagePainter painter;
86
87
88 private JSpinner clusterSizeSpinner;
89
90
91
92
93 public Display() throws IOException {
94 setTitle("Hipparchus: Image Clustering Example");
95 setSize(900, 350);
96
97 setLayout(new FlowLayout());
98
99 Box bar = Box.createHorizontalBox();
100
101 ClassLoader classLoader = ExampleUtils.class.getClassLoader();
102 referenceImage = ExampleUtils.resizeImage(
103 ImageIO.read(classLoader.getResourceAsStream("ColorfulBird.jpg")),
104 350,
105 240,
106 BufferedImage.TYPE_INT_RGB);
107
108 referenceRaster = referenceImage.getData();
109
110 clusterImage = new BufferedImage(referenceImage.getWidth(),
111 referenceImage.getHeight(),
112 BufferedImage.TYPE_INT_RGB);
113
114 JLabel picLabel = new JLabel(new ImageIcon(referenceImage));
115 bar.add(picLabel);
116
117 painter = new ImagePainter(clusterImage.getWidth(), clusterImage.getHeight());
118 bar.add(painter);
119
120 JPanel controlBox = new JPanel();
121 controlBox.setLayout(new GridLayout(5, 1));
122 controlBox.setBorder(BorderFactory.createLineBorder(Color.black, 1));
123
124 JPanel sizeBox = new JPanel();
125 JLabel sizeLabel = new JLabel("Clusters:");
126 sizeBox.add(sizeLabel);
127
128 SpinnerNumberModel model = new SpinnerNumberModel(3, 2, 10, 1);
129 clusterSizeSpinner = new JSpinner(model);
130
131 sizeLabel.setLabelFor(clusterSizeSpinner);
132 sizeBox.add(clusterSizeSpinner);
133 controlBox.add(sizeBox, BorderLayout.NORTH);
134
135 JButton startButton = new JButton("Cluster");
136 startButton.setActionCommand("cluster");
137 controlBox.add(startButton, BorderLayout.CENTER);
138
139 bar.add(controlBox);
140
141 add(bar);
142
143 startButton.addActionListener(e -> clusterImage());
144 }
145
146
147
148 private void clusterImage() {
149 List<PixelClusterable> pixels = new ArrayList<>();
150 for (int row = 0; row < referenceImage.getHeight(); row++) {
151 for (int col = 0; col < referenceImage.getWidth(); col++) {
152 pixels.add(new PixelClusterable(col, row));
153 }
154 }
155
156 int clusterSize = ((Number) clusterSizeSpinner.getValue()).intValue();
157 KMeansPlusPlusClusterer<PixelClusterable> clusterer =
158 new KMeansPlusPlusClusterer<>(clusterSize);
159 List<CentroidCluster<PixelClusterable>> clusters = clusterer.cluster(pixels);
160
161 WritableRaster raster = clusterImage.getRaster();
162 for (CentroidCluster<PixelClusterable> cluster : clusters) {
163 double[] color = cluster.getCenter().getPoint();
164 for (PixelClusterable pixel : cluster.getPoints()) {
165 raster.setPixel(pixel.x, pixel.y, color);
166 }
167 }
168
169 Display.this.repaint();
170 }
171
172
173 private class PixelClusterable implements Clusterable {
174
175
176 private final int x;
177
178
179 private final int y;
180
181
182 private double[] color;
183
184
185
186
187
188 PixelClusterable(int x, int y) {
189 this.x = x;
190 this.y = y;
191 this.color = null;
192 }
193
194
195 @Override
196 public double[] getPoint() {
197 if (color == null) {
198 color = referenceRaster.getPixel(x, y, (double[]) null);
199 }
200 return color;
201 }
202
203 }
204
205
206 private class ImagePainter extends Component {
207
208
209 private int width;
210
211
212 private int height;
213
214
215
216
217
218 ImagePainter(int width, int height) {
219 this.width = width;
220 this.height = height;
221 }
222
223
224 @Override
225 public Dimension getPreferredSize() {
226 return new Dimension(width, height);
227 }
228
229
230 @Override
231 public Dimension getMinimumSize() {
232 return getPreferredSize();
233 }
234
235
236 @Override
237 public Dimension getMaximumSize() {
238 return getPreferredSize();
239 }
240
241
242 @Override
243 public void paint(Graphics g) {
244 g.drawImage(clusterImage, 0, 0, this);
245 }
246
247 }
248
249 }
250
251
252
253
254
255 public static void main(String[] args) throws IOException {
256 ExampleUtils.showExampleFrame(new Display());
257 }
258
259 }