package sowilo

  1. Overview
  2. Docs
Computer vision extensions for Rune

Install

Dune Dependency

Authors

Maintainers

Sources

raven-1.0.0.alpha0.tbz
sha256=a9a8a9787f8250337187bb7b21cb317c41bfd2ecf08bcfe0ab407c7b6660764d
sha512=fe13cf257c487e41efe2967be147d80fa94bac8996d3aab2b8fd16f0bbbd108c15e0e58c025ec9bf294d4a0d220ca2ba00c3b1b42fa2143f758c5f0ee4c15782

doc/sowilo/Sowilo/index.html

Module SowiloSource

Sowilo: Computer vision operations on Rune.

This module provides image manipulation, color conversion, datatype conversion, resizing, filtering, morphological operations, thresholding, and edge detection using Rune buffers.

Sourcetype 'dev uint8_t = 'dev Rune.uint8_t

uint8_t

3-D (H; W; C) or 4-D (N; H; W; C) unsigned 8-bit image tensor type.

Sourcetype 'dev float32_t = 'dev Rune.float32_t

float32_t

3-D or 4-D single-precision float image tensor type, with values typically normalized to 0.0, 1.0.

Sourcetype 'dev int16_t = 'dev Rune.int16_t

int16_t

3-D or 4-D signed 16-bit integer tensor type, commonly used for derivative filters (e.g., Sobel).

Basic Image Manipulations

Sourceval flip_vertical : 'dev uint8_t -> 'dev uint8_t

flip_vertical img

Flip the image vertically (top to bottom).

Parameters

  • img: input image tensor (H; W; C or N; H; W; C).

Returns

  • tensor with rows reversed along the vertical axis.

Notes

  • Preserves datatype and channel count.

Examples

  let flipped = flip_vertical img in
  (* flipped.{0;0;*} = img.{0;H-1;*} *)
Sourceval flip_horizontal : 'dev uint8_t -> 'dev uint8_t

flip_horizontal img

Flip the image horizontally (left to right).

Parameters

  • img: input image tensor (H; W; C or N; H; W; C).

Returns

  • tensor with columns reversed along the horizontal axis.

Notes

  • Preserves datatype and channel count.

Examples

  let flipped = flip_horizontal img in
  (* flipped.{0;*;0} = img.{0;*;W-1} *)
Sourceval crop : y:int -> x:int -> height:int -> width:int -> 'dev uint8_t -> 'dev uint8_t

crop ~y ~x ~height ~width img

Extract a rectangular region of interest from the image.

Parameters

  • y: starting row index (0-based).
  • x: starting column index (0-based).
  • height: number of rows in the crop.
  • width: number of columns in the crop.
  • img: input image tensor of rank 3 (H; W; C).

Returns

  • tensor of shape |height; width; C| containing the cropped region.

Raises

  • Invalid_argument if the specified region exceeds image bounds.

Examples

  let roi = crop ~y:10 ~x:20 ~height:50 ~width:50 img in
  (* roi has shape [50;50;C] *)

Color Space Conversion

Sourceval to_grayscale : 'dev uint8_t -> 'dev uint8_t

to_grayscale img

Convert a color image to grayscale using standard luminance weights.

Parameters

  • img: input uint8 tensor (H; W; C or N; H; W; C) with C>=1.

Returns

  • grayscale tensor (H; W; 1 or N; H; W; 1).

Notes

  • If input has multiple channels, uses weights 0.299, 0.587, 0.114.
  • If input has C=1, returns img unchanged.

Examples

  let gray = to_grayscale img in
  (* gray.{0;i;j;0} = 0.299*R + 0.587*G + 0.114*B *)
Sourceval rgb_to_bgr : 'dev uint8_t -> 'dev uint8_t

rgb_to_bgr img

Swap red and blue channels in an RGB image to produce BGR.

Parameters

  • img: input uint8 tensor with C=3.

Returns

  • tensor with channels reordered to BGR.

Examples

  let bgr = rgb_to_bgr img in
  (* bgr.{i;j;0} = img.{i;j;2} *)
Sourceval bgr_to_rgb : 'dev uint8_t -> 'dev uint8_t

bgr_to_rgb img

Swap blue and red channels in a BGR image to produce RGB.

Parameters

  • img: input uint8 tensor with C=3.

Returns

  • tensor with channels reordered to RGB.

Examples

  let rgb = bgr_to_rgb img in
  (* rgb.{i;j;2} = img.{i;j;0} *)

Data Type Conversion

Sourceval to_float : 'dev uint8_t -> 'dev float32_t

to_float img

Convert uint8 image values 0,255 to float32 0.0,1.0.

Parameters

  • img: input uint8 tensor.

Returns

  • float32 tensor of same shape with values scaled by 1.0 /. 255.0.

Examples

  let f = to_float img in
  (* f.{i;j;k} = float img.{i;j;k} /. 255.0 *)
Sourceval to_uint8 : 'dev float32_t -> 'dev uint8_t

to_uint8 img

Convert float32 image values 0.0,1.0 to uint8 0,255, with clipping.

Parameters

  • img: input float32 tensor.

Returns

  • uint8 tensor of same shape with values scaled by 255 and clipped to 0,255.

Examples

  let u = to_uint8 f in
  (* u.{i;j;k} = clamp(int_of_float (f.{i;j;k} *. 255.), 0, 255) *)

Image Resizing

Sourcetype interpolation =
  1. | Nearest
    (*

    nearest-neighbor interpolation (fast, may alias).

    *)
  2. | Linear
    (*

    bilinear interpolation (default).

    *)

Interpolation methods for image resizing.

Sourceval resize : ?interpolation:interpolation -> height:int -> width:int -> 'dev uint8_t -> 'dev uint8_t

resize ?interpolation ~height ~width img

Resize the image to the specified dimensions.

Parameters

  • ?interpolation: method (Nearest default or Linear).
  • height: target number of rows.
  • width: target number of columns.
  • img: input uint8 tensor of rank 3 (H; W; C) or rank 4 (N; H; W; C).

Returns

  • resized uint8 tensor with shape |...; height; width; C|.

Examples

  let small = resize ~height:100 ~width:200 img in
  (* small has shape [100;200;C] *)

Image Filtering

Sourceval gaussian_blur : ksize:(int * int) -> sigmaX:float -> ?sigmaY:float -> ('a, 'b, 'dev) Rune.t -> ('a, 'b, 'dev) Rune.t

gaussian_blur ~ksize ~sigmaX ?sigmaY img

Apply a Gaussian blur to the image.

Parameters

  • ksize: (height, width) of the Gaussian kernel; must be positive odd integers.
  • sigmaX: standard deviation in the X direction.
  • ?sigmaY: standard deviation in the Y direction; defaults to sigmaX.
  • img: input tensor of type uint8 or float32.

Returns

  • tensor of same type and shape as img, blurred by the Gaussian kernel.

Raises

  • Invalid_argument if any ksize component is even or non-positive.

Examples

  let blurred = gaussian_blur ~ksize:(5,5) ~sigmaX:1.0 img in
  ...
Sourceval box_filter : ksize:(int * int) -> ('a, 'b, 'dev) Rune.t -> ('a, 'b, 'dev) Rune.t

box_filter ~ksize img

Apply a normalized box (average) filter to the image.

Parameters

  • ksize: (height, width) of the filter kernel; must be positive integers.
  • img: input tensor of type uint8 or float32.

Returns

  • tensor of same type and shape as img, averaged over each kernel window.

Examples

  let avg = box_filter ~ksize:(3,3) img in
  ...
Sourceval blur : ksize:(int * int) -> ('a, 'b, 'dev) Rune.t -> ('a, 'b, 'dev) Rune.t

blur ~ksize img

Alias for box_filter ~ksize img, applying an average filter.

Parameters

  • ksize: (height, width) of the filter kernel.
  • img: input tensor.

Returns

  • tensor of same type and shape as img, blurred by box filter.

Examples

  let b = blur ~ksize:(5,5) img in
  ...
Sourceval median_blur : ksize:int -> 'dev uint8_t -> 'dev uint8_t

median_blur ~ksize img

Apply a median filter to a grayscale uint8 image to remove noise.

Parameters

  • ksize: size of the square kernel (positive odd integer).
  • img: input uint8 tensor of rank 3 or 4 with single channel.

Returns

  • uint8 tensor of same shape with median-filtered values.

Raises

  • Invalid_argument if ksize is not a positive odd integer.

Examples

  let clean = median_blur ~ksize:3 gray_img in
  ...

Morphological Operations

Sourcetype structuring_element_shape =
  1. | Rect
    (*

    full rectangle.

    *)
  2. | Cross
    (*

    cross-shaped element.

    *)

Shapes for structuring elements in morphological operations.

Sourceval get_structuring_element : shape:structuring_element_shape -> ksize:(int * int) -> device:'dev Rune.device -> 'dev uint8_t

get_structuring_element ~shape ~ksize

Create a structuring element for morphological operations.

Parameters

  • shape: element shape (Rect or Cross).
  • ksize: (height, width) of the kernel; positive odd integers.

Returns

  • uint8 tensor of shape |height; width| with ones at active elements.

Raises

  • Invalid_argument if ksize components are invalid.

Examples

  let k = get_structuring_element ~shape:Rect ~ksize:(3,3) in
  ...
Sourceval erode : kernel:'dev uint8_t -> 'dev uint8_t -> 'dev uint8_t

erode ~kernel img

Perform morphological erosion on a grayscale uint8 image.

Parameters

  • kernel: 2D uint8 structuring element.
  • img: input uint8 tensor with C=1.

Returns

  • eroded tensor where each pixel is the minimum over the kernel window.

Examples

  let e = erode ~kernel img in
  ...
Sourceval dilate : kernel:'dev uint8_t -> 'dev uint8_t -> 'dev uint8_t

dilate ~kernel img

Perform morphological dilation on a grayscale uint8 image.

Parameters

  • kernel: 2D uint8 structuring element.
  • img: input uint8 tensor with C=1.

Returns

  • dilated tensor where each pixel is the maximum over the kernel window.

Examples

  let d = dilate ~kernel img in
  ...

Image Thresholding

Sourcetype threshold_type =
  1. | Binary
    (*

    dst = maxval if src > thresh else 0.

    *)
  2. | BinaryInv
    (*

    dst = 0 if src > thresh else maxval.

    *)
  3. | Trunc
    (*

    dst = min(src, thresh).

    *)
  4. | ToZero
    (*

    dst = src if src > thresh else 0.

    *)
  5. | ToZeroInv
    (*

    dst = 0 if src > thresh else src.

    *)

Types of fixed-level thresholding operations.

Sourceval threshold : thresh:int -> maxval:int -> type_:threshold_type -> 'dev uint8_t -> 'dev uint8_t

threshold ~thresh ~maxval ~type_ img

Apply fixed-level thresholding to a grayscale uint8 image.

Parameters

  • thresh: threshold value.
  • maxval: value used for Binary/BinaryInv operations.
  • type_: thresholding type (threshold_type).
  • img: input uint8 tensor of shape [|H;W;1|] or [|N;H;W;1|].

Returns

  • uint8 tensor after thresholding (binary or truncated values).

Raises

  • Invalid_argument if thresh or maxval are out of range.

Examples

  let bw = threshold ~thresh:128 ~maxval:255 ~type_:Binary gray in
  (* values >128 become 255, else 0 *)

Edge Detection

Sourceval sobel : dx:int -> dy:int -> ?ksize:int -> 'dev uint8_t -> 'dev int16_t

sobel ~dx ~dy ?ksize img

Compute the Sobel derivative of a grayscale uint8 image.

Parameters

  • dx: derivative order in x direction (0 or 1).
  • dy: derivative order in y direction (0 or 1).
  • ?ksize: aperture size for Sobel operator (default 3; only 3 supported).
  • img: input uint8 tensor of shape [|H;W;1|] or [|N;H;W;1|].

Returns

  • int16 tensor of same shape, containing derivative values.

Raises

  • Invalid_argument if unsupported ksize is provided.

Examples

  let gx = sobel ~dx:1 ~dy:0 img in
  (* x-gradient of image *)
Sourceval canny : threshold1:float -> threshold2:float -> ?ksize:int -> 'dev uint8_t -> 'dev uint8_t

canny ~threshold1 ~threshold2 ?ksize img

Apply the Canny edge detector to a grayscale uint8 image.

Parameters

  • threshold1: first threshold for hysteresis procedure.
  • threshold2: second threshold for hysteresis procedure.
  • ?ksize: Sobel aperture size for gradient computation (default 3).
  • img: input uint8 tensor of shape [|H;W;1|] or [|N;H;W;1|].

Returns

  • uint8 tensor binary edge map (0 for non-edges, 255 for edges).

Raises

  • Invalid_argument if threshold values are invalid or out of range.

Examples

  let edges = canny ~threshold1:50. ~threshold2:150. img in
  (* binary edges *)
OCaml

Innovation. Community. Security.