-
Notifications
You must be signed in to change notification settings - Fork 33
/
Average_Color.java
189 lines (165 loc) · 4.72 KB
/
Average_Color.java
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
import color.CIELAB;
import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.gui.GenericDialog;
import ij.gui.Roi;
import ij.gui.ShapeRoi;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import ij.process.ColorProcessor;
import java.awt.Rectangle;
import java.util.ArrayList;
public class Average_Color implements PlugInFilter {
protected ImagePlus image;
protected int w, h;
protected int[] pixels;
public void test() {
GenericDialog gd = new GenericDialog("L*");
gd.addNumericField("L*", 75, 0);
gd.showDialog();
if (gd.wasCanceled())
return;
float l = (float)gd.getNextNumber();
float[] lab = new float[3];
float[] rgb = new float[3];
int[] rgbi = new int[3];
w = h = 128 * 2 + 1;
pixels = new int[w * h];
for (int a = -128; a <= 128; a++)
for (int b = -128; b <= 128; b++) {
lab[0] = l;
lab[1] = a;
lab[2] = b;
CIELAB.CIELAB2sRGB(lab, rgb);
rgbi[0] = CIELAB.unnorm(rgb[0]);
rgbi[1] = CIELAB.unnorm(rgb[1]);
rgbi[2] = CIELAB.unnorm(rgb[2]);
pixels[a + 128 + (b + 128) * w] =
(rgbi[0] << 16) | (rgbi[1] << 8) |
rgbi[2];
}
ColorProcessor p = new ColorProcessor(w, h, pixels);
new ImagePlus("Colors", p).show();
}
public void run(ImageProcessor ip) {
Roi roi = image.getRoi();
if (roi == null) {
IJ.error("Need a ROI");
return;
}
boolean haveShapeRoi = (roi instanceof ShapeRoi);
GenericDialog gd = new GenericDialog("Average Color");
gd.addCheckbox("CIELab averaging", true);
if (haveShapeRoi)
gd.addCheckbox("Split roi", true);
gd.showDialog();
if(gd.wasCanceled())
return;
boolean cielab = gd.getNextBoolean();
boolean splitRoi = haveShapeRoi ? gd.getNextBoolean() : false;
w = image.getWidth();
h = image.getHeight();
pixels = (int[])image.getProcessor().getPixels();
if (splitRoi) {
ShapeRoi shape = (ShapeRoi)roi;
Roi[] rois = shape.getRois();
for (int i = 0; i < rois.length; i++)
averageColorInRoi(rois[i], cielab);
} else
averageColorInRoi(roi, cielab);
image.updateAndDraw();
}
final float[] getAverageColor(Roi roi, boolean cielab) {
Rectangle r = roi.getBounds();
float[] rgb = new float[3];
float[] lab = new float[3];
float[] cumul = new float[3];
int count = 0;
/* first get the cumulated values in the given color space */
for (int y = r.y; y < r.y + r.height; y++)
for (int x = r.x; x < r.x + r.width; x++) {
if (!roi.contains(x, y))
continue;
int v = pixels[x + w * y];
rgb[0] = CIELAB.norm((v >> 16) & 0xff);
rgb[1] = CIELAB.norm((v >> 8) & 0xff);
rgb[2] = CIELAB.norm(v & 0xff);
if (cielab) {
CIELAB.sRGB2CIELAB(rgb, lab);
cumul[0] += lab[0];
cumul[1] += lab[1];
cumul[2] += lab[2];
} else {
cumul[0] += rgb[0];
cumul[1] += rgb[1];
cumul[2] += rgb[2];
}
count++;
}
/* then make the average... */
cumul[0] /= count;
cumul[1] /= count;
cumul[2] /= count;
return cumul;
}
private void averageColorInRoi(Roi roi, boolean cielab) {
Rectangle r = roi.getBounds();
float[] cumul = getAverageColor(roi, cielab);
float[] rgb = new float[3];
int[] rgbi = new int[3];
if (cielab) {
CIELAB.CIELAB2sRGB(cumul, rgb);
rgbi[0] = CIELAB.unnorm(rgb[0]);
rgbi[1] = CIELAB.unnorm(rgb[1]);
rgbi[2] = CIELAB.unnorm(rgb[2]);
} else {
rgbi[0] = CIELAB.unnorm(cumul[0]);
rgbi[1] = CIELAB.unnorm(cumul[1]);
rgbi[2] = CIELAB.unnorm(cumul[2]);
}
int v = (rgbi[0] << 16) | (rgbi[1] << 8) | rgbi[2];
/* and now substitute that */
for (int y = r.y; y < r.y + r.height; y++)
for (int x = r.x; x < r.x + r.width; x++)
if (roi.contains(x, y))
pixels[x + w * y] = v;
}
public int setup(String args, ImagePlus imp) {
this.image = imp;
return DOES_RGB;
}
public static String getAverageCIELAB() {
Average_Color t = new Average_Color();
try {
t.image = WindowManager.getCurrentImage();
t.pixels = (int[])t.image.getProcessor().getPixels();
t.w = t.image.getWidth();
t.h = t.image.getHeight();
Roi roi = t.image.getRoi();
if (roi == null)
roi = new Roi(0, 0, t.w, t.h);
float[] result = t.getAverageColor(roi, true);
return "" + result[0] + " " + result[1] + " " + result[2];
} catch (Exception e) {
return "";
}
}
public static String setColorCIELAB(String L, String a, String b) {
try {
float[] lab = new float[3];
lab[0] = Float.parseFloat(L);
lab[1] = Float.parseFloat(a);
lab[2] = Float.parseFloat(b);
float[] rgb = new float[3];
CIELAB.CIELAB2sRGB(lab, rgb);
int red = CIELAB.unnorm(rgb[0]);
int green = CIELAB.unnorm(rgb[1]);
int blue = CIELAB.unnorm(rgb[2]);
IJ.setForegroundColor(red, green, blue);
return "" + red + " " + green + " " + blue;
} catch (Exception e) {
return "";
}
}
}