diff options
author | Nick White <git@njw.name> | 2020-07-23 23:04:49 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2020-07-23 23:04:49 +0100 |
commit | 6a36400351bea5052431bb1feace358fa67a5cf9 (patch) | |
tree | 3e5eaf5db7992ea73f3ba0ccbdddfb25a829ea86 | |
parent | 77461fa5f0b0e8fb00d9c04dd9f0a62173f310ce (diff) |
Improve sauvola by ensuring threshold is rounded correctly before comparison
-rw-r--r-- | sauvola.go | 10 | ||||
-rw-r--r-- | testdata/pg1_sauvola_k0.3_w19.png | bin | 19447 -> 19512 bytes | |||
-rw-r--r-- | testdata/pg1_sauvola_k0.5_w19.png | bin | 18231 -> 18354 bytes | |||
-rw-r--r-- | testdata/pg1_sauvola_k0.5_w41.png | bin | 18275 -> 18275 bytes |
4 files changed, 7 insertions, 3 deletions
@@ -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 Binary files differindex bcd595f..e964b19 100644 --- a/testdata/pg1_sauvola_k0.3_w19.png +++ b/testdata/pg1_sauvola_k0.3_w19.png diff --git a/testdata/pg1_sauvola_k0.5_w19.png b/testdata/pg1_sauvola_k0.5_w19.png Binary files differindex 8de596c..780fc4f 100644 --- a/testdata/pg1_sauvola_k0.5_w19.png +++ b/testdata/pg1_sauvola_k0.5_w19.png diff --git a/testdata/pg1_sauvola_k0.5_w41.png b/testdata/pg1_sauvola_k0.5_w41.png Binary files differindex b8f50e0..acce4f3 100644 --- a/testdata/pg1_sauvola_k0.5_w41.png +++ b/testdata/pg1_sauvola_k0.5_w41.png |