From 179bb4a20ad93d418e3e911f90f98b6099a3f0ef Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 24 Aug 2020 14:49:50 +0100 Subject: Add verbose option to print min/max values --- cmd/pggraph/main.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/cmd/pggraph/main.go b/cmd/pggraph/main.go index 2da5c57..fd61761 100644 --- a/cmd/pggraph/main.go +++ b/cmd/pggraph/main.go @@ -25,7 +25,7 @@ import ( "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,12 @@ 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 { + fmt.Printf("%s %0.2f %0.2f\n", flag.Arg(0), miny, maxy) + } } -- cgit v1.2.1-24-ge1ad From 34dda838f09cd3c9d95d435e8fa8ac446401843f Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 24 Aug 2020 15:04:42 +0100 Subject: [pggraph] have verbose printing include options it was run with --- cmd/pggraph/main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/pggraph/main.go b/cmd/pggraph/main.go index fd61761..9842efd 100644 --- a/cmd/pggraph/main.go +++ b/cmd/pggraph/main.go @@ -216,6 +216,10 @@ func main() { } if *verbose { - fmt.Printf("%s %0.2f %0.2f\n", flag.Arg(0), miny, maxy) + v := "" + if *vertical { + v = " vertical" + } + fmt.Printf("%s %d%s %0.2f %0.2f\n", flag.Arg(0), *width, v, miny, maxy) } } -- cgit v1.2.1-24-ge1ad From 54510d7c797682d37b1bb0d62d8bb7c3f5951388 Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 12 Apr 2021 14:31:39 +0100 Subject: Update go-chart dependency (should solve issues with bad go-sdk version) --- cmd/pggraph/main.go | 2 +- go.mod | 5 +---- go.sum | 19 ++++--------------- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/cmd/pggraph/main.go b/cmd/pggraph/main.go index 9842efd..353dce4 100644 --- a/cmd/pggraph/main.go +++ b/cmd/pggraph/main.go @@ -20,7 +20,7 @@ import ( "os" "sort" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart/v2" "rescribe.xyz/integral" "rescribe.xyz/preproc" ) diff --git a/go.mod b/go.mod index 0a31aad..858f072 100644 --- a/go.mod +++ b/go.mod @@ -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.0 ) diff --git a/go.sum b/go.sum index dae16b3..49ed04e 100644 --- a/go.sum +++ b/go.sum @@ -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.0 h1:CLF3sQ6th/OuG+/rp/lLR+AGOT4R7tG3IiUjSLKsriw= rescribe.xyz/integral v0.6.0/go.mod h1:gKJq4UaVn17RsMsUasEMcJDkTkwqeb6AzPIJtwcUipg= -- cgit v1.2.1-24-ge1ad From 3475f12095dec658fc2cc86f171d6cfa5cab6f21 Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 23 Jul 2021 16:11:14 +0100 Subject: gofmt --- sauvola.go | 2 +- wipesides.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sauvola.go b/sauvola.go index 75d558c..f92c5ce 100644 --- a/sauvola.go +++ b/sauvola.go @@ -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 } -- cgit v1.2.1-24-ge1ad