From 859112b5e1516236b36e4faaa95ab5e4029c2f6c Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 28 Mar 2019 10:45:52 +0000 Subject: Improve tests; test regular sauvola, and add option to update golden files --- binarize/sauvola_test.go | 38 +++++++++++++++++++++++------ binarize/testdata/pg1_sauvola_k0.3_w19.png | Bin 0 -> 19447 bytes binarize/testdata/pg1_sauvola_k0.5_w19.png | Bin 0 -> 18231 bytes binarize/testdata/pg1_sauvola_k0.5_w41.png | Bin 0 -> 18275 bytes 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 binarize/testdata/pg1_sauvola_k0.3_w19.png create mode 100644 binarize/testdata/pg1_sauvola_k0.5_w19.png create mode 100644 binarize/testdata/pg1_sauvola_k0.5_w41.png 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 Binary files /dev/null and b/binarize/testdata/pg1_sauvola_k0.3_w19.png 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 Binary files /dev/null and b/binarize/testdata/pg1_sauvola_k0.5_w19.png 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 Binary files /dev/null and b/binarize/testdata/pg1_sauvola_k0.5_w41.png differ -- cgit v1.2.1-24-ge1ad