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 /sauvola.go | |
parent | 77461fa5f0b0e8fb00d9c04dd9f0a62173f310ce (diff) |
Improve sauvola by ensuring threshold is rounded correctly before comparison
Diffstat (limited to 'sauvola.go')
-rw-r--r-- | sauvola.go | 10 |
1 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}) |