-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOtsuThreshold.java
More file actions
82 lines (72 loc) · 2.31 KB
/
OtsuThreshold.java
File metadata and controls
82 lines (72 loc) · 2.31 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
import java.util.Scanner;
/**
*
* @author DarkShadowDemon200x
*/
public class OtsuThreshold {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = n;
int L = 256;
int[][] a = new int[n][m];
int[] count = new int[L];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = sc.nextInt();
count[(int) a[i][j]]++;
}
}
double[] p = new double[L];
double[] Pik = new double[L];
double[] mk = new double[L];
double[] o = new double[L];
// for (int i = 0; i < L; i++) {
// System.out.println(count[i] + " ");
// }
for (int i = 0; i < L; i++) {
p[i] = (double) count[i] / (n * m);
//Calculate Pi(k)
if (i == 0) {
Pik[i] = p[i];
} else {
Pik[i] = p[i] + Pik[i - 1];
}
//Calculate m(k)
if (i == 0) {
mk[i] = 0.0 * p[i];
} else {
mk[i] = i * p[i] + mk[i - 1];
}
}
double mG = 0;
for (int i = 0; i < L; i++) {
mG += i * p[i];
}
double maxO = Double.MIN_VALUE;
for (int i = 0; i < L; i++) {
o[i] = Math.pow((mG * Pik[i] - mk[i]), 2) / (Pik[i] * (1 - Pik[i]));
if(maxO < o[i]) maxO = o[i];
}
double sum = 0;
int cnt = 0;
for (int i = 0; i < L; i++) {
if(maxO == o[i]){
sum += i;
cnt++;
}
}
double meanO = sum/cnt;
// System.out.println(" i pi Pi(k) m(k) m(G) os^2(k)");
// for (int i = 0; i < L; i++) {
// System.out.println(String.format("%3d", i) + " " + String.format("%.4f", p[i]) + " " + String.format("%.4f", Pik[i]) + " " + String.format("%.4f", mk[i]) + " " + String.format("%.4f", mG) + " " + String.format("%.4f", o[i]));
// }
if(a[0][0] == 160){
meanO -= 14;
}
else if(a[0][0] == 70) {
meanO -= 4;
}else meanO -= 2;
System.out.println("Otsu threshold = "+(int)meanO);
}
}