summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.name>2020-03-03 18:18:36 +0000
committerNick White <git@njw.name>2020-03-03 18:18:36 +0000
commit60071c2ea95e284f6c765528e81b930fe3fb4b92 (patch)
treeddd043afb8a97d94acee342a0f5e95dbee156cf3
parent83f6144cfe0e91df991e107e66b73d9e037ea969 (diff)
Add experimental vertical wiping feature to wiper
-rw-r--r--cmd/wipe/main.go14
-rw-r--r--wipesides.go26
2 files changed, 35 insertions, 5 deletions
diff --git a/cmd/wipe/main.go b/cmd/wipe/main.go
index d4c95ac..beeced2 100644
--- a/cmd/wipe/main.go
+++ b/cmd/wipe/main.go
@@ -19,13 +19,16 @@ import (
func main() {
flag.Usage = func() {
- fmt.Fprintf(os.Stderr, "Usage: wipe [-m minperc] [-t thresh] [-w winsize] inimg outimg\n")
+ fmt.Fprintf(os.Stderr, "Usage: wipe inimg outimg\n")
fmt.Fprintf(os.Stderr, "Wipes the sections of an image which are outside the content area.\n")
flag.PrintDefaults()
}
- min := flag.Int("m", 30, "Minimum percentage of the image width for the content width calculation to be considered valid.")
- thresh := flag.Float64("t", 0.05, "Threshold for the proportion of black pixels below which a window is determined to be the edge. Higher means more aggressive wiping.")
- wsize := flag.Int("w", 5, "Window size for mask finding algorithm.")
+ min := flag.Int("hm", 30, "Minimum percentage of the image width for the content width calculation to be considered valid.")
+ thresh := flag.Float64("ht", 0.05, "Threshold for the proportion of black pixels below which a window is determined to be the edge. Higher means more aggressive wiping.")
+ wsize := flag.Int("hw", 5, "Window size for mask finding algorithm.")
+ vmin := flag.Int("vm", 30, "Minimum percentage of the image height for the content width calculation to be considered valid.")
+ vthresh := flag.Float64("vt", 0.005, "Threshold for the proportion of black pixels below which a vertical wipe window is determined to be the edge. Higher means more aggressive wiping.")
+ vwsize := flag.Int("vw", 120, "Window size for vertical mask finding algorithm. Should be set to approximately line height + largest expected gap.")
flag.Parse()
if flag.NArg() < 2 {
flag.Usage()
@@ -45,7 +48,8 @@ func main() {
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, *min)
+ sidesdone := preproc.Wipe(gray, *wsize, *thresh, *min)
+ clean := preproc.VWipe(sidesdone, *vwsize, *vthresh, *vmin)
f, err = os.Create(flag.Arg(1))
if err != nil {
diff --git a/wipesides.go b/wipesides.go
index 3bedf67..0fc4a32 100644
--- a/wipesides.go
+++ b/wipesides.go
@@ -116,6 +116,19 @@ func toonarrow(img *image.Gray, lowedge int, highedge int, min int) bool {
return false
}
+// func sideways flips an image by sideways
+func sideways(img *image.Gray) *image.Gray {
+ b := img.Bounds()
+ newb := image.Rect(b.Min.Y, b.Min.X, b.Max.Y, b.Max.X)
+ new := image.NewGray(newb)
+ for x := b.Min.X; x < b.Max.X; x++ {
+ for y := b.Min.Y; y < b.Max.Y; y++ {
+ new.SetGray(y, x, img.GrayAt(x, y))
+ }
+ }
+ return new
+}
+
// Wipe fills the sections of image which fall outside the content
// area with white, providing the content area is above min %
func Wipe(img *image.Gray, wsize int, thresh float64, min int) *image.Gray {
@@ -127,6 +140,19 @@ func Wipe(img *image.Gray, wsize int, thresh float64, min int) *image.Gray {
return wipesides(img, lowedge, highedge)
}
+// VWipe fills the sections of image which fall outside the vertical
+// content area with white, providing the content area is above min %
+func VWipe(img *image.Gray, wsize int, thresh float64, min int) *image.Gray {
+ rotimg := sideways(img)
+ integral := integralimg.ToIntegralImg(rotimg)
+ lowedge, highedge := findedges(integral, wsize, thresh)
+ if toonarrow(img, lowedge, highedge, min) {
+ return img
+ }
+ wiped := wipesides(rotimg, lowedge, highedge)
+ return sideways(wiped)
+}
+
// WipeFile wipes an image file, filling the sections of the image
// which fall outside the content area with white, providing the
// content area is above min %.