-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageHistogram.java
More file actions
152 lines (133 loc) · 3.76 KB
/
ImageHistogram.java
File metadata and controls
152 lines (133 loc) · 3.76 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
import ij.ImagePlus;
import ij.process.ImageProcessor;
import ij.process.ImageStatistics;
import java.awt.Color;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
public class ImageHistogram {
private static final int HSB=0, RGB=1, LAB=2, YUV=3;
// by default, set the lower threshold so most of the image is black
private static double THRESHOLD_PERCENTAGE = 0.98;
private int[] histogram;
private Color[] hColors;
private int hmax;
private int minThreshold, maxThreshold;
public int getMinThreshold() {
return minThreshold;
}
public void setMinThreshold(int minThreshold) {
this.minThreshold = minThreshold;
}
public int getMaxThreshold() {
return maxThreshold;
}
public void setMaxThreshold(int maxThreshold) {
this.maxThreshold = maxThreshold;
}
private int colorSpace = HSB;
public ImageHistogram(ImagePlus imp, int j) {
ImageProcessor ip = imp.getProcessor();
ImageStatistics stats = ImageStatistics.getStatistics(ip, ij.measure.Measurements.AREA + ij.measure.Measurements.MODE, null);
int maxCount2 = 0;
histogram = stats.histogram;
for (int i = 0; i < stats.nBins; i++)
if ((histogram[i] > maxCount2) ) maxCount2 = histogram[i];
hmax = (int)(maxCount2 * 1.15);//GL was 1.5
ColorModel cm = ip.getColorModel();
if (!(cm instanceof IndexColorModel))
return;
IndexColorModel icm = (IndexColorModel)cm;
int mapSize = icm.getMapSize();
if (mapSize!=256)
return;
byte[] r = new byte[256];
byte[] g = new byte[256];
byte[] b = new byte[256];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
hColors = new Color[256];
if (colorSpace==RGB){
if (j==0){
for (int i=0; i<256; i++)
hColors[i] = new Color(i&255, 0&255, 0&255);
}
else if (j==1){
for (int i=0; i<256; i++)
hColors[i] = new Color(0&255, i&255, 0&255);
}
else if (j==2){
for (int i=0; i<256; i++)
hColors[i] = new Color(0&255, 0&255, i&255);
}
}
else if (colorSpace==HSB){
if (j==0){
for (int i=0; i<256; i++)
hColors[i] = new Color(r[i]&255, g[i]&255, b[i]&255);
}
else if (j==1){
for (int i=0; i<256; i++)
hColors[i] = new Color(255&255, 255-i&255, 255-i&255);
//hColors[i] = new Color(192-i/4&255, 192+i/4&255, 192-i/4&255);
}
else if (j==2){
for (int i=0; i<256; i++)
//hColors[i] = new Color(i&255, i&255, 0&255);
hColors[i] = new Color(i&255, i&255, i&255);
}
}
else if (colorSpace==LAB){
if (j==0){
for (int i=0; i<256; i++)
hColors[i] = new Color(i&255, i&255, i&255);
}
else if (j==1){
for (int i=0; i<256; i++)
hColors[i] = new Color(i&255, 255-i&255, 0&255);
}
else if (j==2){
for (int i=0; i<256; i++)
hColors[i] = new Color(i&255, i&255, 255-i&255);
}
}
else if (colorSpace==YUV){
if (j==0){
for (int i=0; i<256; i++)
hColors[i] = new Color(i&255, i&255, i&255);
}
else if (j==1){
for (int i=0; i<256; i++)
hColors[i] = new Color((int)(36+(255-i)/1.4)&255, 255-i&255, i&255);
}
else if (j==2){
for (int i=0; i<256; i++)
hColors[i] = new Color(i&255, 255-i&255, (int)(83+(255-i)/2.87)&255);
}
}
//AutoThresholder thresholder = new AutoThresholder();
//int threshold = thresholder.getThreshold("Default", histogram);
int total=0, i=0;
for(i=0; i < histogram.length ; i++)
total += histogram[i];
double threshold = THRESHOLD_PERCENTAGE * total;
total=0;
for(i=0; i < histogram.length ; i++){
total += histogram[i];
if (total >= threshold)
break;
}
minThreshold = i;
maxThreshold = Math.min(i +20, 255);
System.err.println("Threshold: " + threshold + ". I:" + i);
}
public Color getColor(int i){
return hColors[i];
}
public int get(int i){
return histogram[i];
}
public int getMax(){
return hmax;
}
}