Skip to content

ndimage #

vsl.ndimage

Portable N-dimensional array filtering and morphology. The module is pure V and does not depend on FFT or native backends.

Arrays and boundaries

NDArray.new(shape, values) builds a row-major array from an []int shape and a flat []f64 value slice. value(coords) reads in-range samples; sample(coords, mode) applies a boundary mode when coordinates fall outside an axis:

mode behavior
.constant out-of-range samples are zero
.nearest clamp to the edge sample
.reflect reflect without repeating the edge
.mirror reflect including the edge
.wrap periodic wrap-around

Filters

  • gaussian_filter(array, sigma, mode) — separable normalized Gaussian with radius ceil(3 * sigma) on every axis.
  • median_filter(array, window, mode) — median over an odd hypercube window.
  • sobel_filter(array, axis, mode) — [-1,0,1]/2 derivative on axis with [1,2,1]/4 smoothing elsewhere; every axis needs at least three samples.

Morphology and labeling

Values greater than zero are foreground. binary_erosion, binary_dilation, binary_opening, and binary_closing use an odd hypercube structuring element. label(array) returns a label array and the component count, using face-adjacent neighbors (4-connected in 2-D).

import vsl.ndimage

mut pixels := []f64{len: 9, init: 0.0}
pixels[4] = 1.0
image := ndimage.NDArray.new([3, 3], pixels)!
denoised := ndimage.median_filter(image, 3, .nearest)!
labels, count := ndimage.label(denoised)!
println('components=${count}, center=${labels.value([1, 1])!}')

Current limitations: one scalar sigma and one shared window size per call, and face connectivity only for labeling. See the image processing example.

fn binary_closing #

fn binary_closing(input NDArray, window int, mode BoundaryMode) !NDArray

binary_closing dilates and then erodes, filling small background holes while preserving the shape of larger ones. window must be positive and odd.

fn binary_dilation #

fn binary_dilation(input NDArray, window int, mode BoundaryMode) !NDArray

binary_dilation expands foreground samples to their hypercube window. window must be positive and odd.

fn binary_erosion #

fn binary_erosion(input NDArray, window int, mode BoundaryMode) !NDArray

binary_erosion shrinks foreground samples to those whose entire hypercube window is foreground. window must be positive and odd.

fn binary_opening #

fn binary_opening(input NDArray, window int, mode BoundaryMode) !NDArray

binary_opening erodes and then dilates, removing small foreground objects while preserving the shape of larger ones. window must be positive and odd.

fn gaussian_filter #

fn gaussian_filter(input NDArray, sigma f64, mode BoundaryMode) !NDArray

gaussian_filter applies a separable normalized Gaussian. sigma must be positive; the per-axis radius is ceil(3 * sigma).

fn label #

fn label(input NDArray) !(NDArray, int)

label assigns connected components using face-adjacent (4-neighbor in 2-D) connectivity. It returns the label array and the number of components.

fn median_filter #

fn median_filter(input NDArray, window int, mode BoundaryMode) !NDArray

median_filter replaces each sample with the median of a hypercube window. window must be positive and odd.

fn sobel_filter #

fn sobel_filter(input NDArray, axis int, mode BoundaryMode) !NDArray

sobel_filter computes the first derivative along axis with a [-1,0,1]/2 kernel and [1,2,1]/4 smoothing on the remaining axes.

fn BoundaryMode.from #

fn BoundaryMode.from[W](input W) !BoundaryMode

from returns the BoundaryMode value of an integer or of a field name, or an error if there is none.

fn NDArray.new #

fn NDArray.new(shape []int, values []f64) !NDArray

NDArray.new validates the shape, the value count, and finiteness of values.

enum BoundaryMode #

enum BoundaryMode {
	constant
	nearest
	reflect
	mirror
	wrap
}

BoundaryMode selects how out-of-range neighbors are sampled.

struct NDArray #

struct NDArray {
pub:
	data  []f64
	shape []int
}

NDArray is a row-major N-dimensional f64 array. Only the active array size is allocated; shape describes how flat indices map to coordinates.

fn (NDArray) sample #

fn (array &NDArray) sample(coords []int, mode BoundaryMode) f64

sample returns the value at coordinates, applying the boundary mode. Under .constant, out-of-range coordinates return zero.

fn (NDArray) value #

fn (array &NDArray) value(coords []int) !f64

value returns the value at in-range coordinates.