-
Notifications
You must be signed in to change notification settings - Fork 0
/
gray2binary_otsu.m
56 lines (48 loc) · 1.29 KB
/
gray2binary_otsu.m
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
function [best_threshold, binary_image] = gray2binary_otsu(original_image, gray_level)
%GRAY2BINARY_OTSU 此处显示有关此函数的摘要
% n0, n1, m0, m1, p0, p1
% g = p0 * p1 * (m0 - m1) ^ 2
% https://blog.csdn.net/qq_43743037/article/details/105376884
height = size(original_image, 1);
width = size(original_image, 2);
binary_image = zeros(height, width);
best_threshold = 0;
g_max = 0;
for k = 0 : gray_level - 1
n0 = 0;
n1 = 0;
sum0 = 0;
sum1 = 0;
for i = 1 : height
for j = 1 : width
if(original_image(i, j) <= k)
sum0 = sum0 + double(original_image(i, j));
n0 = n0 + 1;
else
sum1 = sum1 + double(original_image(i, j));
n1 = n1 + 1;
end
end
end
p0 = n0 / (height * width);
p1 = n1 / (height * width);
m0 = sum0 / n0;
m1 = sum1 / n1;
m = m0 * p0 + m1 * p1;
g = p0 * (m0 - m) * (m0 - m) + p1 * (m1 - m) * (m1 - m);
%g = p0 * p1 * (m0 - m1) * (m0 - m1);
if(g > g_max)
g_max = g;
best_threshold = k;
end
end
for i = 1 : height
for j = 1 : width
if(original_image(i, j) <= best_threshold)
binary_image(i, j) = 1;
else
binary_image(i, j) = 0;
end
end
end
end