This class provides fast (multi-dimensional) Ruby arrays where all elements are of the same type. It is also possible to define custom types and fast operations on them. Nested sequences are used to represent multi-dimensional arrays.
The class Sequence_ is designed to be mostly compatible with Masahiro Tanaka’s NArray. However it allows the definition of custom element types and fast operations on them. Sequence_ is also inspired by Ronald Garcia’s boost::multi_array and by Todd Veldhuizen’s Blitz++. Using Sequence_ you can do number crunching in Ruby!
Sequence_ is implemented as a parametric class. You need to use the constructor methods provided by the classes Sequence and MultiArray to instantiate objects.
| Sequence_ | This class provides fast (multi-dimensional) Ruby arrays where all elements are of the same type. |
| Functions | |
| Sequence_. | Used internally to override default array stride for non-contiguous data. |
| Sequence_. | Used internally to get array stride. |
| Sequence_. | Used internally. |
| inspect | Create textual representation of this array showing part of the content. |
| to_s | Get raw data of array. |
| save_raw | Write meta-information and raw data to file. |
| to_a | Convert to standard Ruby array. |
| to_ary | Convert to standard Ruby array. |
| save | Save image using RMagick. |
| savef | Save image using OpenEXR. |
| save_ubyte | Save as 8-bit greyscale image. |
| save_usint | Save as 16-bit greyscale image. |
| save_ubytergb | Save as colour image. |
| save_sfloat | Save as single-precision floating-point greyscale image. |
| save_sfloatrgb | Save as floating-point colour image. |
| to_object | Shortcut for to_type( OBJECT ). |
| to_bool | Shortcut for to_type( BOOL ). |
| to_ubyte | Shortcut for to_type( UBYTE ). |
| to_byte | Shortcut for to_type( BYTE ). |
| to_usint | Shortcut for to_type( USINT ). |
| to_sint | Shortcut for to_type( SINT ). |
| to_uint | Shortcut for to_type( UINT ). |
| to_int | Shortcut for to_type( INT ). |
| to_ulong | Shortcut for to_type( ULONG ). |
| to_long | Shortcut for to_type( LONG ). |
| to_dfloat | Shortcut for to_type( DFLOAT ). |
| to_sfloat | Shortcut for to_type( SFLOAT ). |
| to_dcomplex | Shortcut for to_type( DCOMPLEX ). |
| to_scomplex | Shortcut for to_type( SCOMPLEX ). |
| to_ubytergb | Shortcut for to_type( UBYTERGB ). |
| to_bytergb | Shortcut for to_type( BYTERGB ). |
| to_usintrgb | Shortcut for to_type( USINTRGB ). |
| to_sintrgb | Shortcut for to_type( SINTRGB ). |
| to_uintrgb | Shortcut for to_type( UINTRGB ). |
| to_intrgb | Shortcut for to_type( INTRGB ). |
| to_ulongrgb | Shortcut for to_type( ULONGRGB ). |
| to_longrgb | Shortcut for to_type( LONGRGB ). |
| to_dfloatrgb | Shortcut for to_type( DFLOATRGB ). |
| to_sfloatrgb | Shortcut for to_type( SFLOATRGB ). |
| to_yv12 | Convert two-dimensional array to YV12 colourspace. |
| to_i420 | Convert two-dimensional array to I420 colourspace. |
| dup | Create duplicate of array. |
| coerce | Perform coercion with another object. |
| mask | Create an array containing only the selected elements. |
| unmask | Distribute elements using a mask. |
| components | N-dimensional connected component labeling. |
| warp | Create warped array using an array of warp vectors (indices). |
| warp_mask | Create warped array using masked array of warp vectors (indices). |
| warp_clipped | Create warped array using an array of warp vectors (indices). |
| downsample | Downsampling of arrays. |
| upsample | Upsampling of arrays. |
| shift | Cycle array along each dimension by the given number of elements. |
| transpose | Reorder the indices of the array. |
| roll | Transpose array buy shifting indices to the left n-times. |
| roll | Transpose array buy shifting indices to the right n-times. |
| zip | Converts the elements of self and each argument to arrays and concatenates them. |
| repmat | Create repetition of array. |
| between? | Check whether each element is within the given boundaries. |
| normalise | Normalise values of elements. |
| clip | Clip values of array using a range which specifies a lower and an upper threshold. |
| min | Get minimum element of all elements. |
| max | Get maximum element of all elements. |
| range | Get range of elements. |
| sum | Compute sum of elements. |
| prod | Compute product of elements. |
| display | Display all values. |
| sobel | Compute Sobel-gradient. |
| prewitt | Compute Prewitt-gradient. |
| gauss_blur | Apply Gaussian blur to the array. |
| gauss_gradient | Compute Gauss gradient. |
| morphology | Apply generic morphology filter to image. |
| erode | Morphological operator reducing the masked area of a binary image. |
| dilate | Morphological operator increasing the masked area of a binary image. |
| fft | Wrapper to use the (inverse) Discrete Fourier Transform as implemented in the FFTW-library. |
| ifft | Wrapper to use the inverse of the Discrete Fourier Transform as implemented in the FFTW-library. |
| rfft | Wrapper to use the (inverse) Discrete Fourier Transform for real data as implemented in the FFTW-library. |
| irfft | Wrapper to use the inverse of the Discrete Fourier Transform for symmetric data as implemented in the FFTW-library. |
| to_magick | Convert to Magick::Image. |
| to_narray | Convert to NArray. |
| to_cvmat | Convert to OpenCV matrix. |
| show | Opens a window and displays the image. |
| Related | |
| Functions | |
| Sequence | Create an array class. |
| MultiArray | Create a multi-dimensional array class. |
def unmask( m, default = typecode.default, options = { :safe => true } )
Distribute elements using a mask. This is the inverse operation of mask.
| m | An array with elements of type BOOL representing a mask. |
| default | Default value for the region outside the mask. |
| safe | Perform range checks to avoid segmentation fault. |
Array with the same shape as the mask.
def warp( field, options = { :safe => true } )
Create warped array using an array of warp vectors (indices). Here is an example
| field | Array containing warp vectors. |
| safe | Do range checks to avoid segmentation fault |
Warped sequence with the same shape as the warp vector field.
def warp_mask( field, m, default = typecode.default, options = { :safe => true } )
Create warped array using masked array of warp vectors (indices). Here is an example
| field | Array object containing warp vectors. |
| m | Mask for selecting warp vectors. |
| default | Default value for undefined pixel. |
| safe | Do range checks to avoid segmentation fault |
Warped array with the same shape as the warp vector field.
def warp_clipped( field, default = typecode.default )
Create warped array using an array of warp vectors (indices). A clipping mask is computed from the warp indices. Undefined pixel are assigned the default value.
| field | Array containing warp indices. |
| default | Default value for undefined pixel. |
Warped array.
def upsample( intervals, offsets, shape = nil )
Upsampling of arrays. The shape of the return value can be specified.
| intervals | array specifying sampling interval for each dimension |
| offsets | array specifying sampling offsets for each dimension |
| shape | array specifying the shape of the return value. |
def transpose( * order )
Reorder the indices of the array.
| order | new order of indices |
def roll( n = 1 )
Transpose array buy shifting indices to the left n-times.
| n | number of times to roll (optional) |
def sobel( direction )
Compute Sobel-gradient.
A two-dimensional Sobel x-gradient for example means a correlation of the array with the following separable filter:
+----+----+----+ | -1 | 0 | 1 | +----+----+----+ | -2 | 0 | 2 | +----+----+----+ | -1 | 0 | 1 | +----+----+----+
| direction | Orientation of gradient-operator |

def prewitt( direction )
Compute Prewitt-gradient.
A two-dimensional Prewitt x-gradient for example means a correlation of the array with the following separable filter:
+----+----+----+ | -1 | 0 | 1 | +----+----+----+ | -1 | 0 | 1 | +----+----+----+ | -1 | 0 | 1 | +----+----+----+
| direction | Orientation of gradient-operator |
def gauss_gradient( sigma, direction, max_error = 1.0 / 256.0 )
Compute Gauss gradient.
Compute gradient of the array. A gradient filter is applied in the specified direction. The filter blurs in all other directions.

| sigma | extend of filter |
| direction | Number of dimension in which the gradient resides |
| max_error | maximum relative error caused by limiting the filter size |
def morphology( w = 3, h = w )
Apply generic morphology filter to image. The input image should be a binary image (values of 0 and 1 only). The result of this operation can be used to implement any morphological operation. Note that if the filter is too large the correlation has to be performed using Ruby’s big integers which is very slow.
+-----+-----+-----+ | 1 | 2 | 4 | +-----+-----+-----+ | 8 | 16 | 32 | +-----+-----+-----+ | 64 | 128 | 256 | +-----+-----+-----+
| w | width of filter (default is 3) |
| h | height of filter (default is w) |
(see log.jpg)
| Functions | |
| Sequence | Create an array class. |
| MultiArray | Create a multi-dimensional array class. |
Used internally to override default array stride for non-contiguous data.
def Sequence_.stride=( value )
Used internally to get array stride.
def Sequence_.stride
Used internally.
def Sequence_.default
Create textual representation of this array showing part of the content.
def inspect
Get raw data of array.
def to_s
Write meta-information and raw data to file.
def save_raw( file )
Convert to standard Ruby array.
def to_a
Convert to standard Ruby array.
def to_ary
Save image using RMagick.
def save( file )
Save image using OpenEXR.
def savef( file )
Save as 8-bit greyscale image.
def save_ubyte( file )
Save as 16-bit greyscale image.
def save_usint( file )
Save as colour image.
def save_ubytergb( file )
Save as single-precision floating-point greyscale image.
def save_sfloat( file )
Save as floating-point colour image.
def save_sfloatrgb( file )
Shortcut for to_type( OBJECT ).
def to_object
Shortcut for to_type( BOOL ).
def to_bool
Shortcut for to_type( UBYTE ).
def to_ubyte
Shortcut for to_type( BYTE ).
def to_byte
Shortcut for to_type( USINT ).
def to_usint
Shortcut for to_type( SINT ).
def to_sint
Shortcut for to_type( UINT ).
def to_uint
Shortcut for to_type( INT ).
def to_int
Shortcut for to_type( ULONG ).
def to_ulong
Shortcut for to_type( LONG ).
def to_long
Shortcut for to_type( DFLOAT ).
def to_dfloat
Shortcut for to_type( SFLOAT ).
def to_sfloat
Shortcut for to_type( DCOMPLEX ).
def to_dcomplex
Shortcut for to_type( SCOMPLEX ).
def to_scomplex
Shortcut for to_type( UBYTERGB ).
def to_ubytergb
Shortcut for to_type( BYTERGB ).
def to_bytergb
Shortcut for to_type( USINTRGB ).
def to_usintrgb
Shortcut for to_type( SINTRGB ).
def to_sintrgb
Shortcut for to_type( UINTRGB ).
def to_uintrgb
Shortcut for to_type( INTRGB ).
def to_intrgb
Shortcut for to_type( ULONGRGB ).
def to_ulongrgb
Shortcut for to_type( LONGRGB ).
def to_longrgb
Shortcut for to_type( DFLOATRGB ).
def to_dfloatrgb
Shortcut for to_type( SFLOATRGB ).
def to_sfloatrgb
Convert two-dimensional array to YV12 colourspace.
def to_yv12
Convert two-dimensional array to I420 colourspace.
def to_i420
Create duplicate of array.
def dup
Perform coercion with another object.
def coerce( other )
Create an array containing only the selected elements.
def mask( m )
Distribute elements using a mask.
def unmask( m, default = typecode.default, options = { :safe => true } )
N-dimensional connected component labeling.
def components( default = typecode.default, target = UINT )
Create warped array using an array of warp vectors (indices).
def warp( field, options = { :safe => true } )
Create warped array using masked array of warp vectors (indices).
def warp_mask( field, m, default = typecode.default, options = { :safe => true } )
Create warped array using an array of warp vectors (indices).
def warp_clipped( field, default = typecode.default )
Downsampling of arrays.
def downsample( intervals, offsets )
Upsampling of arrays.
def upsample( intervals, offsets, shape = nil )
Cycle array along each dimension by the given number of elements.
def shift( * offset )
Reorder the indices of the array.
def transpose( * order )
Transpose array buy shifting indices to the left n-times.
def roll( n = 1 )
Converts the elements of self and each argument to arrays and concatenates them.
def zip( * arrs )
Create repetition of array.
def repmat( * repshape )
Check whether each element is within the given boundaries.
def between?( a, b )
Normalise values of elements.
def normalise( range = 0.0..255.0 )
Clip values of array using a range which specifies a lower and an upper threshold.
def clip( range = 0..255 )
Get minimum element of all elements.
def min
Get maximum element of all elements.
def max
Get range of elements.
def range
Compute sum of elements.
def sum
Compute product of elements.
def prod
Display all values.
def display( port = $> )
Compute Sobel-gradient.
def sobel( direction )
Compute Prewitt-gradient.
def prewitt( direction )
Apply Gaussian blur to the array.
def gauss_blur( sigma, maxError = 1.0 / 256.0 )
Compute Gauss gradient.
def gauss_gradient( sigma, direction, max_error = 1.0 / 256.0 )
Apply generic morphology filter to image.
def morphology( w = 3, h = w )
Morphological operator reducing the masked area of a binary image.
def erode( size = 3 )
Morphological operator increasing the masked area of a binary image.
def dilate( size = 3 )
Wrapper to use the (inverse) Discrete Fourier Transform as implemented in the FFTW-library.
def fft( forward = true )
Wrapper to use the inverse of the Discrete Fourier Transform as implemented in the FFTW-library.
def ifft
Wrapper to use the (inverse) Discrete Fourier Transform for real data as implemented in the FFTW-library.
def rfft( forward = true )
Wrapper to use the inverse of the Discrete Fourier Transform for symmetric data as implemented in the FFTW-library.
def irfft
Convert to Magick::Image.
def to_magick
Convert to NArray.
def to_narray
Convert to OpenCV matrix.
def to_cvmat
Opens a window and displays the image.
def show( width = nil, height = nil )
Create an array class.
def Sequence( element_type, num_elements, stride = element_type.size )
Create a multi-dimensional array class.
def MultiArray( element_type, * shape )
Element-wise conversion to a different type.
def to_type( type )