From 8af4c7e56e0336f1e70a0e079c79ee7459d92519 Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 26 Mar 2019 17:23:33 +0000 Subject: Add zeroinv option for binarize command --- binarize/util.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'binarize/util.go') diff --git a/binarize/util.go b/binarize/util.go index e7fcfe4..ad641c9 100644 --- a/binarize/util.go +++ b/binarize/util.go @@ -1,6 +1,7 @@ package binarize import ( + "errors" "image" "math" ) @@ -65,3 +66,22 @@ func surrounding(img *image.Gray, x int, y int, size int) []int { } return s } + +func BinToZeroInv(bin *image.Gray, orig *image.RGBA) (*image.RGBA, error) { + b := bin.Bounds() + if ! b.Eq(orig.Bounds()) { + return orig, errors.New("bin and orig images need to be the same dimensions") + } + newimg := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + if bin.GrayAt(x, y).Y == 255 { + newimg.Set(x, y, bin.GrayAt(x, y)) + } else { + newimg.Set(x, y, orig.At(x, y)) + } + } + } + + return newimg, nil +} -- cgit v1.2.1-24-ge1ad