Sequence_

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.

Inherits

See also

Summary
Sequence_This class provides fast (multi-dimensional) Ruby arrays where all elements are of the same type.
Functions
Sequence_.stride=Used internally to override default array stride for non-contiguous data.
Sequence_.strideUsed internally to get array stride.
Sequence_.defaultUsed internally.
inspectCreate textual representation of this array showing part of the content.
to_sGet raw data of array.
save_rawWrite meta-information and raw data to file.
to_aConvert to standard Ruby array.
to_aryConvert to standard Ruby array.
saveSave image using RMagick.
savefSave image using OpenEXR.
save_ubyteSave as 8-bit greyscale image.
save_usintSave as 16-bit greyscale image.
save_ubytergbSave as colour image.
save_sfloatSave as single-precision floating-point greyscale image.
save_sfloatrgbSave as floating-point colour image.
to_objectShortcut for to_type( OBJECT ).
to_boolShortcut for to_type( BOOL ).
to_ubyteShortcut for to_type( UBYTE ).
to_byteShortcut for to_type( BYTE ).
to_usintShortcut for to_type( USINT ).
to_sintShortcut for to_type( SINT ).
to_uintShortcut for to_type( UINT ).
to_intShortcut for to_type( INT ).
to_ulongShortcut for to_type( ULONG ).
to_longShortcut for to_type( LONG ).
to_dfloatShortcut for to_type( DFLOAT ).
to_sfloatShortcut for to_type( SFLOAT ).
to_dcomplexShortcut for to_type( DCOMPLEX ).
to_scomplexShortcut for to_type( SCOMPLEX ).
to_ubytergbShortcut for to_type( UBYTERGB ).
to_bytergbShortcut for to_type( BYTERGB ).
to_usintrgbShortcut for to_type( USINTRGB ).
to_sintrgbShortcut for to_type( SINTRGB ).
to_uintrgbShortcut for to_type( UINTRGB ).
to_intrgbShortcut for to_type( INTRGB ).
to_ulongrgbShortcut for to_type( ULONGRGB ).
to_longrgbShortcut for to_type( LONGRGB ).
to_dfloatrgbShortcut for to_type( DFLOATRGB ).
to_sfloatrgbShortcut for to_type( SFLOATRGB ).
to_yv12Convert two-dimensional array to YV12 colourspace.
to_i420Convert two-dimensional array to I420 colourspace.
dupCreate duplicate of array.
coercePerform coercion with another object.
maskCreate an array containing only the selected elements.
unmaskDistribute elements using a mask.
componentsN-dimensional connected component labeling.
warpCreate warped array using an array of warp vectors (indices).
warp_maskCreate warped array using masked array of warp vectors (indices).
warp_clippedCreate warped array using an array of warp vectors (indices).
downsampleDownsampling of arrays.
upsampleUpsampling of arrays.
shiftCycle array along each dimension by the given number of elements.
transposeReorder the indices of the array.
rollTranspose array buy shifting indices to the left n-times.
rollTranspose array buy shifting indices to the right n-times.
zipConverts the elements of self and each argument to arrays and concatenates them.
repmatCreate repetition of array.
between?Check whether each element is within the given boundaries.
normaliseNormalise values of elements.
clipClip values of array using a range which specifies a lower and an upper threshold.
minGet minimum element of all elements.
maxGet maximum element of all elements.
rangeGet range of elements.
sumCompute sum of elements.
prodCompute product of elements.
displayDisplay all values.
sobelCompute Sobel-gradient.
prewittCompute Prewitt-gradient.
gauss_blurApply Gaussian blur to the array.
gauss_gradientCompute Gauss gradient.
morphologyApply generic morphology filter to image.
erodeMorphological operator reducing the masked area of a binary image.
dilateMorphological operator increasing the masked area of a binary image.
fftWrapper to use the (inverse) Discrete Fourier Transform as implemented in the FFTW-library.
ifftWrapper to use the inverse of the Discrete Fourier Transform as implemented in the FFTW-library.
rfftWrapper to use the (inverse) Discrete Fourier Transform for real data as implemented in the FFTW-library.
irfftWrapper to use the inverse of the Discrete Fourier Transform for symmetric data as implemented in the FFTW-library.
to_magickConvert to Magick::Image.
to_narrayConvert to NArray.
to_cvmatConvert to OpenCV matrix.
showOpens a window and displays the image.
Related
Functions
SequenceCreate an array class.
MultiArrayCreate a multi-dimensional array class.

Functions

Sequence_.stride=

def Sequence_.stride=(value)

Used internally to override default array stride for non-contiguous data.

Sequence_.stride

def Sequence_.stride

Used internally to get array stride.

Sequence_.default

def Sequence_.default

Used internally.  Returns default instance value.

inspect

def inspect

Create textual representation of this array showing part of the content.

to_s

def to_s

Get raw data of array.

save_raw

def save_raw(file)

Write meta-information and raw data to file.

See also

  • <MultiArray.load_raw>

to_a

def to_a

Convert to standard Ruby array.

to_ary

def to_ary

Convert to standard Ruby array.

save

def save(file)

Save image using RMagick.  Note that saving is not supported for every element-type.  The array is assumed to be two-dimensional.  It is recommended to use one of the save_* methods instead.

Parameters

fileA filename or an open file.

See also

savef

def savef(file)

Save image using OpenEXR.  The array is assumed to be two-dimensional.  It is recommended to use one of the save_* methods instead.

Parameters

fileA filename or an open file.

See also

save_ubyte

def save_ubyte(file)

Save as 8-bit greyscale image.  The array is assumed to be two-dimensional.

Parameters

fileA filename or an open file.

save_usint

def save_usint(file)

Save as 16-bit greyscale image.  The array is assumed to be two-dimensional.

Parameters

fileA filename or an open file.

save_ubytergb

def save_ubytergb(file)

Save as colour image.  The array is assumed to be two-dimensional.

Parameters

fileA filename or an open file.

save_sfloat

def save_sfloat(file)

Save as single-precision floating-point greyscale image.  The array is assumed to be two-dimensional.

Parameters

fileA filename or an open file.

save_sfloatrgb

def save_sfloatrgb(file)

Save as floating-point colour image.  The array is assumed to be two-dimensional.

Parameters

fileA filename or an open file.

to_object

def to_object

Shortcut for to_type( OBJECT ).

See also

to_bool

def to_bool

Shortcut for to_type( BOOL ).

See also

to_ubyte

def to_ubyte

Shortcut for to_type( UBYTE ).

See also

to_byte

def to_byte

Shortcut for to_type( BYTE ).

See also

to_usint

def to_usint

Shortcut for to_type( USINT ).

See also

to_sint

def to_sint

Shortcut for to_type( SINT ).

See also

to_uint

def to_uint

Shortcut for to_type( UINT ).

See also

to_int

def to_int

Shortcut for to_type( INT ).

See also

to_ulong

def to_ulong

Shortcut for to_type( ULONG ).

See also

to_long

def to_long

Shortcut for to_type( LONG ).

See also

to_dfloat

def to_dfloat

Shortcut for to_type( DFLOAT ).

See also

to_sfloat

def to_sfloat

Shortcut for to_type( SFLOAT ).

See also

to_dcomplex

def to_dcomplex

Shortcut for to_type( DCOMPLEX ).

See also

to_scomplex

def to_scomplex

Shortcut for to_type( SCOMPLEX ).

See also

to_ubytergb

def to_ubytergb

Shortcut for to_type( UBYTERGB ).

See also

to_bytergb

def to_bytergb

Shortcut for to_type( BYTERGB ).

See also

to_usintrgb

def to_usintrgb

Shortcut for to_type( USINTRGB ).

See also

to_sintrgb

def to_sintrgb

Shortcut for to_type( SINTRGB ).

See also

to_uintrgb

def to_uintrgb

Shortcut for to_type( UINTRGB ).

See also

to_intrgb

def to_intrgb

Shortcut for to_type( INTRGB ).

See also

to_ulongrgb

def to_ulongrgb

Shortcut for to_type( ULONGRGB ).

See also

to_longrgb

def to_longrgb

Shortcut for to_type( LONGRGB ).

See also

to_dfloatrgb

def to_dfloatrgb

Shortcut for to_type( DFLOATRGB ).

See also

to_sfloatrgb

def to_sfloatrgb

Shortcut for to_type( SFLOATRGB ).

See also

to_yv12

def to_yv12

Convert two-dimensional array to YV12 colourspace.

See also

to_i420

def to_i420

Convert two-dimensional array to I420 colourspace.

See also

dup

def dup

Create duplicate of array.

coerce

def coerce(other)

Perform coercion with another object.

mask

def mask(m)

Create an array containing only the selected elements.  The elements themselves can be arrays as well.

Parameters

mAn array with elements of type BOOL representing a mask.

Returns

Array with selected elements.

See also

unmask

def unmask(m,  
default =  typecode.default,
options =  { :safe => true })

Distribute elements using a mask.  This is the inverse operation of mask.

Parameters

mAn array with elements of type BOOL representing a mask.
defaultDefault value for the region outside the mask.
safePerform range checks to avoid segmentation fault.

Returns

Array with the same shape as the mask.

See also

components

def components(default =  typecode.default,
target =  UINT)

N-dimensional connected component labeling.  Two pixel get the same label if they have the same value and are next to each other (full connectivity).  Every pixel with the default value is labeled with 0.

See also

warp

def warp(field,  
options =  { :safe => true })

Create warped array using an array of warp vectors (indices).  Here is an example

Parameters

fieldArray containing warp vectors.
safeDo range checks to avoid segmentation fault

Returns

Warped sequence with the same shape as the warp vector field.

See also

  • <map>

warp_mask

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

Parameters

fieldArray object containing warp vectors.
mMask for selecting warp vectors.
defaultDefault value for undefined pixel.
safeDo range checks to avoid segmentation fault

Returns

Warped array with the same shape as the warp vector field.

warp_clipped

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.

Parameters

fieldArray containing warp indices.
defaultDefault value for undefined pixel.

Returns

Warped array.

See also

downsample

def downsample(intervals,
offsets)

Downsampling of arrays.

Parameters

intervalsarray specifying sampling interval for each dimension
offsetsarray specifying sampling offsets for each dimension

upsample

def upsample(intervals,  
offsets,  
shape =  nil)

Upsampling of arrays.  The shape of the return value can be specified.

Parameters

intervalsarray specifying sampling interval for each dimension
offsetsarray specifying sampling offsets for each dimension
shapearray specifying the shape of the return value.

shift

def shift(*offset)

Cycle array along each dimension by the given number of elements.

transpose

def transpose(*order)

Reorder the indices of the array.

Parameters

ordernew order of indices

See also

roll

def roll(n =  1)

Transpose array buy shifting indices to the left n-times.

Parameters

nnumber of times to roll (optional)

See also

roll

def unroll(n =  1)

Transpose array buy shifting indices to the right n-times.

Parameters

nnumber of times to unroll (optional)

See also

zip

def zip(*arrs)

Converts the elements of self and each argument to arrays and concatenates them.  Similar to Array#zip, which is part of the Ruby core.

See also

repmat

def repmat(*repshape)

Create repetition of array.  Create an array of arrays and fill with the values of this array.

Parameters

repshapeadditional dimensions

between?

def between?(a,
b)

Check whether each element is within the given boundaries.

Parameters

alower value which can be a scalar or a array
bupper value which can be a scalar or a array

normalise

def normalise(range =  0.0..255.0)

Normalise values of elements.  Normalise the values.  This is only defined for arrays with scalar values or RGB triplets.

clip

def clip(range =  0..255)

Clip values of array using a range which specifies a lower and an upper threshold.  This function only works with scalar values.

min

def min

Get minimum element of all elements.

Returns

Smallest element in this array.

max

def max

Get maximum element of all elements.

Returns

Largest element in this array.

range

def range

Get range of elements.  Same as

min..max

Returns

Range of elements

sum

def sum

Compute sum of elements.

prod

def prod

Compute product of elements.

display

def display(port =  $>)

Display all values.

sobel

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 |
+----+----+----+

Parameters

directionOrientation of gradient-operator

See also

prewitt

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 |
+----+----+----+

Parameters

directionOrientation of gradient-operator

gauss_blur

def gauss_blur(sigma,  
maxError =  1.0 / 256.0)

Apply Gaussian blur to the array.

See also

  • <Gauss blur>

gauss_gradient

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.

Parameter

sigmaextend of filter
directionNumber of dimension in which the gradient resides
max_errormaximum relative error caused by limiting the filter size

See also

morphology

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 |
+-----+-----+-----+

Parameters

wwidth of filter (default is 3)
hheight of filter (default is w)

(see log.jpg)

See also

erode

def erode(size =  3)

Morphological operator reducing the masked area of a binary image.

See also

dilate

def dilate(size =  3)

Morphological operator increasing the masked area of a binary image.

See also

fft

def fft(forward =  true)

Wrapper to use the (inverse) Discrete Fourier Transform as implemented in the FFTW-library.

Parameters

forwardBoolean indicating whether to compute forward or backward transform.

See also

ifft

def ifft

Wrapper to use the inverse of the Discrete Fourier Transform as implemented in the FFTW-library.  The result returned by the FFTW-library is divided by the size of the array.

See also

rfft

def rfft(forward =  true)

Wrapper to use the (inverse) Discrete Fourier Transform for real data as implemented in the FFTW-library.  The first dimension of the array must be even.

Parameters

forwardBoolean indicating whether to compute forward or backward transform.

See also

irfft

def irfft

Wrapper to use the inverse of the Discrete Fourier Transform for symmetric data as implemented in the FFTW-library.  The result returned by the FFTW-library is divided by the size of the resulting array.

See also

to_magick

def to_magick

Convert to Magick::Image.  This conversion allows you to apply methods implemented in the RMagick-extension.

The array is assumed to be two-dimensional.

See also

to_narray

def to_narray

Convert to NArray.  This conversion allows you to apply methods implemented in the NArray-extension.

See also

to_cvmat

def to_cvmat

Convert to OpenCV matrix.  The array must have two or three dimensions.  The array may contain complex values or RGB triplets if it has two dimensions.

See also

show

def show(width =  nil,
height =  nil)

Opens a window and displays the image.  Only works for 2D arrays.

Parameters

widthoptional parameter specifying the width of the window interior
heightoptional parameter specifying the width of the window interior

See also

Related

Summary
Functions
SequenceCreate an array class.
MultiArrayCreate a multi-dimensional array class.

Functions

Sequence

def Sequence(element_type,  
num_elements,  
stride =  element_type.size)

Create an array class.

Parameters

element_typeType of array elements.  Can be an array type as well.
num_elementsNumber of array elements.
strideOverride array stride for non-contiguous storage (optional).

MultiArray

def MultiArray(element_type,
*shape)

Create a multi-dimensional array class.

Parameters

element_typeType of array elements.  Can be an array type as well.
shapeDimensions of multi-dimensional array.
def Sequence_.stride=(value)
Used internally to override default array stride for non-contiguous data.
def Sequence_.stride
Used internally to get array stride.
def Sequence_.default
Used internally.
def inspect
Create textual representation of this array showing part of the content.
def to_s
Get raw data of array.
def save_raw(file)
Write meta-information and raw data to file.
def to_a
Convert to standard Ruby array.
def to_ary
Convert to standard Ruby array.
def save(file)
Save image using RMagick.
def savef(file)
Save image using OpenEXR.
def save_ubyte(file)
Save as 8-bit greyscale image.
def save_usint(file)
Save as 16-bit greyscale image.
def save_ubytergb(file)
Save as colour image.
def save_sfloat(file)
Save as single-precision floating-point greyscale image.
def save_sfloatrgb(file)
Save as floating-point colour image.
def to_object
Shortcut for to_type( OBJECT ).
def to_bool
Shortcut for to_type( BOOL ).
def to_ubyte
Shortcut for to_type( UBYTE ).
def to_byte
Shortcut for to_type( BYTE ).
def to_usint
Shortcut for to_type( USINT ).
def to_sint
Shortcut for to_type( SINT ).
def to_uint
Shortcut for to_type( UINT ).
def to_int
Shortcut for to_type( INT ).
def to_ulong
Shortcut for to_type( ULONG ).
def to_long
Shortcut for to_type( LONG ).
def to_dfloat
Shortcut for to_type( DFLOAT ).
def to_sfloat
Shortcut for to_type( SFLOAT ).
def to_dcomplex
Shortcut for to_type( DCOMPLEX ).
def to_scomplex
Shortcut for to_type( SCOMPLEX ).
def to_ubytergb
Shortcut for to_type( UBYTERGB ).
def to_bytergb
Shortcut for to_type( BYTERGB ).
def to_usintrgb
Shortcut for to_type( USINTRGB ).
def to_sintrgb
Shortcut for to_type( SINTRGB ).
def to_uintrgb
Shortcut for to_type( UINTRGB ).
def to_intrgb
Shortcut for to_type( INTRGB ).
def to_ulongrgb
Shortcut for to_type( ULONGRGB ).
def to_longrgb
Shortcut for to_type( LONGRGB ).
def to_dfloatrgb
Shortcut for to_type( DFLOATRGB ).
def to_sfloatrgb
Shortcut for to_type( SFLOATRGB ).
def to_yv12
Convert two-dimensional array to YV12 colourspace.
def to_i420
Convert two-dimensional array to I420 colourspace.
def dup
Create duplicate of array.
def coerce(other)
Perform coercion with another object.
def mask(m)
Create an array containing only the selected elements.
def unmask(m,  
default =  typecode.default,
options =  { :safe => true })
Distribute elements using a mask.
def components(default =  typecode.default,
target =  UINT)
N-dimensional connected component labeling.
def warp(field,  
options =  { :safe => true })
Create warped array using an array of warp vectors (indices).
def warp_mask(field,  
m,  
default =  typecode.default,
options =  { :safe => true })
Create warped array using masked array of warp vectors (indices).
def warp_clipped(field,  
default =  typecode.default)
Create warped array using an array of warp vectors (indices).
def downsample(intervals,
offsets)
Downsampling of arrays.
def upsample(intervals,  
offsets,  
shape =  nil)
Upsampling of arrays.
def shift(*offset)
Cycle array along each dimension by the given number of elements.
def transpose(*order)
Reorder the indices of the array.
def roll(n =  1)
Transpose array buy shifting indices to the left n-times.
def zip(*arrs)
Converts the elements of self and each argument to arrays and concatenates them.
def repmat(*repshape)
Create repetition of array.
def between?(a,
b)
Check whether each element is within the given boundaries.
def normalise(range =  0.0..255.0)
Normalise values of elements.
def clip(range =  0..255)
Clip values of array using a range which specifies a lower and an upper threshold.
def min
Get minimum element of all elements.
def max
Get maximum element of all elements.
def range
Get range of elements.
def sum
Compute sum of elements.
def prod
Compute product of elements.
def display(port =  $>)
Display all values.
def sobel(direction)
Compute Sobel-gradient.
def prewitt(direction)
Compute Prewitt-gradient.
def gauss_blur(sigma,  
maxError =  1.0 / 256.0)
Apply Gaussian blur to the array.
def gauss_gradient(sigma,  
direction,  
max_error =  1.0 / 256.0)
Compute Gauss gradient.
def morphology(w =  3,
h =  w)
Apply generic morphology filter to image.
def erode(size =  3)
Morphological operator reducing the masked area of a binary image.
def dilate(size =  3)
Morphological operator increasing the masked area of a binary image.
def fft(forward =  true)
Wrapper to use the (inverse) Discrete Fourier Transform as implemented in the FFTW-library.
def ifft
Wrapper to use the inverse of the Discrete Fourier Transform as implemented in the FFTW-library.
def rfft(forward =  true)
Wrapper to use the (inverse) Discrete Fourier Transform for real data as implemented in the FFTW-library.
def irfft
Wrapper to use the inverse of the Discrete Fourier Transform for symmetric data as implemented in the FFTW-library.
def to_magick
Convert to Magick::Image.
def to_narray
Convert to NArray.
def to_cvmat
Convert to OpenCV matrix.
def show(width =  nil,
height =  nil)
Opens a window and displays the image.
def Sequence(element_type,  
num_elements,  
stride =  element_type.size)
Create an array class.
def MultiArray(element_type,
*shape)
Create a multi-dimensional array class.
Abstract base class to represent native composite numbers and arrays.
def to_type(type)
Element-wise conversion to a different type.
The example program performs two-dimensional object recognition with three degrees of freedom.
This is an example on how to compute the Sobel x-gradient.
This is an example on how to compute the Sobel y-gradient.
This is an example on how to compute the Gaussian x-gradient.
This is an example on how to compute the Gaussian y-gradient.
The LoG-filter creates an image with zero-crossings at edge locations.
The class Magick::Image is extended with a few methods.
The class NArray is extended with methods for conversion.
The class OpenCV::CvMat is extended with a method.
Class for opening and closing an X11-display.
Class for opening and closing a Microsoft Windows display.
Close