-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdynamic_otsu.h
250 lines (196 loc) · 6.93 KB
/
dynamic_otsu.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#pragma once
#include <opencv2/opencv.hpp>
#include <string>
#include <algorithm>
#include <limits>
using namespace std;
vector<int> get_histogram(cv::Mat origin);
vector<double> normalize_histogram(vector<int> histogram, int M, int L);
vector<int> find_valleys(vector<double> norm_hist);
vector<int> threshold_valley_regions(vector<int> histogram, vector<int> valleys, int N);
int otsu_method(vector<int> histogram);
/*
vector<int> get_histogram(cv::Mat origin)
Parameters
---
origin: cv::Mat (256 Gray-scale Image)
Return
---
'origin' Image histogram: vector<int>
Info
---
Return image histogram type to vector<int>.
*/
vector<int> get_histogram(cv::Mat origin)
{
vector<int> histogram(256);
for (int i = 0; i < origin.rows; i++) {
for (int j = 0; j < origin.cols; j++)
{
int val = origin.at<uchar>(i, j);
histogram[val]++;
}
}
return histogram;
}
/*
vector<double> normalize_histogram(vector<int> histogram, int M, int L)
Parameters
---
histogram: vector<int> See: vector<int> get_histogram(cv::Mat origin)
M: int Divided Num for Sets [EX: 256 Gray level image divded 'M' groups -> One Group has N pixels 'N = 256 / M']
L: int Image Gray Level Scale [EX: 256]
Return
---
normalized histogram binning: vector<double>
Info
---
Return normalize histogram type to vector<double>.
Normalized histogram binning reduces noise since bin grouping operates like a smoothing spatial filter.
Bin grouping also greatly increases the stability of the valley estimation process.
Page 6(5636). AUTOMATIC MULTILEVEL THRESHOLDING WITH CLUSTER DETERMINATION, ICIC International, 2011
*/
vector<double> normalize_histogram(vector<int> histogram, int M, int L)
{
vector<double> norm_hist(M);
int N = L / M;
double max;
for (int i = 0; i < L; i++) {
norm_hist[i / N] += histogram[i];
}
max = *max_element(norm_hist.begin(), norm_hist.end());
for (int i = 0; i < norm_hist.size(); i++) {
//norm_hist[i] = round(norm_hist[i] / max * 100.0);
norm_hist[i] = norm_hist[i] / max * 100.0;
}
return norm_hist;
}
/*
vector<int> find_valleys(vector<double> norm_hist)
Parameters
---
norm_hist: vector<double> See: vector<double> normalize_histogram(vector<int> histogram, int M, int L)
Return
---
valleys: vector<int>
Info
---
Return valleys type to vector<int>.
The probabilities for the 9 combinations of bin Group Ci can be given as follows,
Pr(Ci) = 0% if (H(i) > H(i-1)) || (H(i) > H(i+1))
Pr(Ci) = 25% if (H(i) < H(i-1)) && (H(i) == H(i+1))
Pr(Ci) = 75% if (H(i) == H(i-1)) && (H(i) < H(i+1))
Pr(Ci) = 100% if (H(i) < H(i-1)) && (H(i) < H(i+1))
Pr(Ci) = Pr(Ci-1) if (H(i) == H(i-1)) && (H(i) == H(i+1))
Pr(C0) = Pr(CM-1) = 0% M - norm_hist.size()
The probabilities determind in the prvious stage are then added together only if the probability of the scanning counter Ci is not equal to zero.
If the sum of probabilities is higher than or equal to 100%, the probability of Ci is reset to 100%. Otherwise, it is 0%.
Pr(Ci) = 0% if (Pr(Ci-1) + Pr(Ci) + Pr(Ci+1)) < 100%
Pr(Ci) = 100% if (Pr(Ci-1) + Pr(Ci) + Pr(Ci+1)) >= 100%
Pr(C0) = Pr(CM-1) = 0% M - norm_hist.size()
Page 6 ~ 7(5636 ~ 5637). AUTOMATIC MULTILEVEL THRESHOLDING WITH CLUSTER DETERMINATION, ICIC International, 2011
*/
vector<int> find_valleys(vector<double> norm_hist)
{
vector<int> valleys;
vector<double> probs(norm_hist.size());
for (int i = 1; i < norm_hist.size() - 1; i++) {
if (norm_hist[i] > norm_hist[i - 1] || norm_hist[i] > norm_hist[i + 1]) probs[i] = 0.0;
else if (norm_hist[i] < norm_hist[i - 1] && norm_hist[i] == norm_hist[i + 1]) probs[i] = 0.25;
else if (norm_hist[i] == norm_hist[i - 1] && norm_hist[i] < norm_hist[i + 1]) probs[i] = 0.75;
else if (norm_hist[i] < norm_hist[i - 1] && norm_hist[i] < norm_hist[i + 1]) probs[i] = 1.0;
else if (norm_hist[i] == norm_hist[i - 1] && norm_hist[i] == norm_hist[i + 1]) probs[i] = probs[i - 1];
}
vector<int> crobs(probs.size());
for (int i = probs.size()-2; i > 0; i--)
{
if (probs[i] != 0) {
if (probs[i - 1] + probs[i] + probs[i + 1] >= 1.0) crobs[i] = 1;
else crobs[i] = 0;
}
else crobs[i] = 0;
}
//for (int i = 1; i < probs.size() - 1; i++)
//{
// if (probs[i - 1] + probs[i] + probs[i + 1] >= 1.0 && probs[i] != 0) crobs[i] = 1;
// else crobs[i] = 0;
//}
for (int i = 0; i < crobs.size(); i++) {
if (crobs[i] > 0) valleys.push_back(i);
}
return valleys;
}
/*
vector<int> threshold_valley_regions(vector<int> histogram, vectr<int> valleys, int N)
Parameters
---
histogram: vector<int> See: vector<int> get_histogram(cv::Mat origin)
valleys: vector<int> See: vector<int> find_valleys(vector<double> norm_hist)
N: int Number of pixels in a group ['N = 256 / M']
Return
---
thresholds: vector<int>
Info
---
Return thresholds type to vector<int>.
Implementation OTSU Method about each valley histogram.
TSMO method ar always close to the one decided by bilevel Otsu's thresholding.
Page 8(5638). AUTOMATIC MULTILEVEL THRESHOLDING WITH CLUSTER DETERMINATION, ICIC International, 2011
*/
vector<int> threshold_valley_regions(vector<int> histogram, vector<int> valleys, int N) {
vector<int> thresholds;
for (int i = 0; i < valleys.size(); i++) {
vector<int> temp_hist;
int start_pos = (valleys[i] * N) - N;
int end_pos = (valleys[i] + 2) * N;
for (int idx = start_pos; idx < end_pos; idx++) {
temp_hist.push_back(histogram[idx]);
}
thresholds.push_back(otsu_method(temp_hist) + start_pos);
}
return thresholds;
}
/*
int otsu_method(vector<int> histogram)
Parameters
---
histogram: vector<int> See: vector<int> get_histogram(cv::Mat origin)
Return
---
threshold: int
Info
---
Return threshold type to int.
Implementation OTSU Method about input histogram.
Bilevel thresholding, Otsu's method is a very popular nonparametric thresholding technique,
which searches for an optimum threshold by maximizing the between-class variance in a gray image.
Page 3 ~ 4(5633 ~ 5634). AUTOMATIC MULTILEVEL THRESHOLDING WITH CLUSTER DETERMINATION, ICIC International, 2011
*/
int otsu_method(vector<int> histogram) {
int num_bins = histogram.size();
int total = 0;
int sum_total = 0;
for (int i = 0; i < num_bins; i++) {
total += histogram[i];
sum_total += i * histogram[i];
}
double weight_background = 0.0;
double sum_background = 0.0;
int optimum_value = 0;
double maximum = DBL_MIN;
for (int i = 0; i < num_bins; i++) {
weight_background += histogram[i];
if (weight_background == 0) continue;
int weight_foreground = total - weight_background;
if (weight_foreground == 0) break;
sum_background += i * (double)histogram[i];
double mean_foreground = (sum_total - sum_background) / weight_foreground;
double mean_background = sum_background / weight_background;
double inter_class_variance = weight_background * weight_foreground * pow(mean_background - mean_foreground, 2);
if (inter_class_variance > maximum) {
optimum_value = i;
maximum = inter_class_variance;
}
}
return optimum_value;
}