30 lines
755 B
Go
30 lines
755 B
Go
package filter
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
)
|
|
|
|
func BoxBlur(src *image.Gray, r int) *image.Gray {
|
|
b := src.Bounds()
|
|
out := image.NewGray(b)
|
|
|
|
for y := b.Min.Y; y < b.Max.Y; y++ {
|
|
for x := b.Min.X; x < b.Max.X; x++ {
|
|
var sum, n float64
|
|
for dy := -r; dy <= r; dy++ {
|
|
for dx := -r; dx <= r; dx++ {
|
|
nx, ny := x+dx, y+dy
|
|
if nx >= b.Min.X && nx < b.Max.X &&
|
|
ny >= b.Min.Y && ny < b.Max.Y {
|
|
sum += float64(src.GrayAt(nx, ny).Y)
|
|
n++
|
|
}
|
|
}
|
|
}
|
|
out.SetGray(x, y, color.Gray{Y: uint8(sum / n)})
|
|
}
|
|
}
|
|
return out
|
|
}
|