layerbuilder

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 6 Imported by: 0

README

layerbuilder

Image Layer Decomposition (Color segmentation) in pure Go

This implementation differs from the original paper, It produces alpha-composited layers, not additive layers.

This package was developed to meet the need for artistic halftone generation.

Usage

func main() {
	// Read the source image.
	img := utils.ReadImage("../_test_files/parrot.png")
	// Extract a 7-color palette using the dominant-color method.
	palette := utils.ExtractPalette(img, 7, utils.PaletteMethodDominantColor)
	// Sort palette colors from dark to bright for better results.
	utils.SortPaletteByBrightness(palette)

	// Create a new builder with the image and palette.
	builder := layerbuilder.NewLayerBuilder(img, palette)
	// Build internal layers using options derived from image size.
	builder.Build(layerbuilder.OptionsFromSize(img.Bounds().Size()))
	// Reconstruct an image from grayscale alpha layers.
	recon := builder.Reconstruct(builder.GrayLayers())

	// Save reconstructed image and debug outputs.
	utils.SaveImage(recon, "../_test_files/output/recon.png")
	utils.SavePalette(palette, 64, "../_test_files/output/palette.png")
	utils.SaveRgbaImages(builder.RGBALayers(), "../_test_files/output/")
}

Documentation

Overview

Image Layer Decomposition in pure Go

Paper https://arxiv.org/pdf/1701.03754

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LayerBuilder

type LayerBuilder struct {
	InputImage      image.Image
	Rgb             rgb32
	Lab             lab32
	Clusters        labelImage
	SuperPixels     []superpixel
	LLEWeightMatrix *mat.Dense
	L               *mat.Dense
	PixelWeights    []float64
	Palette         []colorful.Color
}

func NewLayerBuilder

func NewLayerBuilder(input image.Image, palette []colorful.Color) *LayerBuilder

func (*LayerBuilder) Build

func (lb *LayerBuilder) Build(opt Options)

func (*LayerBuilder) GrayLayers

func (lb *LayerBuilder) GrayLayers() []*image.Gray

func (*LayerBuilder) RGBALayers

func (lb *LayerBuilder) RGBALayers() []*image.NRGBA

func (*LayerBuilder) Reconstruct

func (lb *LayerBuilder) Reconstruct(grayLayers []*image.Gray) *image.RGBA

type Options

type Options struct {
	// Number of SLIC regions.
	// Image-size dependent: larger images usually need higher values.
	// Ideal start: ~area/(40^2) (roughly 300-1200 for common inputs).
	// Too low => larger regions => blockier alpha transitions.
	NumSuperpixels int
	// Neighbor count for superpixel manifold graph W.
	// Ideal start: 24-40.
	// Too low can over-localize layers; too high can over-smooth boundaries.
	LLENeighbors int
	// Per-pixel interpolation neighbors from superpixels. Most direct blockiness control:
	// lower values (especially 1-4) can create visible region boundaries.
	// Ideal start: 12-20.
	PixelNeighbors int
	// Manifold smoothness weight in solveFullSystem.
	// Ideal start: 0.18-0.30. Lower => weaker cross-region smoothing.
	Lm float64
	// Reconstruction weight in solveFullSystem.
	// Ideal start: ~1.0 (0.8-1.2). Lower => weaker color fitting, more seed-like alphas.
	Lr float64
	// Alpha magnitude regularization.
	// Ideal start: 0.01-0.03. Higher pushes alphas toward 0 and can produce sparse blobs.
	Lu float64
}

func DefaultOptions

func DefaultOptions() Options

func OptionsFromSize

func OptionsFromSize(imageSize image.Point) Options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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