-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElenThreshold.m
64 lines (55 loc) · 2.13 KB
/
ElenThreshold.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
%% Histogram-based Global Thresholding
% -------------------------------------------------------------------------
% This study presents a novel and effective approach to global thresholding
% method of grayscale images. In the proposed method, alpha (α) and beta
% (β) regions are determined using the mean and standard deviation values
% of an image histogram. The optimum threshold value is obtained by
% calculating the average of gray-scale values of the α and β regions.
%
% Cite:
% Elen, A. & Dönmez, E. (2024). Histogram-based global thresholding method
% for image binarization, Optik, vol. 306, pp. 1-20.
% https://doi.org/10.1016/j.ijleo.2024.171814
%
% Contact:
% Department of Software Engineering, Faculty of Engineering and Natural
% Sciences, Bandirma Onyedi Eylul University, Balikesir / TURKIYE.
% -------------------------------------------------------------------------
function result = ElenThreshold(img)
% Histogram of the grayscale image.
hst = imhist(img);
% Set the histogram bins from 0 to 255.
bins = (0 : 255)';
% Probability of the histogram.
prob = hst/sum(hst);
% Mean value of the histogram.
avg = sum(bins.*prob);
% Standard deviation of the histogram.
stdDev = sqrt(sum(((bins-avg).^2).*prob));
% Alpha and Beta region variables.
weightA = 0; pixelA = 0;
weightB = 0; pixelB = 0;
% Lower and upper bounds of the Alpha region.
lbA = avg - stdDev;
ubA = avg + stdDev;
for i = 0 : 1 : 255
% Get current bin location.
binLoc = hst(i + 1);
if (i >= lbA && i <= ubA)
% Alpha region.
weightA = weightA + binLoc;
pixelA = pixelA + (i * binLoc);
else
% Beta region.
weightB = weightB + binLoc;
pixelB = pixelB + (i * binLoc);
end
end
% Mean value of the Alpha region.
avgA = (pixelA / weightA);
% Mean value of the Beta region.
avgB = (pixelB / weightB);
% Threshold value.
result = (avgA + avgB) / 2.0;
end