Documentation ¶
Overview ¶
Package triangle is an image processing library which converts images to computer generated art using delaunay triangulation.
The package provides a command line utility supporting various customization options. Check the supported commands by typing:
$ triangle --help
Using Go interfaces the API can expose the result either as raster or vector type.
Example to generate triangulated image and output the result as a raster type:
package main import ( "fmt" "github.com/esimov/triangle/v2" ) func main() { p := &triangle.Processor{ // Initialize struct variables } img := &triangle.Image{*p} _, _, _, err := img.Draw(srcImg, p, func() {}) if err != nil { fmt.Printf("Error on triangulation process: %s", err.Error()) } }
Example to generate triangulated image and output the result as SVG:
package main import ( "fmt" "github.com/esimov/triangle/v2" ) func main() { p := &triangle.Processor{ // Initialize struct variables } svg := &triangle.SVG{ Title: "Delaunay image triangulator", Lines: []triangle.Line{}, Description: "Convert images to computer generated art using delaunay triangulation.", StrokeWidth: p.StrokeWidth, StrokeLineCap: "round", //butt, round, square Processor: *p, } _, _, _, err := svg.Draw(srcImg, p, func() { // Call the closure function }) if err != nil { fmt.Printf("Error on triangulation process: %s", err.Error()) } }
Index ¶
- Constants
- func Grayscale(src *image.NRGBA) *image.NRGBA
- func ImgToNRGBA(img image.Image) *image.NRGBA
- func Max[T constraints.Ordered](values ...T) T
- func Min[T constraints.Ordered](values ...T) T
- func Noise(amount int, pxl image.Image, w, h int) *image.NRGBA64
- func SobelFilter(img *image.NRGBA, threshold float64) *image.NRGBA
- func StackBlur(img *image.NRGBA, radius uint32) *image.NRGBA
- type Delaunay
- type Drawer
- type Fn
- type Image
- type Line
- type Node
- type Point
- type Processor
- type SVG
- type Triangle
Constants ¶
const ( // WithoutWireframe - generates triangles without stroke WithoutWireframe = iota // WithWireframe - generates triangles with stroke WithWireframe // WireframeOnly - generates triangles only with wireframe WireframeOnly )
Variables ¶
This section is empty.
Functions ¶
func ImgToNRGBA ¶
ImgToNRGBA converts any image type to *image.NRGBA with min-point at (0, 0).
func Max ¶
func Max[T constraints.Ordered](values ...T) T
Max returns the biggest value between two numbers.
func Min ¶
func Min[T constraints.Ordered](values ...T) T
Min returns the smallest value between two numbers.
func Noise ¶
Noise applies a noise factor, like Adobe's grain filter in order to create a despeckle like image.
func SobelFilter ¶
SobelFilter uses the sobel threshold operator to detect the image edges. See https://en.wikipedia.org/wiki/Sobel_operator
Types ¶
type Delaunay ¶
type Delaunay struct {
// contains filtered or unexported fields
}
Delaunay defines the main components of the triangulation.
func (*Delaunay) GetTriangles ¶
GetTriangles returns the generated triangles.
type Drawer ¶
type Drawer interface {
Draw(image.Image, Processor, Fn) (image.Image, []Triangle, []Point, error)
}
Drawer interface defines the Draw method. This interface should be implemented by every struct which declares a Draw method. By using this method the image can be triangulated as raster type or SVG.
type Image ¶
type Image struct {
Processor
}
Image extends the Processor struct.
func (*Image) DecodeImage ¶
DecodeImage calls the decodeImage utility function which decodes an image file type to the generic image.Image type.
type Node ¶
type Node struct {
X, Y float64
}
Node defines a struct having as components the node X and Y coordinate position.
type Point ¶
type Point struct {
X, Y float64
}
Point defines a struct having as components the point X and Y coordinate position.
type Processor ¶
type Processor struct { // BlurRadius defines the intensity of the applied blur filter. BlurRadius int // SobelThreshold defines the threshold intesinty of the sobel edge detector. // By increasing this value the contours of the detected objects will be more evident. SobelThreshold int // PointsThreshold defines the threshold of computed pixel value below a point is generated. PointsThreshold int // PointRate defines the point rate by which the generated polygons will be multiplied by. // The lower this value the bigger the polygons will be. PointRate float64 // BlurFactor defines the factor used to populate the matrix table in conjunction with the convolution filter operator. // This value will affect the outcome of the final triangulated image. BlurFactor int // EdgeFactor defines the factor used to populate the matrix table in conjunction with the convolution filter operator. // The bigger this value is the more cubic alike will be the final image. EdgeFactor int // MaxPoints holds the maximum number of generated points the vertices/triangles will be generated from. MaxPoints int // Wireframe defines the visual appearence of the generated vertices (WithoutWireframe|WithWireframe|WireframeOnly). Wireframe int // Noise defines the intensity of the noise factor used to give a noisy, despeckle like touch of the final image. Noise int // StrokeWidth defines the contour width in case of using WithWireframe | WireframeOnly mode. StrokeWidth float64 // IsStrokeSolid - when this is set as true, the applied stroke color will be black. IsStrokeSolid bool // Grayscale will generate the output in grayscale mode. Grayscale bool // OutputToSVG saves the generated triangles to an SVG file. OutputToSVG bool // ShowInBrowser shows the generated svg file in the browser. ShowInBrowser bool // BgColor defines the background color in case of using transparent images as source files. // By default the background is transparent, but it can be changed using a hexadecimal format, like #fff or #ffff00. BgColor string }
Processor encompasses all of the currently supported processing options.
type SVG ¶
type SVG struct { Width int Height int Title string Lines []Line Color color.RGBA Description string StrokeLineCap string StrokeWidth float64 Processor }
SVG extends the Processor struct with the SVG parameters.
func (*SVG) DecodeImage ¶
DecodeImage calls the decodeImage utility function which decodes an image file type to the generic image.Image type.
func (*SVG) Draw ¶
func (svg *SVG) Draw(src image.Image, proc Processor, fn Fn) (image.Image, []Triangle, []Point, error)
Draw triangulates the source image and outputs the result to an SVG file. It has the same method signature as the rester Draw method, only that accepts a callback function for further processing, like opening the generated SVG file in the web browser. It returns the number of triangles generated, the number of points and the error in case exists.