summaryrefslogtreecommitdiff
path: root/binarize
diff options
context:
space:
mode:
Diffstat (limited to 'binarize')
-rw-r--r--binarize/sauvola_test.go38
-rw-r--r--binarize/testdata/pg1_sauvola_k0.3_w19.pngbin0 -> 19447 bytes
-rw-r--r--binarize/testdata/pg1_sauvola_k0.5_w19.pngbin0 -> 18231 bytes
-rw-r--r--binarize/testdata/pg1_sauvola_k0.5_w41.pngbin0 -> 18275 bytes
4 files changed, 31 insertions, 7 deletions
diff --git a/binarize/sauvola_test.go b/binarize/sauvola_test.go
index 12a040a..6e767ab 100644
--- a/binarize/sauvola_test.go
+++ b/binarize/sauvola_test.go
@@ -1,6 +1,7 @@
package binarize
import (
+ "flag"
"fmt"
"image"
"image/draw"
@@ -9,6 +10,8 @@ import (
"testing"
)
+var update = flag.Bool("update", false, "update golden files")
+
func decode(s string) (*image.Gray, error) {
f, err := os.Open(s)
defer f.Close()
@@ -52,32 +55,53 @@ func imgsequal(img1 *image.Gray, img2 *image.Gray) bool {
}
func TestBinarization(t *testing.T) {
- // TODO: test non-integral sauvola method too
cases := []struct {
name string
orig string
- done string
+ golden string
ksize float64
wsize int
}{
{"integralsauvola", "testdata/pg1.png", "testdata/pg1_integralsauvola_k0.5_w41.png", 0.5, 41},
{"integralsauvola", "testdata/pg1.png", "testdata/pg1_integralsauvola_k0.5_w19.png", 0.5, 19},
{"integralsauvola", "testdata/pg1.png", "testdata/pg1_integralsauvola_k0.3_w19.png", 0.3, 19},
+ {"sauvola", "testdata/pg1.png", "testdata/pg1_sauvola_k0.5_w41.png", 0.5, 41},
+ {"sauvola", "testdata/pg1.png", "testdata/pg1_sauvola_k0.5_w19.png", 0.5, 19},
+ {"sauvola", "testdata/pg1.png", "testdata/pg1_sauvola_k0.3_w19.png", 0.3, 19},
}
for _, c := range cases {
t.Run(fmt.Sprintf("%s_%0.1f_%d", c.name, c.ksize, c.wsize), func(t *testing.T) {
+ var actual *image.Gray
orig, err := decode(c.orig)
if err != nil {
t.Errorf("Could not open file %s: %v\n", c.orig, err)
}
- done, err := decode(c.done)
+ switch c.name {
+ case "integralsauvola":
+ actual = IntegralSauvola(orig, c.ksize, c.wsize)
+ case "sauvola":
+ actual = Sauvola(orig, c.ksize, c.wsize)
+ default:
+ t.Fatalf("No method %s\n", c.name)
+ }
+ if *update {
+ f, err := os.Create(c.golden)
+ defer f.Close()
+ if err != nil {
+ t.Errorf("Could not open file %s to update: %v\n", c.golden, err)
+ }
+ err = png.Encode(f, actual)
+ if err != nil {
+ t.Errorf("Could not encode update of %s: %v\n", c.golden, err)
+ }
+ }
+ golden, err := decode(c.golden)
if err != nil {
- t.Errorf("Could not open file %s: %v\n", c.done, err)
+ t.Errorf("Could not open file %s: %v\n", c.golden, err)
}
- new := IntegralSauvola(orig, c.ksize, c.wsize)
- if ! imgsequal(done, new) {
- t.Errorf("Binarized %s differs to %s\n", c.orig, c.done)
+ if ! imgsequal(golden, actual) {
+ t.Errorf("Binarized %s differs to %s\n", c.orig, c.golden)
}
})
}
diff --git a/binarize/testdata/pg1_sauvola_k0.3_w19.png b/binarize/testdata/pg1_sauvola_k0.3_w19.png
new file mode 100644
index 0000000..bcd595f
--- /dev/null
+++ b/binarize/testdata/pg1_sauvola_k0.3_w19.png
Binary files differ
diff --git a/binarize/testdata/pg1_sauvola_k0.5_w19.png b/binarize/testdata/pg1_sauvola_k0.5_w19.png
new file mode 100644
index 0000000..8de596c
--- /dev/null
+++ b/binarize/testdata/pg1_sauvola_k0.5_w19.png
Binary files differ
diff --git a/binarize/testdata/pg1_sauvola_k0.5_w41.png b/binarize/testdata/pg1_sauvola_k0.5_w41.png
new file mode 100644
index 0000000..b8f50e0
--- /dev/null
+++ b/binarize/testdata/pg1_sauvola_k0.5_w41.png
Binary files differ