From 6a36400351bea5052431bb1feace358fa67a5cf9 Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 23 Jul 2020 23:04:49 +0100 Subject: Improve sauvola by ensuring threshold is rounded correctly before comparison --- sauvola.go | 10 +++++++--- testdata/pg1_sauvola_k0.3_w19.png | Bin 19447 -> 19512 bytes testdata/pg1_sauvola_k0.5_w19.png | Bin 18231 -> 18354 bytes testdata/pg1_sauvola_k0.5_w41.png | Bin 18275 -> 18275 bytes 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sauvola.go b/sauvola.go index bea78ce..ee7c870 100644 --- a/sauvola.go +++ b/sauvola.go @@ -7,22 +7,26 @@ package preproc import ( "image" "image/color" + "image/draw" + "math" "rescribe.xyz/integralimg" ) // Implements Sauvola's algorithm for text binarization, see paper // "Adaptive document image binarization" (2000) -func Sauvola(img *image.Gray, ksize float64, windowsize int) *image.Gray { +func Sauvola(img image.Image, ksize float64, windowsize int) *image.Gray { b := img.Bounds() + gray := image.NewGray(b) + draw.Draw(gray, b, img, b.Min, draw.Src) new := image.NewGray(b) for y := b.Min.Y; y < b.Max.Y; y++ { for x := b.Min.X; x < b.Max.X; x++ { - window := surrounding(img, x, y, windowsize) + window := surrounding(gray, x, y, windowsize) m, dev := meanstddev(window) threshold := m * (1 + ksize*((dev/128)-1)) - if img.GrayAt(x, y).Y < uint8(threshold) { + if gray.GrayAt(x, y).Y < uint8(math.Round(threshold)) { new.SetGray(x, y, color.Gray{0}) } else { new.SetGray(x, y, color.Gray{255}) diff --git a/testdata/pg1_sauvola_k0.3_w19.png b/testdata/pg1_sauvola_k0.3_w19.png index bcd595f..e964b19 100644 Binary files a/testdata/pg1_sauvola_k0.3_w19.png and b/testdata/pg1_sauvola_k0.3_w19.png differ diff --git a/testdata/pg1_sauvola_k0.5_w19.png b/testdata/pg1_sauvola_k0.5_w19.png index 8de596c..780fc4f 100644 Binary files a/testdata/pg1_sauvola_k0.5_w19.png and b/testdata/pg1_sauvola_k0.5_w19.png differ diff --git a/testdata/pg1_sauvola_k0.5_w41.png b/testdata/pg1_sauvola_k0.5_w41.png index b8f50e0..acce4f3 100644 Binary files a/testdata/pg1_sauvola_k0.5_w41.png and b/testdata/pg1_sauvola_k0.5_w41.png differ -- cgit v1.2.1-24-ge1ad