-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitalMedia_U3.java
More file actions
383 lines (300 loc) · 10.2 KB
/
Copy pathDigitalMedia_U3.java
File metadata and controls
383 lines (300 loc) · 10.2 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.gui.ImageCanvas;
import ij.gui.ImageWindow;
import ij.plugin.PlugIn;
import ij.process.ImageProcessor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* Opens an image window and adds a panel below the image
*/
public class DigitalMedia_U3 implements PlugIn {
ImagePlus imp; // ImagePlus object
private int[] origPixels;
private int width;
private int height;
String[] items = { "Original", "Rot-Kanal", "Graustufen", "Negativ", "Binär", "5 Graustufen", "10 Graustufen",
"Fehlerdiffusion", "Sepia", "6 Farben" };
public static void main(String args[]) {
IJ.open("bear.jpg");
DigitalMedia_U3 pw = new DigitalMedia_U3();
pw.imp = IJ.getImage();
pw.run("");
}
public void run(String arg) {
if (imp == null)
imp = WindowManager.getCurrentImage();
if (imp == null) {
return;
}
CustomCanvas cc = new CustomCanvas(imp);
storePixelValues(imp.getProcessor());
new CustomWindow(imp, cc);
}
private void storePixelValues(ImageProcessor ip) {
width = ip.getWidth();
height = ip.getHeight();
origPixels = ((int[]) ip.getPixels()).clone();
}
class CustomCanvas extends ImageCanvas {
CustomCanvas(ImagePlus imp) {
super(imp);
}
} // CustomCanvas inner class
class CustomWindow extends ImageWindow implements ItemListener {
private String method;
CustomWindow(ImagePlus imp, ImageCanvas ic) {
super(imp, ic);
addPanel();
}
void addPanel() {
// JPanel panel = new JPanel();
Panel panel = new Panel();
JComboBox cb = new JComboBox(items);
panel.add(cb);
cb.addItemListener(this);
add(panel);
pack();
}
public void itemStateChanged(ItemEvent evt) {
// Get the affected item
Object item = evt.getItem();
if (evt.getStateChange() == ItemEvent.SELECTED) {
System.out.println("Selected: " + item.toString());
method = item.toString();
changePixelValues(imp.getProcessor());
imp.updateAndDraw();
}
}
private void changePixelValues(ImageProcessor ip) {
// Array zum Zurückschreiben der Pixelwerte
int[] pixels = (int[]) ip.getPixels();
if (method.equals("Original")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
pixels[pos] = origPixels[pos];
}
}
}
if (method.equals("Rot-Kanal")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
// int g = (argb >> 8) & 0xff;
// int b = argb & 0xff;
pixels[pos] = (0xFF << 24) | (r << 16) | (0 << 8) | 0;
}
}
}
// Methode um Negativ zu machen.
if (method.equals("Negativ")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
int rn = 255 - r; // Farbwerte umkehren
int gn = 255 - g;
int bn = 255 - b;
pixels[pos] = (0xFF << 24) | (rn << 16) | (gn << 8) | bn;
}
}
}
// Methode um Graustufen zu machen.
if (method.equals("Graustufen")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
// Durchschnitt berechnen
int gray = (r + g + b) / 3;
pixels[pos] = (0xFF << 24) | (gray << 16) | (gray << 8) | gray;
}
}
}
// Methode um 5 Graustufen zu machen.
if (method.equals("5 Graustufen")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
// Durchschnitt berechnen
int gray = (r + g + b) / 3;
// Schwellenbereich erzeugen, ähnlich der Streifen der USA Flagge
double schwelle = 255 / 4.0;
/*
* Grauwert durch Schwellenwert teilen und Ganzzahl berechnen. Je nachdem
* entstehen werte zwischen 0 und 4 (5 Farben), die wiederum multipliziert, mit
* dem Schwellenwert selbst, jeweils ein vielfaches von 255/4 ergeben: 0, 63,25
* ... 255.
*/
gray = (int) ((int) (gray / schwelle) * schwelle);
pixels[pos] = (0xFF << 24) | (gray << 16) | (gray << 8) | gray;
}
}
}
// Methode um 10 Graustufen zu machen.
if (method.equals("10 Graustufen")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
// Durchschnitt berechnen
int gray = (r + g + b) / 3;
// Schwellenbereich erzeugen, ähnlich der Streifen der USA Flagge
double schwelle = 255 / 9.0;
/*
* Grauwert durch Schwellenwert teilen und Ganzzahl berechnen. Je nachdem
* entstehen werte zwischen 0 und 9 (10 Farben), die wiederum multipliziert, mit
* dem Schwellenwert selbst, jeweils ein vielfaches von 255/4 ergeben: 0, 28, 56
* ... 255.
*/
gray = (int) ((int) (gray / schwelle) * schwelle);
pixels[pos] = (0xFF << 24) | (gray << 16) | (gray << 8) | gray;
}
}
}
// Methode um Binär zu machen.
if (method.equals("Binär")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
// Durchschnitt berechnen
int gray = (r + g + b) / 3;
// Wird der Schwellenwert überschritten, ist der Pixel weiß
if (gray > 128) {
pixels[pos] = (0xFF << 24) | (255 << 16) | (255 << 8) | 255;
} else { // sonst schwarz
pixels[pos] = (0xFF << 24) | (0 << 16) | (0 << 8) | 0;
}
}
}
}
// Methode um Fehlerdiffusion zu machen.
// Formel nach https://www.youtube.com/watch?v=0t8BHaLsXTM&t=255s
if (method.equals("Fehlerdiffusion")) {
for (int y = 0; y < height; y++) {
int error = 0;
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
// Durchschnitt berechnen
int gray = (r + g + b) / 3;
/*
* Alter Code, nicht mehr korrekt if (gray + error < 128) { error = gray +
* error; } else { error = 255 - (gray - error); } gray += error;
* ----------------------------------
*/
gray = gray + error;
// Wird der Schwellenwert überschritten, ist der Pixel weiß
// Fehlerzahl wird fortgeführt.
if (gray < 128) {
error = gray;
pixels[pos] = (0xFF << 24) | (0 << 16) | (0 << 8) | 0;
} else {
error = gray - 255;
pixels[pos] = (0xFF << 24) | (255 << 16) | (255 << 8) | 255;
}
}
}
}
// Methode um Sepia zu machen.
if (method.equals("Sepia")) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
// Durchschnitt berechnen und Filterstärke setzen
int gray = (r + g + b) / 3;
int depth = 20;
int intensity = 30;
// Sepiaberechnung gemäß Tipp von Milana Tran:
// https://www.tutorialspoint.com/how-to-convert-a-colored-image-to-sepia-image-using-java-opencv-library
int rn = gray + (depth * 2);
int gn = gray + depth;
int bn = gray - intensity;
// Hier muessen die neuen RGB-Werte wieder auf den Bereich von 0 bis 255
// begrenzt werden
rn = Math.min(255, Math.max(0, rn));
gn = Math.min(255, Math.max(0, gn));
bn = Math.min(255, Math.max(0, bn));
pixels[pos] = (0xFF << 24) | (rn << 16) | (gn << 8) | bn;
}
}
}
// Methode um 6 Farben zu machen.
if (method.equals("6 Farben")) {
//6 Farben aus dem Beispielbild in Hexnotation.
int[] six_colors = {0x665A51,0x222524, 0x9E9086, 0x2E6489, 0xD1CFD0, 0x6996A2};
double maxDist;
double minDist;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int argb = origPixels[pos]; // Lesen der Originalwerte
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = argb & 0xff;
//Distanz auf Maximum setzen
maxDist = Math.sqrt(Math.pow(255,2)*3);
for (int c : six_colors) {
int red = (c >> 16) & 0xff;
int grn = (c >> 8) & 0xff;
int blu = c & 0xff;
//Distanz zwischen Pixel und aktueller Farbe berechnen
minDist = Math.sqrt(Math.pow(red-r, 2) + Math.pow(grn-g, 2) + Math.pow(blu-b, 2));
/*
* Wenn die neue Distanz kleiner ist (unser Ziel!), dann diese als neues Maximum übernehmen
* (evtl ist die nächste Farbe kleiner) und schon mal auf den Pixel anwenden, bis eine
* bessere Farbe gefunden wird, die dann die neue Obergrence bildet.
*/
if (maxDist > minDist) {
maxDist = minDist;
//Sobald die kleinste Distanz gefunden ist, die Farbwerte schreiben.
pixels[pos] = (0xFF << 24) | (red << 16) | (grn << 8) | blu;
}
}
}
}
}
}
} // CustomWindow inner class
}