triangle

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2018 License: MIT Imports: 11 Imported by: 5

README

Build Status license release homebrew

Triangle is a tool to create image arts using the delaunay triangulation technique. It takes an image as input and it converts to abstract image composed of tiles of triangles.

Sample image

The technique
  • First the image is blured out to smothen the sharp pixel edges. The more blured an image is the more diffused the generated output will be.
  • Second the resulted image is converted to grayscale mode.
  • Then a sobel filter operator is applied on the grayscaled image to obtain the image edges. An optional threshold value is applied to filter out the representative pixels of the resulting image.
  • Lastly we apply the delaunay algorithm on the pixels obtained from the previous step.
blur = tri.Stackblur(img, uint32(width), uint32(height), uint32(*blurRadius))
gray = tri.Grayscale(blur)
sobel = tri.SobelFilter(gray, float64(*sobelThreshold))
points = tri.GetEdgePoints(sobel, *pointsThreshold, *maxPoints)

triangles = delaunay.Init(width, height).Insert(points).GetTriangles()

Installation and usage

$ go get github.com/esimov/triangle/cmd/triangle
$ go install

MacOS (Brew) install

The library can be installed via Homebrew too or by downloading the binary file from the releases folder.

$ brew tap esimov/triangle
$ brew install triangle
Supported commands
$ triangle --help

The following flags are supported:

Flag Default Description
in n/a Input file
out n/a Output file
blur 4 Blur radius
max 2500 Maximum number of points
noise 0 Noise factor
points 20 Points threshold
sobel 10 Sobel filter threshold
solid false Solid line color
wireframe 0 Wireframe mode (without,with,both)
stroke 1 Stroke width
gray false Convert to grayscale
svg false Save as SVG
web false Output SVG in browser
Output as image or SVG

By default the output is saved to an image file, but setting the -svg flag as true you can export the resulted vertices even to an SVG file, being the perfect candidate for large poster images. Using a small image as input source, exporting to an *.svg file will generate a very low processing footprint whitout image loss.

$ triangle -in samples/input.jpg -out output.png -svg=1

Using the -svg together with -web flag you can access the svg result on the web browser.

$ triangle -in samples/input.jpg -out output.png -svg=1 -web=1
Multiple image processing with a single command

You can transform even multiple images from a specific folder with a single command by declaring as -in flag the source folder and as -out flag the destination folder.

$ triangle -in ./samples/ -out ./ouput -wireframe=0 -max=3500 -stroke=2 -blur=2 -noise=4
Tweaks

Setting a lower points threshold, the resulted image will be more like a cubic painting. You can even add a noise factor, generating a more artistic, grainy image.

Here are some examples you can experiment with:

$ triangle -in samples/input.jpg -out output.png -wireframe=0 -max=3500 -stroke=2 -blur=2
$ triangle -in samples/input.jpg -out output.png -wireframe=2 -max=5500 -stroke=1 -blur=10
Examples

Sample_0 Sample_1 Sample_11 Sample_8

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const (
	// WithoutWireframe - generates triangles without stroke
	WithoutWireframe = iota
	// WithWireframe - generates triangles with stroke
	WithWireframe
	// WireframeOnly - generates triangles only with wireframe
	WireframeOnly
)
View Source
const PointRate = 0.875

PointRate defines the default point rate. Changing this value will modify the triangles sizes.

Variables

This section is empty.

Functions

func Grayscale

func Grayscale(src *image.NRGBA) *image.NRGBA

Grayscale convert image to grayscale mode.

func Noise

func Noise(amount int, pxl image.Image, w, h int) *image.NRGBA64

Noise apply a noise factor, like adobe's grain filter to create a despeckle like image.

func SobelFilter

func SobelFilter(img *image.NRGBA, threshold float64) *image.NRGBA

SobelFilter detect image edges. See https://en.wikipedia.org/wiki/Sobel_operator

func StackBlur added in v1.0.2

func StackBlur(img *image.NRGBA, radius uint32) *image.NRGBA

StackBlur is the main function which takes an image as parameter and returns it's blurred version by applying the blur radius.

Types

type Delaunay

type Delaunay struct {
	// contains filtered or unexported fields
}

Delaunay is the main entry struct which defines the delaunay system.

func (*Delaunay) GetTriangles

func (d *Delaunay) GetTriangles() []Triangle

GetTriangles return the generated triangles.

func (*Delaunay) Init

func (d *Delaunay) Init(width, height int) *Delaunay

Init initialize the delaunay structure.

func (*Delaunay) Insert

func (d *Delaunay) Insert(points []Point) *Delaunay

Insert new triangles into the delaunay triangles slice.

type Drawer added in v1.0.2

type Drawer interface {
	Draw(io.Reader, io.Writer) ([]Triangle, []Point, error)
}

Drawer interface which defines the Draw method. This method needs to be implemented by every struct which defines a Draw method. This is meant for code reusing and modularity. In our case the image can be triangulated as raster image or SVG.

type Image added in v1.0.2

type Image struct {
	Processor
}

Image extends the Processor struct.

func (*Image) Draw added in v1.0.2

func (im *Image) Draw(input io.Reader, output io.Writer, closure func()) ([]Triangle, []Point, error)

Draw triangulate the source image and output the result to an image file. It returns the number of triangles generated, the number of points and the error in case exists.

type Line added in v1.0.2

type Line struct {
	P0          Node
	P1          Node
	P2          Node
	P3          Node
	FillColor   color.RGBA
	StrokeColor color.RGBA
}

Line defines the SVG line parameters.

type Node

type Node struct {
	X, Y int
}

Node struct having as components the node X and Y coordinate position.

type Point

type Point struct {
	// contains filtered or unexported fields
}

Point defines a struct having as components the point X and Y coordinate position.

func GetEdgePoints

func GetEdgePoints(img *image.NRGBA, threshold, maxPoints int) []Point

GetEdgePoints retrieves the triangle points after the Sobel threshold has been applied.

type Processor added in v1.0.1

type Processor struct {
	BlurRadius      int
	SobelThreshold  int
	PointsThreshold int
	MaxPoints       int
	Wireframe       int
	Noise           int
	StrokeWidth     float64
	IsSolid         bool
	Grayscale       bool
	OutputToSVG     bool
	OutputInWeb     bool
}

Processor type with processing options

type SVG added in v1.0.2

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) Draw added in v1.0.2

func (svg *SVG) Draw(input io.Reader, output io.Writer, closure func()) ([]Triangle, []Point, error)

Draw triangulate the source image and output the result to an SVG file. It returns the number of triangles generated, the number of points and the error in case exists.

type Triangle

type Triangle struct {
	Nodes []Node
	// contains filtered or unexported fields
}

Triangle struct which defines the basic components of a triangle. It's constructed from the nodes, it's edges and the circumcircle which describes the triangle circumference.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL