/
imaging.go
59 lines (52 loc) · 1.45 KB
/
imaging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package imaging
import (
"github.com/KangSpace/gqrcode/util"
"github.com/disintegration/imaging"
"image"
"image/color"
)
// Using imaging by:
// go get -u github.com/disintegration/imaging
func Resize(src image.Image, newSize int) *image.NRGBA {
return Resize0(src, newSize, newSize)
}
// Resize0 :
func Resize0(src image.Image, width int, height int) *image.NRGBA {
return imaging.Resize(src, width, height, imaging.NearestNeighbor)
}
// Grayscale produces a grayscale version of the image.
func Grayscale(src image.Image) *image.NRGBA {
return imaging.Grayscale(src)
}
// Binarization : To binary image from grayscale image,
func Binarization(src image.Image) *image.NRGBA {
dst := image.NewNRGBA(src.Bounds())
srcW := src.Bounds().Dx()
srcH := src.Bounds().Dy()
//histogram := imaging.Histogram(src)
//threshold := wellnerAdaptiveThreshold()
util.Parallel(0, srcH, func(ys <-chan int) {
for y := range ys {
//i := y * dst.Stride
for x := 0; x < srcW; x++ {
r, g, b, _ := src.At(x, y).RGBA()
rgb := uint8(((r >> 8) + (g >> 8) + (b >> 8)) / 3)
//uint8(threshold)
if rgb > 40 {
rgb = 255
} else {
rgb = 0
}
dst.SetNRGBA(x, y, color.NRGBA{R: rgb, G: rgb, B: rgb, A: 255})
}
}
})
return dst
}
// ImageBinarization To binary image by two step,
// 1. Grayscale image
// 2. Binarization image
func ImageBinarization(src image.Image) *image.NRGBA {
grayImage := Grayscale(src)
return Binarization(grayImage)
}