diff options
author | Nick White <git@njw.name> | 2022-11-17 13:02:42 +0000 |
---|---|---|
committer | Nick White <git@njw.name> | 2022-11-17 13:02:42 +0000 |
commit | b0c91593e0419abbaa6b61eaba95d4934de9a0dc (patch) | |
tree | ce702d541154555f0deb979846e63f79d3ed662f | |
parent | 109aa8b02ed63a4fe3054972f4c79d5c5328f610 (diff) | |
parent | 3475f12095dec658fc2cc86f171d6cfa5cab6f21 (diff) |
-rw-r--r-- | cmd/pggraph/main.go | 38 | ||||
-rw-r--r-- | go.mod | 5 | ||||
-rw-r--r-- | go.sum | 19 | ||||
-rw-r--r-- | sauvola.go | 2 | ||||
-rw-r--r-- | wipesides.go | 10 |
5 files changed, 43 insertions, 31 deletions
diff --git a/cmd/pggraph/main.go b/cmd/pggraph/main.go index 2da5c57..353dce4 100644 --- a/cmd/pggraph/main.go +++ b/cmd/pggraph/main.go @@ -20,12 +20,12 @@ import ( "os" "sort" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart/v2" "rescribe.xyz/integral" "rescribe.xyz/preproc" ) -const usage = `Usage: pggraph [-vertical] [-width] inimg graphname +const usage = `Usage: pggraph [-vertical] [-width] [-v] inimg graphname Creates a graph showing the proportion of black pixels for slices through a binarised image. This is useful to determine @@ -49,14 +49,15 @@ func sideways(img *image.Gray) *image.Gray { return new } -func graph(title string, points map[int]float64, w io.Writer) error { +func graph(title string, points map[int]float64, w io.Writer) (float64, float64, error) { var xvals, yvals []float64 var xs []int var midxvals, midyvals []float64 var midxs []int + var miny, maxy float64 if len(points) < 2 { - return fmt.Errorf("Not enough points to graph, only %d\n", len(points)) + return miny, maxy, fmt.Errorf("Not enough points to graph, only %d\n", len(points)) } for x, _ := range points { @@ -92,6 +93,18 @@ func graph(title string, points map[int]float64, w io.Writer) error { midyvals = append(midyvals, points[x]) } + miny = 100.0 + maxy = 0.0 + for _, x := range midxs { + y := points[x] + if y < miny { + miny = y + } + if y > maxy { + maxy = y + } + } + middleSeries := chart.ContinuousSeries{ XValues: midxvals, YValues: midyvals, @@ -131,6 +144,10 @@ func graph(title string, points map[int]float64, w io.Writer) error { }, YAxis: chart.YAxis{ Name: "Proportion of black pixels", + Range: &chart.ContinuousRange{ + Min: 0.0, + Max: 0.5, + }, }, Series: []chart.Series{ mainSeries, @@ -141,7 +158,7 @@ func graph(title string, points map[int]float64, w io.Writer) error { }, } - return graph.Render(chart.PNG, w) + return miny, maxy, graph.Render(chart.PNG, w) } func main() { @@ -151,6 +168,7 @@ func main() { } vertical := flag.Bool("vertical", false, "Slice image vertically (from top to bottom) rather than horizontally") width := flag.Int("width", 5, "Width of slice in pixels (height if in vertical mode)") + verbose := flag.Bool("v", false, "Print the minimum and maximum values of the middle section to the console") flag.Parse() if flag.NArg() < 2 { flag.Usage() @@ -192,8 +210,16 @@ func main() { if *vertical { title += " (vertical)" } - err = graph(title, points, f) + miny, maxy, err := graph(title, points, f) if err != nil { log.Fatalf("Could not create graph: %v\n", err) } + + if *verbose { + v := "" + if *vertical { + v = " vertical" + } + fmt.Printf("%s %d%s %0.2f %0.2f\n", flag.Arg(0), *width, v, miny, maxy) + } } @@ -3,9 +3,6 @@ module rescribe.xyz/preproc go 1.14 require ( - github.com/blend/go-sdk v1.1.1 // indirect - github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect - github.com/wcharczuk/go-chart v2.0.2-0.20191206192251-962b9abdec2b+incompatible - golang.org/x/image v0.0.0-20200618115811-c13761719519 // indirect + github.com/wcharczuk/go-chart/v2 v2.1.0 rescribe.xyz/integral v0.6.1 ) @@ -1,20 +1,9 @@ -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= -github.com/airbrake/gobrake v3.6.1+incompatible/go.mod h1:wM4gu3Cn0W0K7GUuVWnlXZU11AGBXMILnrdOU8Kn00o= -github.com/blend/go-sdk v1.1.1 h1:R7PcwuIxYvrGc/r9TLLfMpajIboTjqs/HyQouzgJ7mQ= -github.com/blend/go-sdk v1.1.1/go.mod h1:IP1XHXFveOXHRnojRJO7XvqWGqyzevtXND9AdSztAe8= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= -github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/wcharczuk/go-chart v2.0.2-0.20191206192251-962b9abdec2b+incompatible h1:ahpaSRefPekV3gcXot2AOgngIV8WYqzvDyFe3i7W24w= -github.com/wcharczuk/go-chart v2.0.2-0.20191206192251-962b9abdec2b+incompatible/go.mod h1:PF5tmL4EIx/7Wf+hEkpCqYi5He4u90sw+0+6FhrryuE= -golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34= -golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I= +github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA= +golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckHoFxwt8USGY1ajP1ZF5lM= +golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20181205014116-22934f0fdb62/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= rescribe.xyz/integral v0.6.1 h1:0+4lajevAWO33sxnS33+URUbSalAh2Diti7ZWV/DbqU= rescribe.xyz/integral v0.6.1/go.mod h1:gKJq4UaVn17RsMsUasEMcJDkTkwqeb6AzPIJtwcUipg= @@ -80,5 +80,5 @@ func PreCalcedSauvola(intImg integral.Image, intSqImg integral.SqImage, img imag func centeredRectangle(x, y, size int) image.Rectangle { step := size / 2 - return image.Rect(x - step - 1, y - step - 1, x + step, y + step) + return image.Rect(x-step-1, y-step-1, x+step, y+step) } diff --git a/wipesides.go b/wipesides.go index b9f5e5c..316d0b1 100644 --- a/wipesides.go +++ b/wipesides.go @@ -22,12 +22,12 @@ import ( // ProportionSlice returns the proportion of black pixels in a // vertical slice of an image starting at x, width pixels wide. func ProportionSlice(i SummableImage, x int, width int) float64 { - r := image.Rect(x, 0, x + width, i.Bounds().Dy()) + r := image.Rect(x, 0, x+width, i.Bounds().Dy()) in := r.Intersect(i.Bounds()) area := in.Dx() * in.Dy() // 1 << 16 - 1 as we're using Gray16, so 1 << 16 - 1 = white - numwhite := float64(i.Sum(in)) / float64(1 << 16 - 1) - return float64(area) / float64(numwhite) - 1 + numwhite := float64(i.Sum(in)) / float64(1<<16-1) + return float64(area)/float64(numwhite) - 1 } // findbestedge goes through every vertical line from x to x+w to @@ -99,7 +99,7 @@ func findedgesOutin(img SummableImage, wsize int, thresh float64) (int, int) { maxx := img.Bounds().Dx() - 1 var lowedge, highedge int = 0, maxx - for x := maxx-wsize; x > 0; x-- { + for x := maxx - wsize; x > 0; x-- { if ProportionSlice(img, x, wsize) > thresh { highedge = findbestedge(img, x, wsize) break @@ -191,7 +191,7 @@ func VWipe(img *image.Gray, wsize int, thresh float64, min int) *image.Gray { intImg := integral.NewImage(b) draw.Draw(intImg, b, rotimg, b.Min, draw.Src) // TODO: test whether there are any places where Outin makes a real difference - lowedge, highedge:= findedgesOutin(*intImg, wsize, thresh) + lowedge, highedge := findedgesOutin(*intImg, wsize, thresh) if toonarrow(img, lowedge, highedge, min) { return img } |