summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.name>2020-07-23 23:04:49 +0100
committerNick White <git@njw.name>2020-07-23 23:04:49 +0100
commit6a36400351bea5052431bb1feace358fa67a5cf9 (patch)
tree3e5eaf5db7992ea73f3ba0ccbdddfb25a829ea86
parent77461fa5f0b0e8fb00d9c04dd9f0a62173f310ce (diff)
Improve sauvola by ensuring threshold is rounded correctly before comparison
-rw-r--r--sauvola.go10
-rw-r--r--testdata/pg1_sauvola_k0.3_w19.pngbin19447 -> 19512 bytes
-rw-r--r--testdata/pg1_sauvola_k0.5_w19.pngbin18231 -> 18354 bytes
-rw-r--r--testdata/pg1_sauvola_k0.5_w41.pngbin18275 -> 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
--- a/testdata/pg1_sauvola_k0.3_w19.png
+++ b/testdata/pg1_sauvola_k0.3_w19.png
Binary files differ
diff --git a/testdata/pg1_sauvola_k0.5_w19.png b/testdata/pg1_sauvola_k0.5_w19.png
index 8de596c..780fc4f 100644
--- a/testdata/pg1_sauvola_k0.5_w19.png
+++ b/testdata/pg1_sauvola_k0.5_w19.png
Binary files differ
diff --git a/testdata/pg1_sauvola_k0.5_w41.png b/testdata/pg1_sauvola_k0.5_w41.png
index b8f50e0..acce4f3 100644
--- a/testdata/pg1_sauvola_k0.5_w41.png
+++ b/testdata/pg1_sauvola_k0.5_w41.png
Binary files differ