From 6b7cc0cd671db9c8ff89eea1686099c48dba412a Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 13 May 2019 17:28:38 +0100 Subject: Make cleanup a basic library --- cleanup/main.go | 175 ---------------------------------------------- cleanup/main_test.go | 105 ---------------------------- cleanup/wipesides.go | 122 ++++++++++++++++++++++++++++++++ cleanup/wipesides_test.go | 105 ++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+), 280 deletions(-) delete mode 100644 cleanup/main.go delete mode 100644 cleanup/main_test.go create mode 100644 cleanup/wipesides.go create mode 100644 cleanup/wipesides_test.go diff --git a/cleanup/main.go b/cleanup/main.go deleted file mode 100644 index fdec6e9..0000000 --- a/cleanup/main.go +++ /dev/null @@ -1,175 +0,0 @@ -package main - -// TODO: add minimum size variable (default ~30%?) -// TODO: make into a small library -// TODO: have the integral image specific stuff done by interface functions - -import ( - "flag" - "fmt" - "image" - "image/color" - "image/draw" - _ "image/jpeg" - "image/png" - "log" - "os" - - "rescribe.xyz/go.git/binarize" -) - -type windowslice struct { - topleft uint64 - topright uint64 - bottomleft uint64 - bottomright uint64 -} - -func getwindowslice(i [][]uint64, x int, size int) windowslice { - maxy := len(i) - 1 - maxx := x + size - if maxx > len(i[0])-1 { - maxx = len(i[0]) - 1 - } - - return windowslice{i[0][x], i[0][maxx], i[maxy][x], i[maxy][maxx]} -} - -// checkwindow checks the window from x to see whether more than -// thresh proportion of the pixels are white, if so it returns true. -func checkwindow(integral [][]uint64, x int, size int, thresh float64) bool { - height := len(integral) - window := getwindowslice(integral, x, size) - // divide by 255 as each on pixel has the value of 255 - sum := (window.bottomright + window.topleft - window.topright - window.bottomleft) / 255 - area := size * height - proportion := float64(area)/float64(sum) - 1 - return proportion <= thresh -} - -// returns the proportion of the given window that is black pixels -func proportion(integral [][]uint64, x int, size int) float64 { - height := len(integral) - window := getwindowslice(integral, x, size) - // divide by 255 as each on pixel has the value of 255 - sum := (window.bottomright + window.topleft - window.topright - window.bottomleft) / 255 - area := size * height - return float64(area)/float64(sum) - 1 -} - -// wipesides fills the sections of image not within the boundaries -// of lowedge and highedge with white -func wipesides(img *image.Gray, lowedge int, highedge int) *image.Gray { - b := img.Bounds() - new := image.NewGray(b) - - // set left edge white - for x := b.Min.X; x < lowedge; x++ { - for y := b.Min.Y; y < b.Max.Y; y++ { - new.SetGray(x, y, color.Gray{255}) - } - } - // copy middle - for x := lowedge; x < highedge; x++ { - for y := b.Min.Y; y < b.Max.Y; y++ { - new.SetGray(x, y, img.GrayAt(x, y)) - } - } - // set right edge white - for x := highedge; x < b.Max.X; x++ { - for y := b.Min.Y; y < b.Max.Y; y++ { - new.SetGray(x, y, color.Gray{255}) - } - } - - return new -} - -// findbestedge goes through every vertical line from x to x+w to -// find the one with the lowest proportion of black pixels. -func findbestedge(integral [][]uint64, x int, w int) int { - var bestx int - var best float64 - - if w == 1 { - return x - } - - right := x + w - for ; x < right; x++ { - prop := proportion(integral, x, 1) - if prop > best { - best = prop - bestx = x - } - } - - return bestx -} - -// findedges finds the edges of the main content, by moving a window of wsize -// from the middle of the image to the left and right, stopping when it reaches -// a point at which there is a lower proportion of black pixels than thresh. -func findedges(integral [][]uint64, wsize int, thresh float64) (int, int) { - maxx := len(integral[0]) - 1 - var lowedge, highedge int = 0, maxx - - for x := maxx / 2; x < maxx-wsize; x++ { - if checkwindow(integral, x, wsize, thresh) { - highedge = findbestedge(integral, x, wsize) - break - } - } - - for x := maxx / 2; x > 0; x-- { - if checkwindow(integral, x, wsize, thresh) { - lowedge = findbestedge(integral, x, wsize) - break - } - } - - return lowedge, highedge -} - -func main() { - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: cleanup [-t thresh] [-w winsize] inimg outimg\n") - flag.PrintDefaults() - } - wsize := flag.Int("w", 5, "Window size for mask finding algorithm.") - thresh := flag.Float64("t", 0.05, "Threshold for the proportion of black pixels below which a window is determined to be the edge.") - flag.Parse() - if flag.NArg() < 2 { - flag.Usage() - os.Exit(1) - } - - f, err := os.Open(flag.Arg(0)) - defer f.Close() - if err != nil { - log.Fatalf("Could not open file %s: %v\n", flag.Arg(0), err) - } - img, _, err := image.Decode(f) - if err != nil { - log.Fatalf("Could not decode image: %v\n", err) - } - b := img.Bounds() - gray := image.NewGray(image.Rect(0, 0, b.Dx(), b.Dy())) - draw.Draw(gray, b, img, b.Min, draw.Src) - - integral := binarize.Integralimg(gray) - - lowedge, highedge := findedges(integral, *wsize, *thresh) - - clean := wipesides(gray, lowedge, highedge) - - f, err = os.Create(flag.Arg(1)) - if err != nil { - log.Fatalf("Could not create file %s: %v\n", flag.Arg(1), err) - } - defer f.Close() - err = png.Encode(f, clean) - if err != nil { - log.Fatalf("Could not encode image: %v\n", err) - } -} diff --git a/cleanup/main_test.go b/cleanup/main_test.go deleted file mode 100644 index 3c3ce0c..0000000 --- a/cleanup/main_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package main - -// TODO: add different pages as test cases -// TODO: test non integral img version - -import ( - "flag" - "fmt" - "image" - "image/draw" - "image/png" - "os" - "testing" - - "rescribe.xyz/go.git/binarize" -) - -var update = flag.Bool("update", false, "update golden files") - -func decode(s string) (*image.Gray, error) { - f, err := os.Open(s) - defer f.Close() - if err != nil { - return nil, err - } - img, err := png.Decode(f) - if err != nil { - return nil, err - } - b := img.Bounds() - gray := image.NewGray(image.Rect(0, 0, b.Dx(), b.Dy())) - draw.Draw(gray, b, img, b.Min, draw.Src) - return gray, nil -} - -func imgsequal(img1 *image.Gray, img2 *image.Gray) bool { - b := img1.Bounds() - if !b.Eq(img2.Bounds()) { - return false - } - for y := b.Min.Y; y < b.Max.Y; y++ { - for x := b.Min.X; x < b.Max.X; x++ { - r0, g0, b0, a0 := img1.At(x, y).RGBA() - r1, g1, b1, a1 := img2.At(x, y).RGBA() - if r0 != r1 { - return false - } - if g0 != g1 { - return false - } - if b0 != b1 { - return false - } - if a0 != a1 { - return false - } - } - } - return true -} - -func TestWipeSides(t *testing.T) { - cases := []struct { - name string - orig string - golden string - thresh float64 - wsize int - }{ - {"integralwipesides", "testdata/pg1.png", "testdata/pg1_integralwipesides_t0.02_w5.png", 0.02, 5}, - {"integralwipesides", "testdata/pg1.png", "testdata/pg1_integralwipesides_t0.05_w5.png", 0.05, 5}, - {"integralwipesides", "testdata/pg1.png", "testdata/pg1_integralwipesides_t0.05_w25.png", 0.05, 25}, - } - - for _, c := range cases { - t.Run(fmt.Sprintf("%s_%0.2f_%d", c.name, c.thresh, c.wsize), func(t *testing.T) { - var actual *image.Gray - orig, err := decode(c.orig) - if err != nil { - t.Fatalf("Could not open file %s: %v\n", c.orig, err) - } - integral := binarize.Integralimg(orig) - lowedge, highedge := findedges(integral, c.wsize, c.thresh) - actual = wipesides(orig, lowedge, highedge) - if *update { - f, err := os.Create(c.golden) - defer f.Close() - if err != nil { - t.Fatalf("Could not open file %s to update: %v\n", c.golden, err) - } - err = png.Encode(f, actual) - if err != nil { - t.Fatalf("Could not encode update of %s: %v\n", c.golden, err) - } - } - golden, err := decode(c.golden) - if err != nil { - t.Fatalf("Could not open file %s: %v\n", c.golden, err) - } - if !imgsequal(golden, actual) { - t.Errorf("Processed %s differs to %s\n", c.orig, c.golden) - } - }) - } -} diff --git a/cleanup/wipesides.go b/cleanup/wipesides.go new file mode 100644 index 0000000..ce3b374 --- /dev/null +++ b/cleanup/wipesides.go @@ -0,0 +1,122 @@ +package cleanup + +// TODO: add minimum size variable (default ~30%?) +// TODO: have the integral image specific stuff done by interface functions + +import ( + "image" + "image/color" +) + +type windowslice struct { + topleft uint64 + topright uint64 + bottomleft uint64 + bottomright uint64 +} + +func getwindowslice(i [][]uint64, x int, size int) windowslice { + maxy := len(i) - 1 + maxx := x + size + if maxx > len(i[0])-1 { + maxx = len(i[0]) - 1 + } + + return windowslice{i[0][x], i[0][maxx], i[maxy][x], i[maxy][maxx]} +} + +// checkwindow checks the window from x to see whether more than +// thresh proportion of the pixels are white, if so it returns true. +func checkwindow(integral [][]uint64, x int, size int, thresh float64) bool { + height := len(integral) + window := getwindowslice(integral, x, size) + // divide by 255 as each on pixel has the value of 255 + sum := (window.bottomright + window.topleft - window.topright - window.bottomleft) / 255 + area := size * height + proportion := float64(area)/float64(sum) - 1 + return proportion <= thresh +} + +// returns the proportion of the given window that is black pixels +func proportion(integral [][]uint64, x int, size int) float64 { + height := len(integral) + window := getwindowslice(integral, x, size) + // divide by 255 as each on pixel has the value of 255 + sum := (window.bottomright + window.topleft - window.topright - window.bottomleft) / 255 + area := size * height + return float64(area)/float64(sum) - 1 +} + +// findbestedge goes through every vertical line from x to x+w to +// find the one with the lowest proportion of black pixels. +func findbestedge(integral [][]uint64, x int, w int) int { + var bestx int + var best float64 + + if w == 1 { + return x + } + + right := x + w + for ; x < right; x++ { + prop := proportion(integral, x, 1) + if prop > best { + best = prop + bestx = x + } + } + + return bestx +} + +// Findedges finds the edges of the main content, by moving a window of wsize +// from the middle of the image to the left and right, stopping when it reaches +// a point at which there is a lower proportion of black pixels than thresh. +func Findedges(integral [][]uint64, wsize int, thresh float64) (int, int) { + maxx := len(integral[0]) - 1 + var lowedge, highedge int = 0, maxx + + for x := maxx / 2; x < maxx-wsize; x++ { + if checkwindow(integral, x, wsize, thresh) { + highedge = findbestedge(integral, x, wsize) + break + } + } + + for x := maxx / 2; x > 0; x-- { + if checkwindow(integral, x, wsize, thresh) { + lowedge = findbestedge(integral, x, wsize) + break + } + } + + return lowedge, highedge +} + +// Wipesides fills the sections of image not within the boundaries +// of lowedge and highedge with white +func Wipesides(img *image.Gray, lowedge int, highedge int) *image.Gray { + b := img.Bounds() + new := image.NewGray(b) + + // set left edge white + for x := b.Min.X; x < lowedge; x++ { + for y := b.Min.Y; y < b.Max.Y; y++ { + new.SetGray(x, y, color.Gray{255}) + } + } + // copy middle + for x := lowedge; x < highedge; x++ { + for y := b.Min.Y; y < b.Max.Y; y++ { + new.SetGray(x, y, img.GrayAt(x, y)) + } + } + // set right edge white + for x := highedge; x < b.Max.X; x++ { + for y := b.Min.Y; y < b.Max.Y; y++ { + new.SetGray(x, y, color.Gray{255}) + } + } + + return new +} diff --git a/cleanup/wipesides_test.go b/cleanup/wipesides_test.go new file mode 100644 index 0000000..aa3e590 --- /dev/null +++ b/cleanup/wipesides_test.go @@ -0,0 +1,105 @@ +package cleanup + +// TODO: add different pages as test cases +// TODO: test non integral img version + +import ( + "flag" + "fmt" + "image" + "image/draw" + "image/png" + "os" + "testing" + + "rescribe.xyz/go.git/binarize" +) + +var update = flag.Bool("update", false, "update golden files") + +func decode(s string) (*image.Gray, error) { + f, err := os.Open(s) + defer f.Close() + if err != nil { + return nil, err + } + img, err := png.Decode(f) + if err != nil { + return nil, err + } + b := img.Bounds() + gray := image.NewGray(image.Rect(0, 0, b.Dx(), b.Dy())) + draw.Draw(gray, b, img, b.Min, draw.Src) + return gray, nil +} + +func imgsequal(img1 *image.Gray, img2 *image.Gray) bool { + b := img1.Bounds() + if !b.Eq(img2.Bounds()) { + return false + } + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + r0, g0, b0, a0 := img1.At(x, y).RGBA() + r1, g1, b1, a1 := img2.At(x, y).RGBA() + if r0 != r1 { + return false + } + if g0 != g1 { + return false + } + if b0 != b1 { + return false + } + if a0 != a1 { + return false + } + } + } + return true +} + +func TestWipeSides(t *testing.T) { + cases := []struct { + name string + orig string + golden string + thresh float64 + wsize int + }{ + {"integralwipesides", "testdata/pg1.png", "testdata/pg1_integralwipesides_t0.02_w5.png", 0.02, 5}, + {"integralwipesides", "testdata/pg1.png", "testdata/pg1_integralwipesides_t0.05_w5.png", 0.05, 5}, + {"integralwipesides", "testdata/pg1.png", "testdata/pg1_integralwipesides_t0.05_w25.png", 0.05, 25}, + } + + for _, c := range cases { + t.Run(fmt.Sprintf("%s_%0.2f_%d", c.name, c.thresh, c.wsize), func(t *testing.T) { + var actual *image.Gray + orig, err := decode(c.orig) + if err != nil { + t.Fatalf("Could not open file %s: %v\n", c.orig, err) + } + integral := binarize.Integralimg(orig) + lowedge, highedge := Findedges(integral, c.wsize, c.thresh) + actual = Wipesides(orig, lowedge, highedge) + if *update { + f, err := os.Create(c.golden) + defer f.Close() + if err != nil { + t.Fatalf("Could not open file %s to update: %v\n", c.golden, err) + } + err = png.Encode(f, actual) + if err != nil { + t.Fatalf("Could not encode update of %s: %v\n", c.golden, err) + } + } + golden, err := decode(c.golden) + if err != nil { + t.Fatalf("Could not open file %s: %v\n", c.golden, err) + } + if !imgsequal(golden, actual) { + t.Errorf("Processed %s differs to %s\n", c.orig, c.golden) + } + }) + } +} -- cgit v1.2.1-24-ge1ad