/
GrayLevelClassMixtureModeling.java
224 lines (182 loc) · 4.68 KB
/
GrayLevelClassMixtureModeling.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
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
package emblcmci.ext;
/*
* Mixture Modeling algorithm
*
* Copyright (c) 2003 by Christopher Mei (christopher.mei@sophia.inria.fr)
* and Maxime Dauphin
*
* This plugin is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this plugin; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* This code was downloaded from
* http://rsbweb.nih.gov/ij/plugins/mixture-modeling.html
*
*/
import java.util.*;
import ij.*;
import ij.process.*;
/** This class implements a GrayLevelClassMixtureModeling.
**/
public class GrayLevelClassMixtureModeling {
public static int[] histogram;
/** The index must vary between 1 and 253
C1 : [0;index]
C2 : [index+1; 255]
**/
private int index;
private float mu1, mu2;
private float sigma2_1, sigma2_2;
private float mult1, mult2;
private float twoVariance1, twoVariance2;
private float max1, max2;
private int cardinal1, cardinal2;
private int cardinal;
private int INDEX_MIN = 1;
private int INDEX_MAX = 253;
private int MIN = 0;
public static final int MAX = 255;
public GrayLevelClassMixtureModeling(ByteProcessor img) {
cardinal = img.getWidth()*img.getHeight();
histogram = img.getHistogram();
index = INDEX_MIN-1;
//setValues();
}
public boolean addToIndex() {
index++;
if(!(index<=INDEX_MAX))
return false;
setValues();
return true;
}
private float calculateMax(int index) {
float sum = histogram[index];
float num = 1;
if(index-1>=0) {
sum += histogram[index-1];
num++;
}
if(index+1<MAX) {
sum += histogram[index+1];
num++;
}
return sum/num;
}
public String toString() {
StringBuffer ret = new StringBuffer();
ret.append("Index : "+index+"\n");
ret.append("Max1 : "+max1+" ");
ret.append("Max2 : "+max2+"\n");
ret.append("Mu1 : "+mu1+" ");
ret.append("Mu2 : "+mu2+"\n");
ret.append("Cardinal1 : "+cardinal1+" ");
ret.append("Cardinal2 : "+cardinal2+"\n");
ret.append("Variance1 : "+sigma2_1+" ");
ret.append("Variance2 : "+sigma2_2+"\n");
return ret.toString();
}
public float getCardinal() {
return cardinal;
}
public float getMu1() {
return mu1;
}
public float getMu2() {
return mu2;
}
public float getMax1() {
return max1;
}
public float getMax2() {
return max2;
}
public float getVariance1() {
return sigma2_1;
}
public float getVariance2() {
return sigma2_2;
}
public float getCardinal1() {
return cardinal1;
}
public float getCardinal2() {
return cardinal2;
}
public int getThreshold() {
return index;
}
public void setIndex(int index) {
this.index = index;
setValues();
}
private void setValues() {
mu1 = 0; mu2 = 0;
sigma2_1 = 0; sigma2_2 = 0;
max1 = 0; max2 = 0;
cardinal1 = 0; cardinal2 = 0;
for(int i=MIN; i<=index ; i++) {
cardinal1 += histogram[i];
mu1 += i*histogram[i];
}
for(int i=index+1; i<=MAX ; i++) {
cardinal2 += histogram[i];
mu2 += i*histogram[i];
}
if(cardinal1 == 0) {
mu1 = 0;
sigma2_1 = 0;
}
else
mu1 /= (float)cardinal1;
if(cardinal2 == 0) {
mu2 = 0;
sigma2_2 = 0;
}
else
mu2 /= (float)cardinal2;
if( mu1 != 0 ) {
for(int i=MIN; i<=index ; i++)
sigma2_1 += histogram[i]*Math.pow(i-mu1,2);
sigma2_1 /= (float)cardinal1;
max1 = calculateMax((int) mu1);
mult1 = (float) max1;
twoVariance1 = 2*sigma2_1;
}
if( mu2 != 0 ) {
for(int i=index+1; i<=MAX ; i++)
sigma2_2 += histogram[i]*Math.pow(i-mu2,2);
sigma2_2 /= (float)cardinal2;
max2 = calculateMax((int) mu2);
mult2 = (float) max2;
twoVariance2 = 2*sigma2_2;
}
}
public final float gamma1(int i) {
if(sigma2_1 == 0)
return 0;
return (float)(mult1*Math.exp(-(Math.pow((float)i-mu1,2))/twoVariance1));
}
public final float gamma2(int i) {
if(sigma2_2 == 0)
return 0;
return (float)(mult2*Math.exp(-(Math.pow((float)i-mu2,2))/twoVariance2));
}
public float gamma(int i) {
return gamma1(i)+gamma2(i);
}
public float differenceGamma(int i) {
return gamma1(i)-gamma2(i);
}
public static int getHistogram(int i) {
return histogram[i];
}
}