matlab code is listed as follow:
function outputbinaryimage = adaptivethresholding(inputimage)
if ndims(inputimage) == 3
inputimage = rgb2gray(inputimage);
end
inputimage = double(inputimage);
[width height] = size(inputimage);
outputbinaryimage = zeros(width,height);
s = width/8;
t = 15;
intimg = ones(width,height).*255;
for i = 1:width
sum = 0;
for j = 1:height
sum = sum + inputimage(i,j);
if i == 1
intimg(i,j) = sum;
else
intimg(i,j) = intimg(i-1,j) + sum;
end
end
end
for i = 1:width
for j = 1:height
x1 = round(i – s/2);
x2 = round(i + s/2);
y1 = round(j – s/2);
y2 = round(j + s/2);
if x1 width
x1 = width;
end
if x2 > width
x2 = width;
end
if y1 height
y2 = height;
end
if y1 > height
y1 = height;
end
count = (x2 – x1 + 1) * (y2 – y1 + 1);
sumv = intimg(x2,y2) – intimg(x2,y1) – intimg(x1,y2) + intimg(x1,y1);
if inputimage(i,j) * count <= (sumv * (100.0 – t)/100)
outputbinaryimage(i,j) = 0;
else
outputbinaryimage(i,j) = 255;
end
end
end