Skip to content

Instantly share code, notes, and snippets.

@PirosB3
Created May 1, 2014 14:45
Show Gist options
  • Save PirosB3/c27c64cc4642f789d761 to your computer and use it in GitHub Desktop.
Save PirosB3/c27c64cc4642f789d761 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
type Kernel [9]float64
type MaskFunc func(x float64, y float64) float64
func (k *Kernel) BlurCenter() {
k[4] = k.Blur(4)
}
func (k *Kernel) Median() {
for i, _ := range k {
k[i] = k[4]
}
}
func GaussianMask(x float64, y float64) float64 {
return (1.0/ 2 * math.Pi) * math.Pow(math.E, -((math.Pow(x,2) + math.Pow(y,2))/2))
}
func CreateMask(mask MaskFunc) Kernel {
var res Kernel
for x:=-1; x < 2; x++ {
for y:=-1; y < 2; y++ {
kernelRes := mask(float64(x), float64(y))
res[(y+1)*3 + (x+1)] = kernelRes
}
}
return res
}
func (k *Kernel) Blur(n int) float64 {
sum := 0.0
for i, el := range k {
if i != 4 {
sum += el
}
}
return sum/8.0
}
func main() {
k := Kernel{1,2,4,2,4,5,7,5,4}
fmt.Println(k)
k.BlurCenter()
fmt.Println(k)
k.Median()
fmt.Println(k)
fmt.Println(CreateMask(GaussianMask))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment