summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cleanup/wipesides.go (renamed from cleanup/main.go)103
-rw-r--r--cleanup/wipesides_test.go (renamed from cleanup/main_test.go)6
2 files changed, 28 insertions, 81 deletions
diff --git a/cleanup/main.go b/cleanup/wipesides.go
index fdec6e9..ce3b374 100644
--- a/cleanup/main.go
+++ b/cleanup/wipesides.go
@@ -1,21 +1,11 @@
-package main
+package cleanup
// 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 {
@@ -57,34 +47,6 @@ func proportion(integral [][]uint64, x int, size int) float64 {
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 {
@@ -107,10 +69,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
@@ -131,45 +93,30 @@ func findedges(integral [][]uint64, wsize int, thresh float64) (int, int) {
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)
- }
+// 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()
- 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)
+ new := image.NewGray(b)
- f, err = os.Create(flag.Arg(1))
- if err != nil {
- log.Fatalf("Could not create file %s: %v\n", flag.Arg(1), err)
+ // 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})
+ }
}
- defer f.Close()
- err = png.Encode(f, clean)
- if err != nil {
- log.Fatalf("Could not encode image: %v\n", err)
+ // 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/main_test.go b/cleanup/wipesides_test.go
index 3c3ce0c..aa3e590 100644
--- a/cleanup/main_test.go
+++ b/cleanup/wipesides_test.go
@@ -1,4 +1,4 @@
-package main
+package cleanup
// TODO: add different pages as test cases
// TODO: test non integral img version
@@ -80,8 +80,8 @@ func TestWipeSides(t *testing.T) {
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)
+ lowedge, highedge := Findedges(integral, c.wsize, c.thresh)
+ actual = Wipesides(orig, lowedge, highedge)
if *update {
f, err := os.Create(c.golden)
defer f.Close()