-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedges.go
41 lines (35 loc) · 925 Bytes
/
edges.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
package main
import (
"image"
"image/color"
// "fmt"
)
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func clampedFilter(c color.Color) int {
if ColorIsGood(c) { return 255 }
return 0
}
func Sobel(imgP *image.Image, pixels <-chan *image.Point, edge chan<- image.Point) {
img := *imgP
// sent := 0
for pixel := range pixels {
x,y := pixel.X,pixel.Y
gx := clampedFilter(img.At(x-1,y-1))+2*clampedFilter(img.At(x,y-1))+clampedFilter(img.At(x+1,y-1)) -
clampedFilter(img.At(x-1,y+1))-2*clampedFilter(img.At(x,y+1))-clampedFilter(img.At(x+1,y+1))
gy := -clampedFilter(img.At(x-1,y-1))+clampedFilter(img.At(x+1,y-1))-
2*clampedFilter(img.At(x-1,y))+2*clampedFilter(img.At(x+1,y))-
clampedFilter(img.At(x-1,y+1))+clampedFilter(img.At(x+1,y+1))
g := abs(gx)+abs(gy)
if g >= 255 {
edge <-*pixel
// sent++
}
}
// fmt.Printf("Sent %d times in edges.Sobel\n", sent)
close(edge)
}