Module rustml::sliding [] [src]

Sliding windows over strings, bytes and ranges for arbitrary dimensions.

A sliding window is often used in image processing, e.g. for object recognition. It is a technique where a small window is moved over the image and some image processing is performed on that sub-image, e.g. to detect objects. A sliding window is also useful for strings to extract all substrings of a fixed size.

A sliding window with a width w on an area with width a and a delta d returns the following set of numbers (i.e. positions of the window): {0, d, 2d, ..., w - d + 1}

A sliding window on an area with two dimensions returns a set of tuples of window positions. A sliding window on an area with more then two dimensions returns a set of vectors of window positions.

Examples

The recommended way to build a sliding window over an arbitrary number of ranges is to first call the function builder and to sequently call the method add of the returned instance for each dimension.

// Example of a sliding window over two dimensions.
use rustml::sliding::*;

// Create a sliding window over two dimensions. For the first dimension the
// size of the window is 10, the position is incremented by 25 and the
// area over which the window is moved is 100. for the second dimension
// the size of the window is 10, the position is incremented by 2 and the
// area over which the window is moved is 10.
let windows = builder().add(100, 10, 25).add(10, 1, 2).to_2d().unwrap();
assert_eq!(windows, vec![
    (0, 0), (25, 0), (50, 0), (75, 0),
    (0, 2), (25, 2), (50, 2), (75, 2),
    (0, 4), (25, 4), (50, 4), (75, 4),
    (0, 6), (25, 6), (50, 6), (75, 6),
    (0, 8), (25, 8), (50, 8), (75, 8)
]);

Another way to build a sliding window over an arbitrary number of ranges is to use the function sliding_window.

// Example of a sliding window over two dimensions.
use rustml::sliding::*;

let windows = builder().add(100, 10, 25).add(10, 1, 2).to_vec();

let params = vec![
    param(100, 10, 25),
    param(10, 1, 2)
];

assert_eq!(
    sliding_window(&params),
    windows
);
// Example of a sliding window over one dimensions.
use rustml::sliding::*;

assert_eq!(
    builder().add(50, 20, 5).to_1d().unwrap(),
    vec![0, 5, 10, 15, 20, 25, 30]
);

Sliding window over a string.

use rustml::sliding::*;

let s = "hello, world!";
assert_eq!(
    // create a sliding window of size 5
    string_slider(s, 5).unwrap().collect::<Vec<&str>>(),
    vec![
        "hello", "ello,", "llo, ", "lo, w", 
        "o, wo", ", wor", " worl", "world", "orld!"
    ]
);

Structs

ByteSlider

Sliding window iterator of fixed size over bytes created with the function byte_slider.

DimensionParameters

Contains the parameters for one dimensions of a sliding window.

SlidingWindowBuilder

A builder to create a sliding window over an arbitrary number of dimensions.

StringSlider

Sliding window iterator of fixed size over a string.

Functions

builder

A function to comfortably create a sliding window over an arbitrary number of dimensions.

byte_slider

Creates a sliding window iterator of fixed size over bytes.

param

Creates a set of parameters for one dimension of a sliding window.

sliding_window

Function to create a sliding window over an arbitrary number of dimensions.

sliding_window_1d

Function to create a sliding window over one dimension.

sliding_window_2d

Funciton to create a sliding window over two dimensions.

string_slider

Creates a sliding window of fixed size over a string.