-
Notifications
You must be signed in to change notification settings - Fork 0
/
otsu.c
139 lines (90 loc) · 2.69 KB
/
otsu.c
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
#include <stdio.h>
#include <windows.h>
#pragma warning(disable:4996)
int main() {
FILE* fp;
unsigned char Readbuf[256][256];
unsigned char Writebuf[256][256];
unsigned long histogram[256] = { 0 };
double histogram_n[256] = { 0 };
double histogram_c[256];
float scale = 256 * 256;
fp = fopen("LENA_256x256_8bit.raw", "rb");
for (int i = 0; i < 256; i++)
fread(Readbuf[i], 1, 256, fp);
fclose(fp);
//히스토그램
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
histogram[Readbuf[i][j]]++;
}
}
int result, wb, wf, ub, uf, u, max, temp;
result = wb = wf = ub = uf = u = max = temp = 0;
// 오츄
for (int i = 0; i < 256; i++) {
for (int j = 0; j < i + 1; j++) { // background
wb += histogram[j]; // weight
ub = (double)(ub + j * histogram[i] / wb); // mean
}
for (int j = i + 1; j < 256; j++) { // foreground
wf += histogram[j]; // weight
uf = (double)(uf + j * histogram[j] / wf); // mean
}
u = (wb * ub) + (wf * uf); // total mean
result = wb * wf * (uf - ub) * (uf - ub); //between variance
// result = (wb*(ub-u)*(ub-u)) + (wf*(uf-u)*(uf-u))
if (result > max) {
max = result;
temp = i;
}
}
//임계값 T
int T = temp;
printf("T = %d", T);
//영상 이진화
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
if(Readbuf[i][j] > T)
Writebuf[i][j] = 255;
else
Writebuf[i][j] = 0;
}
}
//영상 저장
fp = fopen("lena.raw", "wb");
for (int i = 0; i < 256; i++)
fwrite(Writebuf[i], 1, 256, fp);
fclose(fp);
return 0;
}
int otsh_threshold = 0;
double inter_class_variance = 0;
double calcBinary[256];
Mat dst; // 사전에 정규화된 히스토그램 행렬
// 0 ~ 256 사이의 임계값 구하기
for (int i = 0; i < 256; i++) {
int alpha = 0, beta = 0;
int sum1 = 0, sum2 = 0;
double avg1 = 0, avg2 = 0;
// w0 계산 (0 ~ t까지의 누적합)
for (int j = 0; j < i; j++)
sum1 += calcBinary[j];
// w1 계산 (t + 1 ~ L - 1까지의 누적합)
sum2 = dst.rows * dst.cols - sum1;
// w0과 w1의 가중치 구하기
alpha = (double)sum1 / (double)calcBinary.size();
beta = (double)sum2 / (double)calcBinary.size();
// μ0과 μ1 구하기 (w0과 w1의 평균)
for (int m = 0; m < i; m++)
avg1 += (double)(m * calcBinary[m]) / (double)sum1;
for (int m = i; m < 256; m++)
avg2 += (double)(m * calcBinary[m]) / (double)sum2;
// v0과 v1 구하기 (분산)
double temp = alpha * beta * pow((avg1 - avg2), 2);
// 가장 큰 V between(t)를 임계값 T로 취한다.
if (inter_class_variance < temp) {
inter_class_variance = temp;
otsh_threshold = i;
}
}