0

I'm trying to implement the Sauvola & Pietaksinen method to perform a binarization in an image via local thresholding.

The method defines the threshold of each pixel (x,y) as T(x,y) = mean(x,y)*[1+k(std(x,y)/R-1)], as in the arcticle ”Adaptive Document Image Binarization”. The mean and the standard deviation are calculated in a neighbourhood of (x,y). k and R are suggested to be 0.5 and 128, respectively.

This is what my code looks like:

filtered = colfilt(image, [n n], "sliding", @(x) (mean(x).*(1+0.5*(std(x)/128 - 1))));
image(image < filtered) = 0;
image(image >= filtered) = 255;

However, for all images I tested, the result is a entirely blank image, which is obviously incorrect. I think I must be misusing some element in the colfilt function, but I'm too newbie at Octave and couldn't find it until now.

Could someone please give me a hand?

Thanks in advance.

gcolucci
  • 438
  • 1
  • 5
  • 21
  • Have you tried to scale the image (`imshow(image,[])`)? I agree with @Andy. You shouldn't overwrite function names (like image). – kkuilla Oct 20 '14 at 11:53

1 Answers1

2

I can't see a problem. You really should include your source and perhaps also your input image and parameter for n. Btw, you shouldn't overwrite function names (like image in your case).

Input image:

Lenna in

pkg load image
img = imread ("lenna256.jpg");
k = 0.5;
R = 128;
n = 5;
filtered = colfilt(img, [n n], "sliding", @(x) (mean(x).*(1+0.5*(std(x)/128 - 1))));
img(img < filtered) = 0;
img(img >= filtered) = 255;

image (img)
imwrite (img, "lenna_out.png")

which creates

Lenna out

Andy
  • 7,931
  • 4
  • 25
  • 45
  • Thanks for pointing it out! At the end of the day, I think it's a problem with the parameter values, specially the R. I was able to improve the results by ignoring the original suggestion of the authors and trying values way different, so it's not an issue with the code. I'll mark it as fixed. – gcolucci Oct 20 '14 at 13:28