This is an implementation of the Suzuki + Abe "Topological Structural Analysis of Digitized Binary Images by Border Following" ( http://pdf.xuebalib.com:1262/xuebalib.com.17233.pdf )
This implementation is based off the original paper as well as being inspired by other papers/implementations/ideas from across the web.
Borders generates a tree of contours/borders which are generated from a monochrome bitmap. The bitmap needs to be either black/0 for background and white/1 for content to be scanned for borders. The result is a tree of borders due to borders can contain inner borders (holes), which can in turn contain other borders etc.
Easiest approach is to have a black and white png file and execute:
To generate contours from one file and save to another:
img,_ := border.LoadImage("test.png")
// contour is root node
contour := border.FindContours(img)
// this will save contours in contour.png
border.SaveContourSliceImage("contour.png", contour, img.Width, img.Height, false, 0)
To generate a geometry of the contours from an image file:
img, _ := border.LoadImage("someimage.png", 1, 1)
cont, _ := border.FindContours(img)
// create a slippy map converter so we can generate a multipolygon that has the latitude/longitude
// geojson.
// This means that we need to know the slippy X/Y coords of the top left corner of the map
slippyX := 1891519.0
slippyY := 1285047.0
slippyConverter := converters.NewSlippyToLatLongConverter(slippyX, slippyY, scale)
// tolerance of 0 means get ConvertContourToPolygon to calculate it
poly, _ := converters.ConvertContourToPolygon(cont, scale, true, 0, 0, true, slippyConverter)
j, _ := poly.MarshalJSON()
fmt.Printf("geojson: %s\n", string(j))
The resulting geojson can be put into any GIS system or viewer (eg geojson.io)
go test ./... -coverprofile=coverage
then
go tool cover -html=coverage
The directory opencv-sanity-check has a small test of test images and the contours generated by OpenCV. The code for this is at github.com/kpfaulkner/opencvtest
This is purely to compare results of Borders vs OpenCV.