Qu

Images

Every table below is generated by reading the interpreter's own builtin dispatch table (engine/crates/qu-interp/src/lib.rs, inside Interp::call_builtin's giant match f { ... }), not written from a spec — it reflects exactly what runs today. Signatures use [name=default] for named (key=value) style arguments and [positional] for optional positional ones. Line numbers are cited so a future audit can re-verify them against a moving file.

Creating & Loading Images

FunctionSignatureDescription
image_newimage_new(width, height, [r=0, g=0, b=0])Creates a blank solid-color canvas. width and height are positive integers giving the canvas size in pixels. r, g, b are optional named numbers 0–255 giving the fill color's channel values (default 0, i.e. solid black). Returns a new Image of exactly width x height pixels. The only way to build a synthetic image with no file on disk.
image_from_matriximage_from_matrix(matrix)Builds a grayscale Image directly from matrix, a 2-D Mat of numbers; each value is rounded and clamped to 0–255 and written to R=G=B for that pixel (matrix rows become image rows, columns become image columns). Returns a new Image sized cols x rows to match the matrix's own shape. For a real pixel buffer you can further process/save, as opposed to imagesc's colormap display.
load_imageload_image(path)path is a string file path. Reads the file from disk and decodes it — PNG, BMP, JPEG and TIFF. The format is sniffed from the file's first bytes rather than its extension, because a .png that is really a BMP is a thing that happens and the bytes are never wrong. PNG: bit depths 1/2/4/8/16, colour types grey, truecolour, palette, grey+alpha and RGBA; alpha composites onto white, since Image is three channels. Adam7 interlacing is refused by name rather than decoded wrongly. JPEG: baseline and progressive, grayscale (widened to R=G=B) and colour; 16-bit and CMYK JPEGs are refused by name rather than converted, because guessing CMYK's Adobe inversion convention wrong yields silently colour-inverted pixels. TIFF: 8- and 16-bit integer samples in grayscale, grayscale+alpha, RGB and RGBA (16-bit is scaled down to 8, since Image is 8-bit; alpha composites onto white as with PNG); floating-point, palette, CMYK and YCbCr TIFFs are refused by name. Returns a new Image sized to the file's own dimensions. JPEG and TIFF reading added in v0.3.0.
save_imagesave_image(path, img, [quality=90])path is a string destination; img is the Image to write. Encodes img and writes it to path, dispatching on the path's own extension: .png writes a real (if uncompressed) PNG; .jpg/.jpeg writes a baseline JPEG; .tif/.tiff writes an uncompressed 8-bit RGB TIFF; any other extension, or none, writes a BMP. (Extension dispatch on write, magic-byte sniffing on read, is not an inconsistency: writing has no bytes to sniff yet, so the extension is the only statement of what the caller wants.) quality is an optional named number 1–100 (default 90) that is only meaningful for JPEG — it is accepted and ignored for the other formats, and a value outside 1–100 is an error rather than silently clamped. JPEG is lossy: a save_image/load_image round trip through .jpg does not return the pixels that went in. On the gradient images the test suite measures, the RMS difference is about 1.3–1.4 levels out of 255 at the default quality 90, and about 0.6 at quality 100 — small, but not zero, and it compounds if you re-save repeatedly. PNG, BMP and TIFF as written here are lossless, so their round trips are pixel-exact. Returns nothing. JPEG and TIFF writing, and quality=, added in v0.3.0.
imagescimagesc(matrix, [colormap="viridis"])matrix is a 2-D Mat of arbitrary real-valued numbers (no 0–255 requirement). colormap is an optional named string (default "viridis") selecting the palette, same set as heatmap's. False-color raster display of the plain numeric matrix (no pixel buffer produced) — pushes a dense Heatmap onto the current plot panel, reusing the same renderer as heatmap, just without per-cell value labels or gridlines. Defaults to viridis rather than heatmap's blues. Returns nothing; it is a plotting side effect.
imshowimshow(img)img is an Image value (e.g. from load_image/grayscale/any transform below), of any size. Places it inline in the current plot panel as a native SVG <image> element at its own resolution. Takes an actual Image, not a scalar matrix — use imagesc for that. Returns nothing.

Basic Transforms

FunctionSignatureDescription
grayscalegrayscale(img)img is an Image of any size. Converts RGB → luma via ITU-R BT.601 weights (matches plotting.rs's contrast_text_color). Returns a new Image, same width/height as img, with R=G=B at every pixel — still a full 3-channel RGB image, not a separate 1-channel type.
resizeresize(img, width, height, [method="nearest"])img is an Image; width/height are positive integers giving the absolute target size in pixels (not a scale factor — see imscale for that). method is an optional named string: "nearest" (default, cheap nearest-neighbor) or "bilinear" (smoother). Returns a new Image of exactly width x height pixels. Also overloaded for Vec (resize(vec, new_length, [fill=0.0]), a ReDim Preserve-style grow/shrink to new_length elements, padding new slots with the number fill when growing, truncating when shrinking, and returning a new Vec) — unrelated to images but the same builtin name.
cropcrop(img, x0, y0, width, height)img is an Image; x0, y0 are integers giving the top-left pixel coordinate of the sub-rectangle; width/height are positive integers giving its size in pixels. Extracts that sub-rectangle. Returns a new Image of exactly width x height pixels.
imrotateimrotate(img, angle_degrees, [method="bilinear"], [bbox="loose"])Rotates an image about its own center. img is an Image; angle_degrees is a number, the rotation angle about the image center — positive = counterclockwise (MATLAB's convention). method is an optional named string, "nearest" or "bilinear" (default), controlling resampling. bbox is an optional named string: "loose" (default) expands the canvas to fit the rotated content; "crop" keeps the original width/height, clipping content that rotates outside the frame. Returns a new Image.
imtranslateimtranslate(img, dx, dy, [method=], [bbox="crop"], [fill=0])img is an Image; dx/dy are numbers, the horizontal/vertical pixel shift (positive = right/down). method is an optional named string ("nearest"/"bilinear") controlling resampling. bbox is an optional named string, defaulting to "crop" (output stays the same size as the input, matching MATLAB imtranslate's 'OutputView','same') — the opposite default from imwarp/imscale. fill is an optional named number 0–255 (default 0), the byte value used on all 3 channels for pixels that map outside the source. Implemented as imwarp + affine_translate. Returns a new Image.
imscaleimscale(img, sx, [sy], [method=], [bbox="loose"], [fill=0])img is an Image; sx is a number, the scale factor (imscale(img, 2) doubles both dimensions); sy is an optional number, the vertical scale factor, defaulting to sx (uniform scale) when omitted. method/fill as in imtranslate. bbox is an optional named string, defaulting to "loose" so the output canvas grows/shrinks to fit the scaled content — the opposite default from imtranslate. Implemented as imwarp + affine_scale. Different from resize, which takes absolute output dimensions rather than a factor. Returns a new Image.

Overloads: resize

Case: Image

img an Image; width/height are the absolute output size in pixels. Resamples the whole picture to that size using method for interpolation — the resulting figure is the same content redrawn at a new resolution, nothing about the pixels' meaning changes.

img = image_new(10, 10, r=0, g=0, b=0)
big = resize(img, 20, 15, method="bilinear")
print(big.width, big.height)   # 20 15

output
20 15

Ask an image its dimensions with .width and .height, not with a tuple. Every call in this chapter that takes a size takes it as width then height — image_new(width, height), resize(img, width, height), crop(img, x0, y0, width, height) — and a named accessor cannot be read in the wrong order the way a pair can. img.width means the same thing however the language chooses to order a returned pair, so code written against it does not depend on remembering a convention, and does not change meaning if that convention is ever revisited.

For completeness: size(img) returns [width, height] — the same order every call above takes — and length(img) returns the pixel count, which has no order to get wrong. There is no worked example of size on an image on this page yet, deliberately: the build that renders these pages runs an engine older than that behaviour, so an example printing it would show you a number the current engine does not produce, over the build's signature. It will be added once the two agree.

Case: Vec

vec a Vec; new_length is the target element count, fill (optional, default 0.0) is the value used to pad new trailing slots when growing. A ReDim Preserve-style grow/shrink — growing keeps every existing element and appends fills, shrinking truncates from the end. Nothing to do with pixels at all; same builtin name, unrelated meaning.

v = [1, 2, 3]
print(resize(v, 5, fill=9))   # [1, 2, 3, 9, 9] -- grown, padded with 9

print(resize(v, 2))           # [1, 2]          -- shrunk, tail dropped

output
[1, 2, 3, 9, 9]
[1, 2]

Affine Transforms

The affine_* builtins each return a plain 3x3 homogeneous-coordinate Mat value — real matrices, not an opaque "transform" type — so they compose with Qu's own */matmul operator exactly like any other matrix. A point transforms as M @ [x, y, 1]^T. Composing A = affine_translate(...) * affine_rotate(...) * affine_scale(...) and applying A @ p computes translate(rotate(scale(p))): scale happens first (rightmost factor), then rotate, then translate last (leftmost factor) — the same right-to-left application order as OpenGL's M = T*R*S. imwarp (and imtranslate/imscale, which build their own transform internally) is what actually *applies* one of these matrices to an image, via one inverse-mapping resampling pass.

Note: affine_rotate's matrix is the plain textbook CCW rotation matrix in a standard (x right, y up) coordinate system, deliberately not special-cased for image pixel coordinates (y increasing downward) the way imrotate internally negates its angle. Applied to image pixels via imwarp, a positive affine_rotate angle therefore appears clockwise on screen — the opposite visual sign from imrotate's own angle_degrees.

FunctionSignatureDescription
affine_identityaffine_identity()Takes no arguments. Returns the 3x3 identity Mat — a no-op transform, useful as a composition base case.
affine_translateaffine_translate(dx, dy)dx/dy are numbers, the horizontal/vertical shift amount. Returns a new 3x3 homogeneous-coordinate Mat encoding that translation.
affine_scaleaffine_scale(sx, [sy])Builds a scaling transform for imwarp. sx is a number, the scale factor along x; sy is an optional number, the scale factor along y, defaulting to sx (uniform scale) when omitted — matching MATLAB imresize(img, scale)'s single-factor convention. Returns a new 3x3 Mat.
affine_rotateaffine_rotate(angle_degrees)angle_degrees is a number. Returns a new 3x3 Mat encoding a CCW rotation matrix in standard math convention (see the clockwise-on-screen note above for the visual difference from imrotate).
affine_shearaffine_shear(shx, shy)shx/shy are numbers, the shear factors along x and y. Returns a new 3x3 Mat encoding that shear.
imwarpimwarp(img, M, [method="bilinear"], [bbox="loose"], [fill=0])img is an Image; M is a 3x3 (or 2x3, auto-padded with [0,0,1]) affine Mat — from the affine_* builders above, a literal matrix, or any composition of the two via */matmul. method is an optional named string ("nearest"/"bilinear", default "bilinear"). bbox is an optional named string, "loose" (default, grows the canvas to fit) or "crop" (keeps the input's size). fill is an optional named number 0–255 (default 0) applied to all 3 channels for pixels that map outside the source image. Applies M to img in one resampling pass via inverse mapping. Returns a new Image.

Filtering & Enhancement

FunctionSignatureDescription
blurblur(img)img is an Image of any size. Applies a fixed 3x3 box-average blur kernel. Returns a new Image, the same dimensions as img.
sharpensharpen(img)img is an Image. Applies a fixed 3x3 unsharp-mask sharpen kernel. Returns a new Image, same dimensions as img.
edge_detectedge_detect(img)img is an Image. Applies a fixed 3x3 discrete Laplacian edge filter, highlighting intensity discontinuities. Returns a new Image, same dimensions as img.
imfilterimfilter(img, kernel)img is an Image; kernel is an arbitrary odd-sized (e.g. 3x3, 5x5) square Mat of numeric weights. Generic 2-D convolution with edge-clamped borders — the escape hatch when blur/sharpen/edge_detect's fixed kernels aren't enough. Returns a new Image, same dimensions as img.
imdilateimdilate(img, radius) <br> imdilate(img, se=)img is a binary (0/255 mask-style) Image; radius is a positive integer, the half-width of a square structuring element of side 2*radius+1 (a documented choice over a disk element). Performs binary morphological dilation, growing foreground regions. Returns a new Image, same dimensions as img. se= is an optional named structuring-element mask Image (from disk()/line(), or hand-built) used in place of the radius-implied square — see "Structuring elements" below. radius is simply not read when se= is given, so the original two-positional-argument form keeps working unchanged. se= added in v0.4.0.
imcloseimclose(img, radius) <br> imclose(img, se=)img is a binary Image; radius is a positive integer (same square structuring-element convention as imdilate). Morphological closing (dilate then erode) — fills small background holes without enlarging the overall shape. Returns a new Image, same dimensions. (imopen/imerode, erosion and opening, also exist alongside it in the same match arm pair but aren't in this chapter's requested scope — see the "More functions" table below.) Same additive se= kwarg as imdilate above, added in v0.4.0.
imbothatimbothat(img, radius) <br> imbothat(img, se=)img is a binary Image; radius is a positive integer. Bottom-hat: imclose(img, radius) - img, extracts small dark features / corrects a slowly-varying dark background. Returns a new Image, same dimensions. (imtophat, the bright-feature dual, is its sibling arm, documented below.) Same additive se= kwarg as imdilate above, added in v0.4.0.
histeqhisteq(img) (alias of imequalize)img is an Image of any size, color or grayscale. Global histogram equalization: builds a BT.601-luma cumulative distribution and remaps intensities to spread across the full range. RGB-preserving via luma-ratio remapping — not a grayscale-only fallback. Returns a new Image, same dimensions as img.
imadjustimadjust(img, [in_low=], [in_high=], [out_low=0.0], [out_high=1.0], [gamma=1.0])img is an Image. in_low/in_high are optional named numbers in [0,1], the input intensity range to stretch from; when both are omitted they auto-default to the image's own 1st/99th percentile (all channel bytes pooled) — robust to outlier pixels, like MATLAB's stretchlim. out_low/out_high are optional named numbers in [0,1] (default 0.0/1.0), the output range to stretch to. gamma is an optional named number (default 1.0) applying a power-law curve after the linear stretch. MATLAB-style contrast stretch, applied identically and independently to each of R/G/B. Returns a new Image, same dimensions as img.
imhistimhist(img, [n_bins=256])img is an Image; n_bins is an optional positive integer (default 256; may be given positionally or as a named argument), the number of histogram bins. Computes raw per-bin BT.601-luma intensity counts. Returns a Vec of length n_bins — data, not a rendered plot. Pipe the result into hist/bar to actually draw it.
imnoiseimnoise(img, type, [mean=0.0], [sigma=0.05], [density=0.05], [seed=])img is an Image; type is a required string, "gaussian" or "salt_pepper". For "gaussian": mean/sigma are optional named numbers (defaults 0.0/0.05), the additive per-channel Gaussian noise's mean/standard-deviation in normalized [0,1] intensity units. For "salt_pepper": density is an optional named number (default 0.05), the fraction of pixels (not channel samples) flipped to pure black/white. seed is an optional named integer for reproducibility (same convention as randn/uniform); omitted, a fresh random seed is used. Adds synthetic noise for testing other CV algorithms. Returns a new Image, same dimensions as img.

Overloads: imdilate

Case: binary mask

img a binary (0/255) Image. Dilation/erosion/opening/closing/top-hat/bottom-hat all reduce to their textbook binary definitions on a genuine mask: the foreground blob grows outward by radius, and every output pixel stays exactly 0 or 255 — no new grey levels ever appear.

mb = [0, 0, 0, 0, 0; 0, 0, 0, 0, 0; 0, 0, 255, 0, 0; 0, 0, 0, 0, 0; 0, 0, 0, 0, 0]
binary_img = image_from_matrix(mb)
grown = imdilate(binary_img, 1)
print(imhist(grown, n_bins=4))   # [16, 0, 0, 9] -- only the two extreme bins are ever used

output
[16, 0, 0, 9]

Case: grayscale image

img a genuine grayscale (or color) Image with more than two intensity levels. imdilate/imerode/imopen/imclose/imtophat/imbothat are all implemented as a per-channel MAX (dilate) or MIN (erode) over the structuring window — true grayscale morphology, not "threshold then grow." Fed a smooth image, the intermediate grey levels shift around instead of snapping everything to black/white.

mg = [10, 60, 110, 160, 210; 20, 70, 120, 170, 220; 30, 80, 130, 180, 230; 40, 90, 140, 190, 240; 50, 100, 150, 200, 250]
gray_img = image_from_matrix(mg)
grown = imdilate(gray_img, 1)
print(imhist(grown, n_bins=4))   # [0, 6, 7, 12] -- three separate bins in use, no black survives at all

output
[0, 6, 7, 12]

Structuring elements: disk, line, se=, and hit_miss

*Added in v0.4.0.* imdilate/imerode/imopen/imclose/imtophat/imbothat's radius argument always implies a square structuring element (side 2*radius+1, every offset in the bounding box). disk/line build two other shapes, passed in as the se= keyword shown above instead of radius; a structuring element is represented the same way docs/design/toolkit-image.md §2 already frames a mask — "just a bool image" (here, an ordinary 0/255 binary-mask Image you can imshow directly) — rather than a new value kind.

FunctionSignatureDescription
diskdisk(radius)radius is a non-negative integer. Builds a disk-shaped structuring-element mask Image: a (2*radius+1) x (2*radius+1) binary mask, foreground wherever dx*dx + dy*dy <= radius*radius relative to its own center pixel. Unlike the radius-implied square, a disk excludes the four corner directions — disk(1) is a 5-pixel plus shape, not the full 3x3 block. Returns a new binary Image. Added in v0.4.0.
lineline(length, angle_degrees)length is a positive integer, the SE's length in pixels; angle_degrees is a number, the line's orientation (0 = along +x, positive = counterclockwise as displayed, same convention as imrotate). Builds a line-segment structuring-element mask Image by Bresenham-rasterizing a length-pixel segment centered on its own middle pixel. length <= 1 degenerates to a single center pixel. Returns a new binary Image, sized to the line's own bounding box. Added in v0.4.0.
hit_misshit_miss(img, se_fg, se_bg)img is a binary Image; se_fg/se_bg are structuring-element mask Images (from disk()/line(), or hand-built with e.g. image_from_matrix). The morphological hit-or-miss transform: output pixel (x, y) is foreground iff EVERY foreground pixel of se_fg (relative to its own center) lands on a foreground pixel of img at that offset from (x, y), AND EVERY foreground pixel of se_bg lands on a BACKGROUND pixel — a position neither SE covers is a don't-care. Out-of-range neighbors count as background (same border convention distance_transform/fill_holes use). The standard way to isolate a specific local pattern (corners, endpoints, ...) in a binary image. Returns a new binary Image, same dimensions as img. Added in v0.4.0.
# disk(1) is a plus shape, not a 3x3 square: eroding a 1-pixel-wide plus

# with the radius-implied square wipes it out completely, but a disk SE

# of the same radius exactly matches the plus's own footprint and leaves

# its center pixel standing.

plus = image_from_matrix([0,0,0,0,0; 0,0,255,0,0; 0,255,255,255,0; 0,0,255,0,0; 0,0,0,0,0])
square_eroded = imerode(plus, 1)
print(imhist(square_eroded, n_bins=2))   # [25, 0] -- wiped out entirely


se = disk(1)
disk_eroded = imerode(plus, se=se)
print(imhist(disk_eroded, n_bins=2))     # [24, 1] -- only the center pixel survives


# hit_miss isolating the top-left corner of a solid 3x3 square: fg

# requires self/east/south/south-east foreground, bg requires

# west/north/north-west background -- true only at the one corner pixel.

sq = image_from_matrix([0,0,0,0,0; 0,255,255,255,0; 0,255,255,255,0; 0,255,255,255,0; 0,0,0,0,0])
fg = image_from_matrix([0,0,0; 0,255,255; 0,255,255])
bg = image_from_matrix([255,255,0; 255,0,0; 0,0,0])
corner = hit_miss(sq, fg, bg)
print(imhist(corner, n_bins=2))          # [24, 1] -- exactly one corner pixel found

output
[25, 0]
[24, 1]
[24, 1]

Overloads: imadjust

Case: explicit range

in_low/in_high given explicitly. The stretch maps exactly that input range to out_low..out_high linearly — passing the image's own full byte range (in_low=0.0, in_high=1.0) is a no-op that leaves a low-contrast image just as flat as it started.

m = [40, 42, 41, 43; 44, 40, 250, 41]
img = image_from_matrix(m)
fixed = imadjust(img, in_low=0.0, in_high=1.0)
print(imhist(fixed, n_bins=8))   # [0, 7, 0, 0, 0, 0, 0, 1] -- unchanged, still clustered in one bin

output
[0, 7, 0, 0, 0, 0, 0, 1]

Case: auto range (both omitted)

When both in_low and in_high are left out, they default to the image's own 1st/99th percentile of pooled channel bytes rather than 0.0/1.0 — a robust, data-driven stretch (MATLAB's stretchlim), not a no-op. The exact same source image that stayed flat above spreads across the full range once the percentile-based stretch kicks in.

m = [40, 42, 41, 43; 44, 40, 250, 41]
img = image_from_matrix(m)
auto = imadjust(img)
print(imhist(auto, n_bins=8))   # [7, 0, 0, 0, 0, 0, 0, 1] -- stretched out to both ends of the range

output
[7, 0, 0, 0, 0, 0, 0, 1]

Segmentation & Thresholding

FunctionSignatureDescription
thresholdthreshold(x, level)x is an Image, Signal, Vec, or scalar; level is a number, the cutoff. Elementwise binary threshold, general-purpose (not image-specific). Image → binary black/white image of the same dimensions (per-pixel BT.601 luma vs level); Signal → same-Fs Signal of 0.0/1.0; Vec/scalar → identical to x >= level (a Mask/Bool via the shared compare path). Returns the same kind of value x was: an Image from an Image, a same-Fs Signal from a Signal, a Mask from a Vec, a Bool from a scalar.
multithresholdmultithreshold(x, levels)x is an Image, Signal, Vec, or scalar; levels is a Vec/matrix or tuple List of cut-point numbers (sorted ascending regardless of input order). Quantizes into len(levels)+1 bands at the given cut points. Image → posterized image of the same dimensions with evenly-spaced gray levels per band; Signal/plain numeric → raw 0-based band index. Returns the same kind of value x was: an Image from an Image, a same-Fs Signal from a Signal, and otherwise a Vec/Num of 0-based band indices.
otsu_thresholdotsu_threshold(x)x is an Image, Vec, or Signal. Computes (does not apply) the classic Otsu optimal threshold from x's histogram (Image reads per-pixel luma; Vec/Signal read their own values). Returns a single number, the threshold level. Compose with threshold(x, otsu_threshold(x)) to segment.
otsuotsu(x)x is an Image, Vec, or Signal. Compute-and-apply convenience: otsu_threshold followed immediately by threshold in one call. Returns the same type/shape threshold(x, level) would (e.g. a new binary Image of the same dimensions for an Image input).
multi_otsumulti_otsu(x, n_classes)x is an Image, Vec, or Signal; n_classes is a positive integer, the desired number of intensity classes. The n_classes - 1-threshold generalization of otsu_threshold. Returns a Vec of n_classes - 1 cut points in ascending order. Compose with multithreshold(x, multi_otsu(x, n)) to segment into n classes.
kapur_thresholdkapur_threshold(x)x is an Image, Vec, or Signal. Same "compute, don't apply" shape as otsu_threshold, but using Kapur's entropy-maximizing criterion instead of Otsu's between-class variance — tends to handle unequal class sizes better. Returns a single threshold number.
adaptive_threshold <br> *Added in v0.4.0*adaptive_threshold(image, block_size=, [method="mean"], [offset=0])image is an Image; block_size is a required odd positive integer, the side length of the per-pixel neighborhood. Locally adaptive, unlike threshold/otsu above: each pixel's own threshold is mean(neighborhood) - offset, where the neighborhood mean is a uniform box average (method="mean") or a Gaussian-weighted one (method="gaussian"). Binarized with the same >= convention as threshold. Returns a binary Image of the same dimensions.
sauvola_threshold <br> *Added in v0.4.0*sauvola_threshold(image, window_size=, [k=0.5], [r=128])image is an Image; window_size is a required odd positive integer. Sauvola's local threshold, T = mean * (1 + k * (stddev/r - 1)) computed per pixel over window_size, purpose-built for document/text images whose illumination drifts across the frame — a case a single global otsu cut cannot separate cleanly. k (typically 0.2–0.5) controls how strongly local contrast shifts the threshold; r is the expected dynamic range of the local stddev (128 for 8-bit images). Binarized with the same >= convention as threshold. Returns a binary Image of the same dimensions.
clahe <br> *Added in v0.4.0*clahe(image, tile_size=, [clip_limit=2.0])image is an Image; tile_size is a required positive integer, the pixel side length of each equalization tile (the last row/column of tiles shrinks to fit). Contrast-Limited Adaptive Histogram Equalization: imequalize's tile-based local counterpart, with a clipped per-tile histogram (clip threshold clip_limit * tile_pixel_count / 256) and — the part a naive per-tile implementation skips — bilinear interpolation of each pixel's mapping between its four nearest tile centers, so there is no hard seam at a tile boundary. RGB-preserving via the same luma-ratio rescale imequalize uses. Returns an Image of the same dimensions.
image.watershed <br> *Added in v0.4.0* (import image)image.watershed(surface, markers=)surface is a Mat (row-major, arbitrary real values) — the topographic surface to flood, typically a NEGATED image.distance_transform(mask) (so each blob's own center, the point farthest from any background, is the surface's deepest point and floods first) or a gradient magnitude. markers= is a required named Mat, the same shape as surface: 0 = unlabeled/to-be-flooded, any other value a seed already assigned to that label — required rather than derived from local extrema when omitted, because auto-deriving seeds is a separate, parameter-sensitive algorithm and a silent wrong guess would look like a wrong watershed instead of what it is. Marker-controlled watershed segmentation (priority-flood/Meyer's algorithm): floods strictly by ascending surface value, 4-connected, growing each marker's labeled region outward; where two different regions' flood-fronts meet, that pixel becomes a watershed line and is written 0 (the same value markers uses for "unlabeled", not -1) rather than joining either region — this is what stops two touching blobs from merging into one label. Returns a new Mat of labels, same shape as surface. Added in v0.4.0.

Overloads: threshold

Case: Image

x an Image. Compares each pixel's BT.601 luma against level and returns a new binary (0/255) Image of the same dimensions — the classic black-and-white segmentation mask.

m = [10, 200; 50, 220]
img = image_from_matrix(m)
mask = threshold(img, 128)
imshow(mask)
print(mask.width, mask.height)   # 2 2

output
2 2

Case: Signal

x a Signal. Elementwise compare against level, but returned as a same-Fs Signal of 0.0/1.0 values rather than a Mask — so it stays a Signal and can keep flowing through DSP functions that expect one.

sig = signal([1, 5, 2, 8], 100)
print(threshold(sig, 4))   # signal(Fs=100) [0, 1, 0, 1]

output
signal(Fs=100) [0, 1, 0, 1]

Case: Vec or scalar

x a Vec or a plain number. Identical to x >= level — a Mask for a Vec, a plain bool for a scalar — reusing the exact same shared compare path the >= operator itself uses, rather than any image-specific logic.

v = [1, 5, 10, 2]
print(threshold(v, 4))    # [F, T, T, F]

print(threshold(7, 4))    # true

output
[F, T, T, F]
true

multithreshold branches on x's type the same way, but the results differ more: its Image case returns a posterized *image* (evenly-spaced gray bands), while its Signal/plain-numeric case returns a raw 0-based *band index* rather than anything image-shaped.

image.watershed: separating two touching blobs

The property a plain connected-component labeling (bwlabel) cannot give you: two circles that overlap enough to form ONE connected blob still come back as two distinct labels, with a watershed line between them, as long as markers= seeds one point per circle's own center.

import image

# Two overlapping circles -- one connected blob to bwlabel.

w = 21
h = 13
mask_m = zeros(h, w)
for y in 0 to h - 1
  for x in 0 to w - 1
    in_a = (x - 6) ^ 2 + (y - 6) ^ 2 <= 25
    in_b = (x - 14) ^ 2 + (y - 6) ^ 2 <= 25
    if in_a or in_b
      mask_m[y, x] = 255
    end if
  end for
end for
mask = image_from_matrix(mask_m)

n_blobs = bwlabel(mask).count
print(n_blobs)   # 1 -- the two circles are one connected blob


dt = image.distance_transform(mask)
surface = -dt                 # each circle's own center floods first

markers = zeros(h, w)
markers[6, 6] = 1              # circle A's own center

markers[6, 14] = 2             # circle B's own center

labels = image.watershed(surface, markers=markers)

print(labels[6, 2])    # 1 -- deep inside circle A only

print(labels[6, 18])   # 2 -- deep inside circle B only

output
1
1
2

Region Analysis

FunctionSignatureDescription
bwlabelbwlabel(binary_img, [connectivity=8]) (alias of label_blobs)binary_img is a binary (0/255) Image. connectivity is an optional named number, 4 or 8 (default 8), the neighbor rule used for flood-fill labeling. Connected-component labeling. Returns a Model (kind "blobs") with fields .labels (a Mat of the same height/width as binary_img, integer label ids, background = 0), .count (a number, the blob count), .width, .height (the source image's own dimensions).
regionpropsregionprops(labeled) (alias of blob_stats)labeled is a Model produced by bwlabel/label_blobs. Takes that result and returns a List of one Record per blob, in label-id order, each with fields: label (integer id), area (pixel count), centroid_x, centroid_y (real pixel-coordinate means, not bbox-center approximations), bbox_x, bbox_y, bbox_width, bbox_height (tightest axis-aligned rectangle) — all numbers. Returns a List with one Record per blob, in label-id order — empty when labeled found none.
bwareaopenbwareaopen(binary_img, min_area) (alias of remove_small_blobs)binary_img is a binary Image; min_area is a number, the minimum pixel count a blob must have to survive. Labels internally (8-connectivity, matching MATLAB's default) and zeroes out every blob whose pixel count is below min_area. Returns a new binary Image, same dimensions as binary_img, with small blobs removed.
foreground_maskforeground_mask(img, level, [radius=1])img is an Image, color or grayscale (BT.601 luma either way); level is a required number, the threshold cutoff 0–255; radius is an optional named positive integer (default 1), the structuring-element radius for the cleanup step. Thresholds img at level, then applies imopen(radius) to remove noise specks. level is a required argument by design — pair with otsu_threshold for auto-thresholding: foreground_mask(img, otsu_threshold(img)). Returns a new binary Image, same dimensions as img.
image.regionsimage.regions(labeled, [pixel_size=], [unit=], [intensity_image=]) (import image)labeled is a Model from bwlabel/label_blobs. Returns a Table, one row per non-empty label, ordered by label id — the regionprops-shaped measurement but as columns rather than a List of Records, so a measurement can be filtered/grouped/joined/plotted with the language's ordinary table verbs. Columns: label; area, centroid_x, centroid_y, bbox_width, bbox_height (all suffixed _px/_px2 with no pixel_size=, or _<unit>/_<unit>2 when both pixel_size= and unit= are given — a unit with no scale, or unit="px" alongside a scale, is refused); extent (area / bbox_area, unitless, the scale cancels); perimeter (suffixed like the other lengths) — the region's outer boundary walked pixel-centre to pixel-centre (8-connected), summed and closed into a loop; a solid axis-aligned WxH rectangle measures 2*(W+H-2), NOT the 2*(W+H) edge-crossing count some other tools report, because the border ring of a solid block is all orthogonal steps; eccentricity (0–1, from an equivalent ellipse fit to the region's second central moments — 0 for a circle/square, approaching 1 for a thin line); orientation (radians, (-pi/2, pi/2], the equivalent ellipse's major-axis angle from +x, positive rotating towards +y — which is DOWN the image, i.e. clockwise on screen, not the counter-clockwise math-plot convention; 0 for an isotropic region by convention); solidity (area / convex_hull_area, unitless — the hull is built from every boundary pixel's four corners rather than its centre, so a solid convex region's hull area equals its own pixel-count area exactly and solidity is 1.0, never slightly above it). intensity_image= is an optional Image, the ORIGINAL (non-labeled) image to measure — when given, an intensity_mean column (BT.601 luma mean per region) is appended; when omitted, no such column appears at all, rather than one filled with a placeholder. The perimeter/eccentricity/orientation/solidity columns and the intensity_image= keyword were added in v0.4.0.

Drawing & Annotation

Draws directly onto a raster Image's pixel buffer and returns a NEW Image — the same "returns a new value, does not mutate the input" convention every other transform in this chapter follows. This is a different thing from the plotting system's own annotate/text/arrow builtins (see the Plotting chapter): those label a FIGURE — an SVG/PDF vector canvas with a data coordinate system — not a raw pixel buffer. Everything here takes plain pixel-integer coordinates, top-left origin, the same convention crop/get_pixel already use. A coordinate that lands (partly or entirely) outside the canvas is silently clipped rather than an error, the same way imtranslate's bbox="crop" lets content move partly off-frame.

FunctionSignatureDescription
draw_line <br> *Added in v0.4.0*draw_line(img, x0, y0, x1, y1, color=, [thickness=1])img is an Image; x0, y0, x1, y1 are numbers (pixel coordinates, rounded to the nearest integer). color= is required, in the same colour spelling every other color= keyword in this codebase accepts (a CSS name, #rrggbb/#rgb hex, or rgb(...)/rgba(...)). thickness= is an optional named positive integer (default 1), a square brush stamped at every point of a Bresenham line from (x0,y0) to (x1,y1) inclusive. Returns a new Image, same dimensions as img.
draw_rect <br> *Added in v0.4.0*draw_rect(img, x, y, width, height, color=, [thickness=1], [filled=false])img is an Image; x, y are the top-left corner (pixel coordinates); width, height are positive numbers. filled=true paints the whole interior (thickness is ignored — a fill has no stroke width); filled=false (default) strokes the four edges with the given thickness. At thickness=1 the outline is exactly 2*(width+height-2) pixels for width, height >= 2 — the same formula image.regions's own perimeter column documents for a solid rectangle's boundary walk. Returns a new Image, same dimensions as img.
draw_circle <br> *Added in v0.4.0*draw_circle(img, cx, cy, radius, color=, [thickness=1], [filled=false])img is an Image; cx, cy are the center (pixel coordinates); radius is a positive number. filled=true fills the digital disk (every pixel with dx^2+dy^2 <= radius^2); filled=false (default) strokes the boundary using the textbook midpoint (Bresenham) circle algorithm at the given thickness. Returns a new Image, same dimensions as img.
draw_arrow <br> *Added in v0.4.0*draw_arrow(img, x0, y0, x1, y1, color=, [thickness=1], [head_size=10])img is an Image; draws a draw_line-style shaft from (x0,y0) to (x1,y1), plus an arrowhead: two more strokes from the tip back along the reversed shaft direction, splayed +/-30 degrees, each head_size pixels long (a two-stroke arrowhead, not a filled triangle — chosen so the head reuses the exact same line rasterizer and thickness as the shaft). A zero-length shaft draws only the (single-point) shaft, with no head, since it has no defined direction. Returns a new Image, same dimensions as img.
draw_scale_bar <br> *Added in v0.4.0*draw_scale_bar(img, length_physical, [pixel_size=], [unit=], color=, [position="bottom-right"], [margin=10], [thickness=4], [label=true], [font_scale=1])img is an Image; length_physical is a positive number. Draws a horizontal bar whose PIXEL length is length_physical / pixel_size, rounded to the nearest pixel — reusing image.regions's own pixel_size=/unit= convention exactly: omitting pixel_size= means length_physical is already a pixel count (unit= then defaults to, and must be, "px"); giving unit= with no pixel_size=, or unit="px" alongside one, are refused for the same reason image.regions refuses them (a unit with no scale, or a scale with a contradictory unit). position= is one of "bottom-left"/"bottom-right" (default)/"top-left"/"top-right", placed margin= pixels (default 10) from that corner. thickness= (default 4) is the bar's pixel height. label= (default true) draws length_physical and the unit underneath (bottom positions: above, to stay on-canvas) the bar using a built-in bitmap font — digits, ., -, %, and the handful of unit letters (u, n, m, c, p, x, k, i) a physical unit actually needs; any other character renders as blank space rather than an error. font_scale= (default 1) is an integer pixel-doubling factor for the label. Returns a new Image, same dimensions as img.
img = image_new(300, 200, r=20, g=20, b=20)
img = draw_line(img, 10, 10, 290, 10, color="yellow", thickness=2)
img = draw_rect(img, 20, 30, 60, 40, color="cyan", thickness=2)
img = draw_circle(img, 150, 100, 30, color="magenta", filled=true)
img = draw_arrow(img, 200, 150, 260, 180, color="white", head_size=12)

# A 100 px bar labeled "50 um" when pixel_size = 0.5 um/px.

img = draw_scale_bar(img, 50, pixel_size=0.5, unit="um", position="bottom-right", color="white")
save_image("/tmp/annotated.bmp", img)

Gradients, Edges & Colorspace (image module)

Reached through import image, the same namespaced module as image.canny/ image.bilateral/image.regions above — always call these qualified (image.sobel(...), not bare sobel(...)), since a bare blur/sobel-shaped name may already exist as an unrelated core builtin (blur does; see the "Filtering & Enhancement" table above).

Unlike the point/neighbourhood filters above, these return raw signed floating-point results rather than a clamped 8-bit preview Image — sobel/scharr/laplacian/gradient_magnitude return a Mat (same shape as image.luma's own return), and rgb2hsv/rgb2lab return a Record of three same-shape Mats rather than a Value::Image, because hue (0–360) and Lab's a\*/b\* (roughly -128..127) cannot be held in an 8-bit RGB pixel without truncating them — this codebase's Image type has no colorspace tag (a known, deliberately deferred gap; see docs/design/toolkit-image.md).

FunctionSignatureDescription
image.sobelimage.sobel(img, [direction="both"])img is an Image. direction is an optional named string, "x", "y", or "both" (default). Computes the 3x3 Sobel gradient over img's BT.601 luma (replicate/clamped border). direction="x"/"y" return the raw signed gradient component; the default "both" returns the gradient MAGNITUDE sqrt(gx^2 + gy^2) (identical to image.gradient_magnitude(img)). Named direction=, not axis= — axis= is a reserved keyword the call machinery intercepts itself for sum/mean-style row/column reductions and would never reach this function. Returns a Mat, same height x width shape as img. Added in v0.4.0.
image.scharrimage.scharr(img, [direction="both"])Same signature, same direction= convention, and the same border/luma handling as image.sobel, but with the Scharr 3x3 kernel ([-3,0,3; -10,0,10; -3,0,3], transposed for the y-direction) in place of Sobel's — better rotational symmetry, the textbook reason to reach for it when the gradient's DIRECTION matters and not only where an edge is. Returns a Mat. Added in v0.4.0.
image.laplacianimage.laplacian(img, [kernel_size=3])img is an Image; kernel_size is an optional named number, 3 (default, the same 4-neighbour kernel edge_detect applies per-channel and clamps to 8-bit) or 5 (a wider, less noise-sensitive discrete Laplacian). Any other kernel_size is refused by name. Computes the discrete Laplacian over img's BT.601 luma as raw signed floats (unlike edge_detect, which clamps to a preview Image). Returns a Mat. Added in v0.4.0.
image.gradient_magnitudeimage.gradient_magnitude(img)img is an Image. Gradient magnitude sqrt(gx^2 + gy^2) from the Sobel gradient over img's BT.601 luma — the same value image.sobel(img)/image.sobel(img, direction="both") returns, under its own more discoverable name. Returns a Mat. Added in v0.4.0.
image.rgb2hsvimage.rgb2hsv(img)img is an Image. Per-pixel RGB→HSV conversion (the whole-Image counterpart to the scalar to_hsv), reusing the exact same conversion the scalar builtin uses. Returns a Record with fields h (hue, degrees 0–360), s, v (each 0–1), each a Mat the same height x width shape as img. Added in v0.4.0.
image.hsv2rgbimage.hsv2rgb(hsv)hsv is a Record with h/s/v Mat fields, the shape image.rgb2hsv returns (a plain Image is refused by name, since it is the wrong shape — that is what rgb2hsv itself takes, not what it returns). Converts back to 8-bit RGB, rounding and clamping each channel to 0..255. Returns a new Image, same dimensions as the input Mats. Added in v0.4.0.
image.rgb2labimage.rgb2lab(img)img is an Image. Per-pixel sRGB→CIE L\*a\*b\* conversion (D65 white point), the whole-Image counterpart to the scalar to_lab, reusing its exact conversion math. Returns a Record with fields l (0–100), a, b (roughly -128..127), each a Mat the same shape as img. Added in v0.4.0.
image.lab2rgbimage.lab2rgb(lab)lab is a Record with l/a/b Mat fields, the shape image.rgb2lab returns. Converts back to 8-bit sRGB, clamped into gamut. Returns a new Image. Added in v0.4.0.

Gradient direction and a colorspace round trip

import image

# A vertical edge: left half dark, right half bright.

m = zeros(4, 8)
for y in 0 to 3
  for x in 4 to 7
    m[y, x] = 255
  end for
end for
img = image_from_matrix(m)

gx = image.sobel(img, direction="x")
gy = image.sobel(img, direction="y")
print(gx[0, 3], gx[0, 4])   # 1020 1020 -- the edge, straddled from both sides

print(gy[0, 3])             # 0 -- no variation along y at all


mag = image.gradient_magnitude(img)
print(mag[0, 3] == gx[0, 3])   # true -- gy is zero here, so magnitude reduces to |gx|


# A colorspace round trip: pure red is hue 0, full saturation, full value.

red = image_new(2, 2, r=255, g=0, b=0)
hsv = image.rgb2hsv(red)
print(hsv.h[0, 0], hsv.s[0, 0], hsv.v[0, 0])   # 0 1 1


back = image.hsv2rgb(hsv)
print(back.width, back.height)   # 2 2

output
1020 1020
0
true
0 1 1
2 2

Vector Graphics (SVG)

Everything above this point is raster: an Image is a grid of pixels. This section is the vector counterpart — objects and coordinates, written out as SVG. The two do not mix: save_image writes pixels, save_svg writes shapes.

Two things this is deliberately *not*:

Shapes are built by the svg.* constructors and collected into a list, which save_svg writes. Each constructor returns an ordinary Record, so a shape can be inspected (s.kind, s.width) and built by hand if you prefer. The constructors are namespaced because rect, circle and text already exist as unrelated *plotting* commands that annotate the current figure — svg.circle(...) and circle(...) are different functions in different domains. Reach them with import svg, and always call them qualified.

FunctionSignatureDescription
svg.rectsvg.rect(x, y, width, height, [fill="#000000"], [stroke="none"], [stroke_width=1], [opacity=1])An axis-aligned rectangle with its top-left corner at (x, y). All four positional arguments are finite numbers; width/height must be 0 or more. fill/stroke are color strings ("#e74c3c", "red", or "none") — a number is refused, because fill="3" is an invalid paint that every renderer silently falls back to black on. Returns a Record with fields kind, x, y, width, height, fill, stroke, stroke_width, opacity. Added in v0.3.0.
svg.circlesvg.circle(cx, cy, r, [fill="#000000"], [stroke="none"], [stroke_width=1], [opacity=1])A circle of radius r centred on (cx, cy); r must be 0 or more. Styling as for svg.rect. Returns a Record with fields kind, cx, cy, r and the four styling fields. Added in v0.3.0.
svg.linesvg.line(x1, y1, x2, y2, [fill="none"], [stroke="#000000"], [stroke_width=1], [opacity=1])A straight segment from (x1, y1) to (x2, y2). Note the defaults are inverted relative to the area shapes: a line has no interior to fill, so it defaults to stroked-black and unfilled. Returns a Record with fields kind, x1, y1, x2, y2 and the four styling fields. Added in v0.3.0.
svg.pathsvg.path(d, [fill="none"], [stroke="#000000"], [stroke_width=1], [opacity=1])An arbitrary path from the SVG path-data string d. Supports M/L (moveto/lineto), H/V (axis forms), C/Q (cubic and quadratic Bezier) and Z (closepath), each in both absolute (uppercase) and relative (lowercase) spelling, with the SVG repetition rule — a repeated moveto's extra coordinate pairs are implicit linetos. Arcs (A) and the smooth-curve shorthands (S/T) are refused by name rather than passed through, since an unvalidated d produces a file that opens without complaint and draws nothing. d is re-emitted normalized to single-space separation. Returns a Record with fields kind, d and the four styling fields. Added in v0.3.0.
svg.textsvg.text(x, y, text, [font_size=16], [font_family="sans-serif"], [fill="#000000"], [stroke="none"], [stroke_width=1], [opacity=1])A text run anchored at (x, y) — which in SVG is the *baseline start*, not the top-left corner. font_size must be more than 0. The content is XML-escaped on write, so & and < in a label are safe. Returns a Record with fields kind, x, y, text, font_size, font_family and the four styling fields. Added in v0.3.0.
save_svgsave_svg(path, shapes, [width=800], [height=600])path is a string destination; shapes is a List of shape records (a single record on its own is also accepted). Writes a standalone SVG document with an XML declaration, the SVG namespace, and both width/height and a matching viewBox — a viewBox alone renders at the container's size rather than the file's own. The canvas is not auto-fitted to the shapes: a text run's rendered width depends on a font this writer does not load, so an automatic canvas would be right for the geometry and silently clip the labels. Pass width=/height= for a tight canvas. Returns nothing. Added in v0.3.0.
load_svgload_svg(path)Reads an SVG file back. Returns a Record with fields width, height (from the width/height attributes, falling back to the viewBox when those are absent or a percentage; absolute CSS units px/pt/pc/in/cm/mm are converted to user units) and shapes, a List of shape records in document order. Reads the presentation attributes of rect, circle, line, path and text, descending into <g> groups. It does not apply transform=, resolve CSS (<style>, style=, class=), inherit styling from an ancestor group, or resolve <use>/<symbol>/gradients/patterns/masks/clip paths, and it skips <ellipse>/<polygon>/<polyline>/<image>. Unrecognized elements are skipped rather than rejected, so "loaded without error" does not mean "read everything in the file" — a round trip of a file save_svg wrote is exact, and that is the case it is built for. A file with no <svg> root is refused by name. Added in v0.3.0.

Drawing and reading back an SVG

import svg

shapes = [
  svg.rect(10, 10, 100, 50, fill = "#e74c3c"),
  svg.circle(200, 60, 40, fill = "#3498db", stroke = "#000000", stroke_width = 2),
  svg.line(0, 0, 300, 120, stroke = "#333333", stroke_width = 1.5),
  svg.path("M 10 100 L 60 40 L 110 100 Z", stroke = "#2ecc71", stroke_width = 2),
  svg.text(20, 150, "R&D output", font_size = 16, fill = "#111111")]

save_svg("drawing.svg", shapes, width = 320, height = 180)

doc = load_svg("drawing.svg")
print(doc.width)             # 320

print(len(doc.shapes))       # 5

print(doc.shapes[1].kind)    # circle

print(doc.shapes[1].r)       # 40

output
320
5
circle
40

Examples

Load, grayscale, and display

grayscale is usually the first step in a CV pipeline that doesn't care about color: it collapses a loaded RGB image down to R=G=B luma so that every downstream filter (edge detection, thresholding, morphology) only has to reason about one intensity value per pixel instead of three. The example below loads a photo, converts it, and shows it inline — the resulting figure looks identical in shape and size to the original, but every trace of color is gone, leaving a flat grey rendition where only brightness carries information.

# A gradient with a bright bar in it, so the rotation below is visible:

# a symmetric test image would look identical afterwards.

n = 48
g = zeros(n, n)
for i in 0 to n - 1
    for j in 0 to n - 1
        g[i, j] = 40 + 3 * j
        if i > 10 and i < 22 and j > 6 and j < 30
            g[i, j] = 240
        end if
    end for
end for
save_image("sample.bmp", image_from_matrix(g))

img = load_image("sample.bmp")
gray = grayscale(img)
imshow(gray)

Edge detection

edge_detect runs a small Laplacian kernel over the image, so it responds strongly wherever intensity jumps sharply — silhouettes, text edges, texture boundaries — and stays near zero across smooth regions. Feeding it a grayscale image first (rather than raw color) keeps that response consistent across channels. The resulting figure is mostly dark, with the outlines of objects in the original photo picked out as thin bright (or high-contrast) lines, roughly like a pencil sketch of the scene's contours.

img = load_image("sample.bmp")
edges = edge_detect(grayscale(img))
imshow(edges)

Otsu thresholding and blob analysis

This is the classic segment-then-measure workflow: otsu_threshold picks a data-driven cutoff between foreground and background without any manual tuning, threshold applies it to get a binary mask, bwareaopen scrubs out tiny specks that are just noise, and bwlabel/regionprops turn the cleaned mask into a list of discrete objects with their own area and centroid. Viewed as an image, mask is stark black-and-white; clean is the same but with small isolated white flecks removed, leaving only the blobs large enough to be real objects (e.g. cells in a microscopy image). The printed rows give one line of measurements — label id, area in pixels, and centroid coordinates — per surviving blob.

# Three bright discs on a dark field, written out and read back. The blob

# count printed below is then checkable against an image the reader can

# see being built, rather than asserted about a file nobody has.

n = 60
field = zeros(n, n)
centres = [15, 15; 42, 20; 30, 45]
for i in 0 to n - 1
    for j in 0 to n - 1
        for k in 0 to 2
            dy = i - centres[k, 0]
            dx = j - centres[k, 1]
            if dy * dy + dx * dx < 64
                field[i, j] = 230
            end if
        end for
    end for
end for
save_image("cells.bmp", image_from_matrix(field))

img = load_image("cells.bmp")
level = otsu_threshold(img)
mask = threshold(img, level)
clean = bwareaopen(mask, 20)          # drop specks smaller than 20 px

imshow(clean)

labeled = bwlabel(clean)
print(labeled.count)                  # number of detected blobs


stats = regionprops(labeled)
for i = 1 to labeled.count
    r = stats[i - 1]
    print(r.label, r.area, r.centroid_x, r.centroid_y)
end for
output
3
1 193 15 15
2 193 45 30
3 193 20 42

Composable affine transform via imwarp

imwarp is the general-purpose way to move pixels: build up any combination of translation, rotation, scale, and shear as plain 3x3 matrices, multiply them together with Qu's own * operator, and apply the whole thing in a single resampling pass. Here a rotation and a translation are composed into one matrix and applied at once, with fill=255 painting the newly-exposed corners white instead of the default black. The resulting figure shows the source image rotated 30 degrees and shifted down and to the right, on a larger white canvas that has grown (bbox="loose") to fit the rotated content without clipping any of it.

img = load_image("sample.bmp")

# Rotate 30 degrees about the origin, then shift right/down by (50, 20).

M = affine_translate(50, 20) * affine_rotate(30)
out = imwarp(img, M, method="bilinear", bbox="loose", fill=255)
imshow(out)

Direct rotation with imrotate

For the common case of "just rotate the image," imrotate is more direct than composing an affine matrix by hand: give it a plain angle in degrees and it handles the center-of-rotation bookkeeping itself. The figure below shows the source image rotated 45 degrees counterclockwise, on a canvas that has grown (bbox="loose") enough to contain every rotated corner — the four original corners now sit inside a larger, diamond-oriented bounding image with black fill in the newly-exposed background.

img = load_image("sample.bmp")
rotated = imrotate(img, 45, method="bilinear", bbox="loose")
imshow(rotated)

Contrast enhancement and histogram

imadjust fixes an image that looks washed-out or too dark by stretching its intensity range to fill 0–1 (auto-picking the 1st/99th percentile as the input range when none is given), and imhist turns the resulting pixel intensities into a plain count-per-bin Vec that bar can plot. Starting from a very dark, low-contrast image, bright looks visibly lighter and punchier after the stretch; the accompanying bar chart shows intensity counts spread out across the bin range rather than bunched up near zero the way the original dark image's histogram would be.

# Deliberately dark and low-contrast -- everything between 28 and 42 of

# 255 -- so there is something for `imadjust` to stretch.

n = 48
d = zeros(n, n)
for i in 0 to n - 1
    for j in 0 to n - 1
        d[i, j] = 28 + 14 * (i + j) / (2 * n)
    end for
end for
save_image("dark.bmp", image_from_matrix(d))

img = load_image("dark.bmp")
bright = imadjust(img)                # auto-stretches to the 1st/99th percentile

imshow(bright)
counts = imhist(bright, n_bins=64)
bar(counts)
output
· bar: figure 8 (64 points)

A runnable image

The examples above open files that are not in this repository, so they show the calls without showing the result. Building the image in Qu instead means the rest can actually run:

w = 120
h = 120
M = zeros(h, w)
for r in 0 to h - 1
    for c in 0 to w - 1
        M[r, c] = 40 + 60 * sin(c / 9) * cos(r / 14)
    end for
end for
for r in 45 to 75
    for c in 30 to 60
        M[r, c] = 210
    end for
end for

img = image_from_matrix(M)
imshow(img)
title("a synthetic field with one bright patch")
a synthetic field with one bright patch

image_from_matrix takes values in 0-255. Anything that produces a matrix produces an image, which is the useful direction: a simulation result, a correlation surface, or a measurement grid can be looked at with the same tools as a photograph.

More functions

FunctionSignatureDescription
imerodeimerode(img, radius) <br> imerode(img, se=)img is a binary Image; radius is a positive integer, the half-width of a square structuring element of side 2*radius+1. Binary morphological erosion — shrinks foreground regions and removes specks smaller than the element. Returns a new Image, same dimensions as img. Same additive se= kwarg as imdilate (see "Structuring elements" above), added in v0.4.0.
imopenimopen(img, radius) <br> imopen(img, se=)img is a binary Image; radius is a positive integer (same structuring-element convention as imerode). Morphological opening (erode then dilate) — removes specks smaller than the structuring element while leaving larger shapes their size. Returns a new Image, same dimensions as img. Same additive se= kwarg as imdilate (see "Structuring elements" above), added in v0.4.0.
imtophatimtophat(img, radius) <br> imtophat(img, se=)img is a binary/grayscale Image; radius is a positive integer. The image minus its opening: what the opening removed. The standard way to pull small bright features off an uneven background. Returns a new Image, same dimensions as img. Same additive se= kwarg as imdilate (see "Structuring elements" above), added in v0.4.0.
imequalizeimequalize(img)img is an Image of any size. Histogram equalisation, spreading the intensities to fill the range (identical operation to histeq above). Reveals detail in a flat image and exaggerates noise in a clean one. Returns a new Image, same dimensions as img.
remove_small_blobsremove_small_blobs(img, min_area)img is a binary Image; min_area is a number, the minimum pixel area a connected component must have to survive. Drop connected components below that area, after labelling internally (identical operation to bwareaopen above). The cheap alternative to tuning a filter. Returns a new binary Image, same dimensions as img.
label_blobslabel_blobs(img)img is a binary Image. Connected-component labelling: every separate region gets its own integer (identical operation to bwlabel above). Returns a Model (kind "blobs") with .labels, .count, .width, .height fields.
blob_statsblob_stats(img)img is a Model produced by label_blobs/bwlabel. Per-region area, centroid and bounding box, from a labelled image (identical operation to regionprops above). Returns a List of Records, one per blob.

Building an image from scratch

No file on disk needed: image_new makes a blank canvas, and image_from_matrix turns any numeric matrix straight into a grayscale image. resize/crop then reshape it, imagesc shows the raw matrix as a false-color raster, and save_image writes real pixels to a scratch file. Running this script produces two very different-looking figures side by side: imagesc(m) renders the tiny 3x3 matrix as a smooth, colorful viridis heatmap where each of the 9 cells is one large colored square, while the final imshow(piece) shows a flat, uniformly light-grey rectangle — a 30x20 crop taken from the middle of the enlarged solid-grey canvas, with no texture since the canvas started as one flat color.

canvas = image_new(40, 30, r=200, g=200, b=200)

m = [10, 40, 250; 90, 140, 190; 220, 255, 30]
gray = image_from_matrix(m)
save_image("/tmp/gray.bmp", gray)

imagesc(m, colormap="viridis")          # false-color view of the same matrix


big = resize(canvas, 80, 60, method="bilinear")
piece = crop(big, 10, 10, 30, 20)
print(piece.width, piece.height)
imshow(piece)

Geometric transforms: translate, scale, and compose an affine matrix

imtranslate/imscale are ready-made pixel moves; affine_identity, affine_scale, and affine_shear build the raw 3x3 matrices that imwarp (see above) applies for anything more custom. Starting from a plain black 20x20 square, shifted is visually identical black content but now sitting on a canvas whose freshly-exposed left/top edge strip is filled white (fill=255) where the shift moved content away from it; the printed matrix M shows the combined scale-then-shear transform as raw numbers rather than a rendered image, since it is never applied to a pixel buffer in this example.

img = image_new(20, 20, r=0, g=0, b=0)

shifted = imtranslate(img, 5, 3, fill=255)
scaled = imscale(img, 1.5, fill=0)
imshow(shifted)

M = affine_scale(2, 1.5) * affine_shear(0.2, 0) * affine_identity()
print(shifted.width, scaled.width, M)
output
20 30 [2, 0.4, 0; 0, 1.5, 0; 0, 0, 1]
a synthetic field with one bright patch

Convolution filters: blur, sharpen, and a custom kernel

blur/sharpen are fixed 3x3 kernels; imfilter takes any odd-sized kernel for cases those don't cover. The source image here is a tight stripe pattern (alternating dark and light pixels); soft visibly smears those stripes into softer, blended bands, crisp pushes the opposite way and exaggerates the contrast between adjacent stripes so the pattern looks harsher, and custom — built from an explicit unsharp kernel via imfilter — looks similar to crisp but demonstrates that the same effect can be hand-specified as a raw kernel matrix rather than relying on the built-in sharpen.

import image          # blur is also a core builtin, so with the module open it is called qualified


m = [10, 200, 10, 200, 10; 200, 10, 200, 10, 200; 10, 200, 10, 200, 10]
img = image_from_matrix(m)

soft = image.blur(img)
crisp = sharpen(img)
imshow(soft)
imshow(crisp)

kernel = [0, -1, 0; -1, 5, -1; 0, -1, 0]      # explicit unsharp kernel

custom = imfilter(img, kernel)
print(soft.width, crisp.width, custom.width)
output
5 5 5
a synthetic field with one bright patch

Histogram equalization and synthetic noise

histeq and imequalize are the same operation under two names. imnoise adds Gaussian or salt-and-pepper noise, handy for testing the filters above. The source matrix here is a low-contrast image with all values clustered tightly around 88; after equalization, eq spreads those near-identical grey values apart so the same pattern shows visibly more contrast between neighboring pixels. noisy_sp looks like the original image but with a scattering of pure-black and pure-white pixels speckled across it — the "salt and pepper" the name describes — while a Gaussian-noise version would instead show a subtler, grainy variation across every pixel rather than isolated extreme dots.

m = [80, 90, 85, 95; 88, 92, 84, 91; 86, 89, 93, 87; 90, 85, 88, 92]
img = image_from_matrix(m)

eq = histeq(img)
eq2 = imequalize(img)                          # same result, other name

imshow(eq)

noisy_g = imnoise(img, "gaussian", sigma=0.05, seed=1)
noisy_sp = imnoise(img, "salt_pepper", density=0.1, seed=2)
imshow(noisy_sp)
print(eq.width, eq2.width, noisy_g.width, noisy_sp.width)
output
4 4 4 4
a synthetic field with one bright patch

Morphology: erode, dilate, open, close, and the hat transforms

The four base operations (imerode/imdilate/imopen/imclose) and their bright/dark-feature duals (imtophat/imbothat) on one small speckled image. The source img is a binary image with two solid 2x2 blobs, one lone single-pixel speck, and one small gap between two foreground pixels. Displayed side by side, img shows all of that detail; opened looks like img but with the lone speck erased, since opening removes anything smaller than the structuring element while leaving the larger 2x2 blobs untouched — eroded would shrink every blob including erasing the thin ones entirely, dilated would grow every blob and fuse nearby ones together, and closed would fill the small gap between neighboring foreground pixels.

m = [0, 0, 0, 0, 0, 0; 0, 255, 255, 0, 255, 0; 0, 255, 255, 0, 0, 0; 0, 0, 0, 0, 0, 0; 0, 255, 0, 0, 255, 255; 0, 0, 0, 0, 255, 255]
img = image_from_matrix(m)
imshow(img)

eroded = imerode(img, 1)
dilated = imdilate(img, 1)
opened = imopen(img, 1)                # drops the lone speck

closed = imclose(img, 1)               # fills small gaps

bright = imtophat(img, 1)              # small bright features

dark = imbothat(img, 1)                # small dark features

imshow(opened)
print(eroded.width, dilated.width, opened.width, closed.width, bright.width, dark.width)
output
6 6 6 6 6 6
a synthetic field with one bright patch

Thresholding: multi-level, Otsu, and Kapur

otsu computes-and-applies in one step; multi_otsu/kapur_threshold compute cut points that feed into multithreshold; foreground_mask thresholds and cleans up specks in one call. The source matrix is a 4x4 image with two clearly separated intensity groups (dark values near 10–30 and bright values near 200–230). seg (from otsu) renders as a crisp black-and-white split along that boundary; banded, built from multi_otsu's two cut points, instead shows three distinct grey bands rather than a plain binary split, since it quantizes into more classes than a single threshold could.

m = [10, 20, 30, 220; 15, 25, 200, 230; 120, 130, 210, 225; 125, 135, 205, 215]
img = image_from_matrix(m)

seg = otsu(img)                             # compute Otsu's level and apply it

imshow(seg)

cuts = multi_otsu(img, 3)                   # two cut points -> three bands

banded = multithreshold(img, cuts)
imshow(banded)

k = kapur_threshold(img)                    # entropy-based single threshold

fg = foreground_mask(img, k, radius=1)      # threshold + speck cleanup

print(cuts, k, seg.width, banded.width, fg.width)
output
[30.625, 135.46875] 130.3125 4 4 4
a synthetic field with one bright patch

Cleaning and measuring blobs

remove_small_blobs drops specks by area, label_blobs assigns each remaining region an id, and blob_stats reports area/centroid/bbox per region. The source binary image contains one solid 2x2 white blob in the top-left corner, one lone single white pixel next to it, and one solid 2x2 white blob in the bottom-right corner. After remove_small_blobs(binary, 3), the resulting cleaned image looks identical to the source except the lone single-pixel speck has vanished, leaving only the two real 2x2 blobs — which is exactly what the printed loop then reports: two rows, each with a label id, an area of 4 pixels, and a centroid at the geometric middle of its own 2x2 square.

m = [255, 255, 0, 0, 0, 0; 255, 255, 0, 255, 0, 0; 0, 0, 0, 0, 0, 0; 0, 255, 0, 0, 0, 0; 0, 0, 0, 0, 255, 255; 0, 0, 0, 0, 255, 255]
binary = image_from_matrix(m)

cleaned = remove_small_blobs(binary, 3)     # drop the two 1-pixel specks

imshow(cleaned)
labeled = label_blobs(cleaned)
stats = blob_stats(labeled)

for i = 1 to labeled.count
    r = stats[i - 1]
    print(r.label, r.area, r.centroid_x, r.centroid_y)
end for
output
1 4 0.5 0.5
2 4 4.5 4.5
a synthetic field with one bright patch