summaryrefslogtreecommitdiff
path: root/preproc/sauvola.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-05-13 19:23:03 +0100
committerNick White <git@njw.name>2019-05-13 19:23:03 +0100
commit02a6c66eb77a5b455bcf2d0547d2383074eb7e41 (patch)
tree00f2bde1dfa7a1b23e55478309f26ba1b54fdab9 /preproc/sauvola.go
parentd94fb9f7e74aaad335a587030ed2b4ce44c24cbf (diff)
Reorganise image manipulation to separate integral image parts
Also unify everything else under preproc/ Note that the UsefulImg interface should be used by the main functions, to simplify things, but this hasn't been done yet.
Diffstat (limited to 'preproc/sauvola.go')
-rw-r--r--preproc/sauvola.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/preproc/sauvola.go b/preproc/sauvola.go
new file mode 100644
index 0000000..e93ea81
--- /dev/null
+++ b/preproc/sauvola.go
@@ -0,0 +1,55 @@
+package preproc
+
+import (
+ "image"
+ "image/color"
+
+ "rescribe.xyz/go.git/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 {
+ b := img.Bounds()
+ 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)
+ m, dev := meanstddev(window)
+ threshold := m * (1 + ksize * ((dev / 128) - 1))
+ if img.GrayAt(x, y).Y < uint8(threshold) {
+ new.SetGray(x, y, color.Gray{0})
+ } else {
+ new.SetGray(x, y, color.Gray{255})
+ }
+ }
+ }
+
+ return new
+}
+
+// Implements Sauvola's algorithm using Integral Images, see paper
+// "Efficient Implementation of Local Adaptive Thresholding Techniques Using Integral Images"
+// and
+// https://stackoverflow.com/questions/13110733/computing-image-integral
+func IntegralSauvola(img *image.Gray, ksize float64, windowsize int) *image.Gray {
+ b := img.Bounds()
+ new := image.NewGray(b)
+
+ integrals := integralimg.ToAllIntegralImg(img)
+
+ for y := b.Min.Y; y < b.Max.Y; y++ {
+ for x := b.Min.X; x < b.Max.X; x++ {
+ m, dev := integrals.MeanStdDevWindow(x, y, windowsize)
+ threshold := m * (1 + ksize * ((dev / 128) - 1))
+ if img.GrayAt(x, y).Y < uint8(threshold) {
+ new.SetGray(x, y, color.Gray{0})
+ } else {
+ new.SetGray(x, y, color.Gray{255})
+ }
+ }
+ }
+
+ return new
+}