-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathblur.go
143 lines (128 loc) · 3.35 KB
/
blur.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package imgo
import (
"github.com/BurntSushi/graphics-go/graphics"
"image"
"image/color"
"sync"
)
// GaussianBlur returns a blurred image.
// ksize is Gaussian kernel size
// sigma is Gaussian kernel standard deviation.
func (i *Image) GaussianBlur(ksize int, sigma float64) *Image {
if i.Error != nil {
return i
}
dst := image.NewRGBA(i.image.Bounds())
err := graphics.Blur(dst, i.image, &graphics.BlurOptions{
StdDev: sigma,
Size: ksize,
})
if err != nil {
i.addError(err)
return i
}
i.image = dst
i.width = dst.Bounds().Dx()
i.height = dst.Bounds().Dy()
return i
}
// normalizeKernel normalizes a kernel.
func (i Image) normalizeKernel(kernel [][]float64) {
var sum float64
for _, row := range kernel {
for _, value := range row {
sum += value
}
}
for i, row := range kernel {
for j, value := range row {
kernel[i][j] = value / sum
}
}
}
// Blur returns a blurred image.
// ksize is filter kernel size, it must be a odd number.
func (i *Image) Blur(ksize int) *Image {
if i.Error != nil {
return i
}
if ksize < 2 {
return i
}
if ksize%2 == 0 {
ksize++
}
kernel := make([][]float64, ksize)
for p := range kernel {
row := make([]float64, ksize)
for q := 0; q < ksize; q++ {
row[q] = 1
}
kernel[p] = row
}
i.imageFilterFast(kernel)
return i
}
// imageFilterFast returns a filtered image with Goroutine.
func (i *Image) imageFilterFast(kernel [][]float64) *Image {
i.normalizeKernel(kernel)
dst := image.NewRGBA(i.image.Bounds())
kernelSize := len(kernel)
radius := (kernelSize - 1) / 2
wg := sync.WaitGroup{}
convolution := func(x, y int, wg *sync.WaitGroup) {
defer wg.Done()
var sumR, sumG, sumB, sumA uint16
for p := -radius; p <= radius; p++ {
for q := -radius; q <= radius; q++ {
trueX := x + q
trueY := y + p
if image.Pt(trueX, trueY).In(i.Bounds()) {
thisR, thisG, thisB, thisA := i.image.At(trueX, trueY).RGBA()
sumR += uint16(float64(thisR) * kernel[q+radius][p+radius])
sumG += uint16(float64(thisG) * kernel[q+radius][p+radius])
sumB += uint16(float64(thisB) * kernel[q+radius][p+radius])
sumA += uint16(float64(thisA) * kernel[q+radius][p+radius])
}
}
}
dst.SetRGBA64(x, y, color.RGBA64{R: sumR, G: sumG, B: sumB, A: sumA})
}
wg.Add(i.width * i.height)
for y := 0; y < i.height; y++ {
for x := 0; x < i.width; x++ {
go convolution(x, y, &wg)
}
}
wg.Wait()
i.image = dst
return i
}
// imageFilter returns a filtered image.
func (i *Image) imageFilter(kernel [][]float64) *Image {
i.normalizeKernel(kernel)
dst := image.NewNRGBA64(i.image.Bounds())
kernelSize := len(kernel)
radius := (kernelSize - 1) / 2
for y := 0; y < i.height; y++ {
for x := 0; x < i.width; x++ {
var sumR, sumG, sumB, sumA uint16
for p := -radius; p <= radius; p++ {
for q := -radius; q <= radius; q++ {
trueX := x + q
trueY := y + p
if image.Pt(trueX, trueY).In(i.Bounds()) {
thisR, thisG, thisB, thisA := i.image.At(trueX, trueY).RGBA()
sumR += uint16(float64(thisR) * kernel[q+radius][p+radius])
sumG += uint16(float64(thisG) * kernel[q+radius][p+radius])
sumB += uint16(float64(thisB) * kernel[q+radius][p+radius])
sumA += uint16(float64(thisA) * kernel[q+radius][p+radius])
}
}
}
dst.SetRGBA64(x, y, color.RGBA64{R: sumR, G: sumG, B: sumB, A: sumA})
}
}
i.image = Image2RGBA(dst)
return i
}