forked from esimov/triangle
/
edges.go
65 lines (56 loc) · 1.22 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package triangle
import (
"image"
"math/rand"
"time"
)
const POINT_RATE = 0.875
// Get the edge points after the Sobel filter has been applied.
func GetEdgePoints(img *image.NRGBA, threshold, maxPoints int)[]Point {
rand.Seed(time.Now().UTC().UnixNano())
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
var (
points []Point
sum, total int
x, y, row, col, sx, sy, step int
dpoints []Point
)
for y = 0; y < height; y++ {
for x = 0; x < width; x++ {
sum, total = 0, 0
for row = -1; row <= 1; row++ {
sy = y + row
step = sy * width
if sy >= 0 && sy < height {
for col = -1; col <= 1; col++ {
sx = x + col
if sx >= 0 && sx < width {
sum += int(img.Pix[(sx + step) * 4])
total++
}
}
}
}
if total > 0 {
sum /= total
}
if sum > threshold {
points = append(points, Point{x: x, y: y})
}
}
}
ilen := len(points)
tlen := ilen
limit := int(float64(ilen) * POINT_RATE)
if limit > maxPoints {
limit = maxPoints
}
for i := 0; i < limit && i < ilen; i++ {
j := int(float64(tlen) * rand.Float64())
dpoints = append(dpoints, points[j])
// Remove points
points = append(points[:j], points[j+1:]...)
tlen--
}
return dpoints
}