github ↗
docs menu

sowilo

contrib

Differentiable computer vision on Nx tensors.

install
opam install sowilo
built on
nx · hugin

Sowilo provides image processing operations expressed purely through Nx tensor operations. Filters, edge detectors, morphological operations, color transforms, and geometric transforms -- all compatible with Rune.grad' and Rune.vmap'.

Image conventions

Images are Nx.t tensors with channels-last layout:

  • Single image: [H; W; C] (height, width, channels)
  • Batch: [N; H; W; C] (batch, height, width, channels)
  • Grayscale: C = 1, RGB: C = 3, RGBA: C = 4

Operations expect float32 tensors with values in [0, 1]. Use to_float to convert from uint8 and to_uint8 to convert back.

An image lies on any device set, and an operation computes where it lies: its kernels and constants are made beside the image, and its result is on the image's set, as the Device sets section of sowilo.mli states.

What's included

  • Type conversion: to_float, to_uint8, normalize, threshold
  • Color: to_grayscale, rgb_to_hsv, hsv_to_rgb, brightness, contrast, saturation, hue, gamma, invert
  • Geometric transforms: resize, crop, center_crop, hflip, vflip, rotate90, pad
  • Spatial filters: gaussian_blur, box_blur, median_blur, filter2d, unsharp_mask
  • Morphology: structuring_element, erode, dilate, opening, closing, morphological_gradient
  • Edge detection: sobel, scharr, laplacian, canny

Quick start

ocaml
open Sowilo

let () =
  (* Load image and convert to float32 [0, 1] *)
  let img = Nx_io.load_image "photo.png" |> to_float in

  (* Process: grayscale, blur, edge detection *)
  let edges =
    img
    |> to_grayscale
    |> gaussian_blur ~sigma:1.0
    |> canny ~low:0.2 ~high:0.6
  in

  (* Save the boolean edge mask as an image *)
  Nx_io.save_png "edges.png" (to_uint8 (Nx.cast Nx.float32 edges))

Learn more

Examples

  • 01-grayscale ↗Convert a color image to grayscale using Sowilo.to_grayscale and render the original and result side by side to grayscale.png with Hugin.
  • 02-gaussian-blur ↗Apply a Gaussian blur with Sowilo.gaussian_blur to smooth a grayscale image.
  • 03-median-blur ↗Apply a median filter with Sowilo.median_blur for noise removal.
  • 04-threshold ↗Binarize a grayscale image with Sowilo.threshold.
  • 05-sobel ↗Compute horizontal and vertical edge gradients with Sowilo.sobel.
  • 06-canny ↗Detect edges with the Canny algorithm using Sowilo.canny.
  • 07-morphology ↗Apply morphological erosion and dilation with Sowilo.erode and Sowilo.dilate using a rectangular structuring element.

this page is tested with mdxedit this page ↗