From 778fc08e5d4b58d457e1754325eb0ff4127ccf1d Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 13 May 2019 17:41:30 +0100 Subject: Rename cleanup to wipe, and only export main function --- preproc/cmd/cleanup/main.go | 62 --------------------------------------------- preproc/cmd/wipe/main.go | 56 ++++++++++++++++++++++++++++++++++++++++ preproc/wipesides.go | 18 ++++++++++--- preproc/wipesides_test.go | 6 +---- 4 files changed, 71 insertions(+), 71 deletions(-) delete mode 100644 preproc/cmd/cleanup/main.go create mode 100644 preproc/cmd/wipe/main.go (limited to 'preproc') diff --git a/preproc/cmd/cleanup/main.go b/preproc/cmd/cleanup/main.go deleted file mode 100644 index 7ea0c84..0000000 --- a/preproc/cmd/cleanup/main.go +++ /dev/null @@ -1,62 +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/draw" - _ "image/jpeg" - "image/png" - "log" - "os" - - "rescribe.xyz/go.git/binarize" - "rescribe.xyz/go.git/preproc" -) - -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 := preproc.Findedges(integral, *wsize, *thresh) - - clean := preproc.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/preproc/cmd/wipe/main.go b/preproc/cmd/wipe/main.go new file mode 100644 index 0000000..e735a0a --- /dev/null +++ b/preproc/cmd/wipe/main.go @@ -0,0 +1,56 @@ +package main + +// TODO: add minimum size variable (default ~30%?) + +import ( + "flag" + "fmt" + "image" + "image/draw" + _ "image/jpeg" + "image/png" + "log" + "os" + + "rescribe.xyz/go.git/preproc" +) + +func main() { + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: wipe [-t thresh] [-w winsize] inimg outimg\n") + fmt.Fprintf(os.Stderr, "Wipes the sections of an image which are outside the content area.\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) + + clean := preproc.Wipe(gray, *wsize, *thresh) + + 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/preproc/wipesides.go b/preproc/wipesides.go index 2afe1d2..60911cd 100644 --- a/preproc/wipesides.go +++ b/preproc/wipesides.go @@ -6,6 +6,8 @@ package preproc import ( "image" "image/color" + + "rescribe.xyz/go.git/binarize" ) type windowslice struct { @@ -69,10 +71,10 @@ func findbestedge(integral [][]uint64, x int, w int) int { return bestx } -// Findedges finds the edges of the main content, by moving a window of wsize +// 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) { +func findedges(integral [][]uint64, wsize int, thresh float64) (int, int) { maxx := len(integral[0]) - 1 var lowedge, highedge int = 0, maxx @@ -93,9 +95,9 @@ func Findedges(integral [][]uint64, wsize int, thresh float64) (int, int) { return lowedge, highedge } -// Wipesides fills the sections of image not within the boundaries +// 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 { +func wipesides(img *image.Gray, lowedge int, highedge int) *image.Gray { b := img.Bounds() new := image.NewGray(b) @@ -120,3 +122,11 @@ func Wipesides(img *image.Gray, lowedge int, highedge int) *image.Gray { return new } + +// wipe fills the sections of image which fall outside the content +// area with white +func Wipe(img *image.Gray, wsize int, thresh float64) *image.Gray { + integral := binarize.Integralimg(img) + lowedge, highedge := findedges(integral, wsize, thresh) + return wipesides(img, lowedge, highedge) +} diff --git a/preproc/wipesides_test.go b/preproc/wipesides_test.go index 76151fb..b0ada4e 100644 --- a/preproc/wipesides_test.go +++ b/preproc/wipesides_test.go @@ -11,8 +11,6 @@ import ( "image/png" "os" "testing" - - "rescribe.xyz/go.git/binarize" ) var update = flag.Bool("update", false, "update golden files") @@ -79,9 +77,7 @@ func TestWipeSides(t *testing.T) { 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) + actual = Wipe(orig, c.wsize, c.thresh) if *update { f, err := os.Create(c.golden) defer f.Close() -- cgit v1.2.1-24-ge1ad