-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptiveThreholding1.m
88 lines (63 loc) · 1.98 KB
/
adaptiveThreholding1.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
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
function [threshold] = adaptiveThreholding1(img,considerZeros);
[imgH, imgW] = size(img);
iniT = mean(mean(img));
lvPixelMean=0.0;
hvPixelMean=0.0;
if considerZeros==1
imgVector = reshape(img, imgH*imgW, 1);
lvPixels = find(imgVector<iniT); %% low-value pixels
hvPixels = find(imgVector>=iniT); %% high-value pixels
lvPixelMean = mean(imgVector(lvPixels));
hvPixelMean = mean(imgVector(hvPixels));
T = (lvPixelMean + hvPixelMean)/2;
T0 = 3;
Tdiff = 0;
if T>=iniT
Tdiff = T-iniT;
else
Tdiff = iniT-T;
end
while Tdiff>=T0
iniT = T;
lvPixels = find(imgVector<iniT); %% low-value pixels
hvPixels = find(imgVector>=iniT); %% high-value pixels
lvPixelMean = mean(imgVector(lvPixels));
hvPixelMean = mean(imgVector(hvPixels));
T = (lvPixelMean + hvPixelMean)/2;
if T>=iniT
Tdiff = T-iniT;
else
Tdiff = iniT-T;
end
end
threshold = T;
else
imgVector = reshape(img, imgH*imgW, 1);
lvPixels = find((imgVector<iniT)&(imgVector>0)); %% low-value pixels
hvPixels = find(imgVector>=iniT); %% high-value pixels
lvPixelMean = mean(imgVector(lvPixels));
hvPixelMean = mean(imgVector(hvPixels));
T = (lvPixelMean + hvPixelMean)/2;
T0 = 3;
Tdiff = 0;
if T>=iniT
Tdiff = T-iniT;
else
Tdiff = iniT-T;
end
while Tdiff>=T0
iniT = T;
lvPixels = find((imgVector<iniT)&(imgVector>0)); %% low-value pixels
hvPixels = find(imgVector>=iniT); %% high-value pixels
lvPixelMean = mean(imgVector(lvPixels));
hvPixelMean = mean(imgVector(hvPixels));
T = (lvPixelMean + hvPixelMean)/2;
if T>=iniT
Tdiff = T-iniT;
else
Tdiff = iniT-T;
end
end
threshold = T;
end
threshold = threshold-5;