Skip to content

Commit

Permalink
go impl
Browse files Browse the repository at this point in the history
  • Loading branch information
LingDong- committed Apr 22, 2020
1 parent d70123d commit e9e379c
Show file tree
Hide file tree
Showing 5 changed files with 677 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

*A new algorithm for retrieving topological skeleton as a set of polylines from binary images.*

[Available in all your favorite languages](#impl): C, C++, Java, JavaScript, Python, C#/Unity, Processing, OpenFrameworks.
[Available in all your favorite languages](#impl): C, C++, Java, JavaScript, Python, Go, C#/Unity, Processing, OpenFrameworks.

**[[Online Demo](https://skeleton-tracing.netlify.app)]**

Expand Down Expand Up @@ -52,6 +52,7 @@ Click on links below to see each implementation's documentation and code.
- [**Java**](java) (includes a Processing demo)
- [**OpenFrameworks addon**](of) (friendly wrapper on C++ version)
- [**C#**](cs) (demo script for Unity Engine)
- [**Go**](go) (parallelized with goroutines)


**Developed at [Frank-Ratchye STUDIO for Creative Inquiry](https://studioforcreativeinquiry.org) at Carnegie Mellon University.**
24 changes: 24 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# traceskeleton.go

Golang version using prallel goroutines.

## Usage

```
import "traceskeleton"
```

See `example.go` for detailed usage.

All source code is in `src/traceskeleton/traceskeleton.go`

## Compile

```
export GO_PATH=$PWD; go build example.go
```

**Note:** This is my "Hello World" in Go, feel free to PR if you feel something could be done more idiomatically!


**Developed at [Frank-Ratchye STUDIO for Creative Inquiry](https://studioforcreativeinquiry.org) at Carnegie Mellon University.**
Binary file added go/example
Binary file not shown.
53 changes: 53 additions & 0 deletions go/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
Loading

0 comments on commit e9e379c

Please sign in to comment.