package sowilo
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=a9a8a9787f8250337187bb7b21cb317c41bfd2ecf08bcfe0ab407c7b6660764d
sha512=fe13cf257c487e41efe2967be147d80fa94bac8996d3aab2b8fd16f0bbbd108c15e0e58c025ec9bf294d4a0d220ca2ba00c3b1b42fa2143f758c5f0ee4c15782
doc/sowilo/Sowilo/index.html
Module Sowilo
Source
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.
uint8_t
3-D (H; W; C
) or 4-D (N; H; W; C
) unsigned 8-bit image tensor type.
float32_t
3-D or 4-D single-precision float image tensor type, with values typically normalized to 0.0, 1.0
.
int16_t
3-D or 4-D signed 16-bit integer tensor type, commonly used for derivative filters (e.g., Sobel).
Basic Image Manipulations
flip_vertical img
Flip the image vertically (top to bottom).
Parameters
img
: input image tensor (H; W; C
orN; 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;*} *)
flip_horizontal img
Flip the image horizontally (left to right).
Parameters
img
: input image tensor (H; W; C
orN; 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} *)
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
to_grayscale img
Convert a color image to grayscale using standard luminance weights.
Parameters
img
: input uint8 tensor (H; W; C
orN; H; W; C
) with C>=1.
Returns
- grayscale tensor (
H; W; 1
orN; 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 *)
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} *)
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
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 *)
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
Interpolation methods for image resizing.
val 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 orLinear
).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
val 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 tosigmaX
.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 anyksize
component is even or non-positive.
Examples
let blurred = gaussian_blur ~ksize:(5,5) ~sigmaX:1.0 img in
...
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
...
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
...
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
ifksize
is not a positive odd integer.
Examples
let clean = median_blur ~ksize:3 gray_img in
...
Morphological Operations
Shapes for structuring elements in morphological operations.
val 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
orCross
).ksize
: (height, width) of the kernel; positive odd integers.
Returns
- uint8 tensor of shape
|height; width|
with ones at active elements.
Raises
Invalid_argument
ifksize
components are invalid.
Examples
let k = get_structuring_element ~shape:Rect ~ksize:(3,3) in
...
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
...
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
Types of fixed-level thresholding operations.
val 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 forBinary
/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
ifthresh
ormaxval
are out of range.
Examples
let bw = threshold ~thresh:128 ~maxval:255 ~type_:Binary gray in
(* values >128 become 255, else 0 *)
Edge Detection
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 unsupportedksize
is provided.
Examples
let gx = sobel ~dx:1 ~dy:0 img in
(* x-gradient of image *)
val 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 *)