-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathexample.go
More file actions
53 lines (46 loc) · 1.21 KB
/
example.go
File metadata and controls
53 lines (46 loc) · 1.21 KB
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
package main
import "traceskeleton"
import (
"image/png"
"os"
"log"
"time"
"fmt"
"io/ioutil"
)
// read png image using stdlib
func readImage(path string) ([]uint8, int, int){
reader, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
m, err := png.Decode(reader)
if err != nil {
log.Fatal(err)
}
bounds := m.Bounds()
var w = bounds.Max.X - bounds.Min.X;
var h = bounds.Max.Y - bounds.Min.Y;
var im = make([]uint8, w*h)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, _, _, _ := m.At(x, y).RGBA()
if r >= 32768 {
im[w*y+x] = 1
}else{
im[w*y+x] = 0
}
}
}
return im,w,h
}
func main() {
var im, w, h = readImage(os.Args[1]);
traceskeleton.ThinningZS(im,w,h); // do raster thinning first
start := time.Now()
var p = traceskeleton.TraceSkeleton(im,w,h,0,0,w,h,10,999); // trace to polylines
fmt.Println(time.Since(start))
// save the result as scalable vector graphics
ioutil.WriteFile("out.svg", []byte(traceskeleton.PolylinesToSvg(p,w,h)), 0644);
}