Module: util

skimage.util.img_as_bool(image[, force_copy]) Convert an image to boolean format.
skimage.util.img_as_float(image[, force_copy]) Convert an image to double-precision floating point format.
skimage.util.img_as_int(image[, force_copy]) Convert an image to 16-bit signed integer format.
skimage.util.img_as_ubyte(image[, force_copy]) Convert an image to 8-bit unsigned integer format.
skimage.util.img_as_uint(image[, force_copy]) Convert an image to 16-bit unsigned integer format.
skimage.util.view_as_blocks(arr_in, block_shape) Block view of the input n-dimensional array (using re-striding).
skimage.util.view_as_windows(arr_in, ...) Rolling window view of the input n-dimensional array.

img_as_bool

skimage.util.img_as_bool(image, force_copy=False)

Convert an image to boolean format.

Parameters :

image : ndarray

Input image.

force_copy : bool

Force a copy of the data, irrespective of its current dtype.

Returns :

out : ndarray of bool (bool_)

Output image.

Notes

The upper half of the input dtype’s positive range is True, and the lower half is False. All negative values (if present) are False.

img_as_float

skimage.util.img_as_float(image, force_copy=False)

Convert an image to double-precision floating point format.

Parameters :

image : ndarray

Input image.

force_copy : bool

Force a copy of the data, irrespective of its current dtype.

Returns :

out : ndarray of float64

Output image.

Notes

The range of a floating point image is [0.0, 1.0] or [-1.0, 1.0] when converting from unsigned or signed datatypes, respectively.

img_as_int

skimage.util.img_as_int(image, force_copy=False)

Convert an image to 16-bit signed integer format.

Parameters :

image : ndarray

Input image.

force_copy : bool

Force a copy of the data, irrespective of its current dtype.

Returns :

out : ndarray of uint16

Output image.

Notes

If the input data-type is positive-only (e.g., uint8), then the output image will still only have positive values.

img_as_ubyte

skimage.util.img_as_ubyte(image, force_copy=False)

Convert an image to 8-bit unsigned integer format.

Parameters :

image : ndarray

Input image.

force_copy : bool

Force a copy of the data, irrespective of its current dtype.

Returns :

out : ndarray of ubyte (uint8)

Output image.

Notes

If the input data-type is positive-only (e.g., uint16), then the output image will still only have positive values.

img_as_uint

skimage.util.img_as_uint(image, force_copy=False)

Convert an image to 16-bit unsigned integer format.

Parameters :

image : ndarray

Input image.

force_copy : bool

Force a copy of the data, irrespective of its current dtype.

Returns :

out : ndarray of uint16

Output image.

Notes

Negative input values will be shifted to the positive domain.

view_as_blocks

skimage.util.view_as_blocks(arr_in, block_shape)

Block view of the input n-dimensional array (using re-striding).

Blocks are non-overlapping views of the input array.

Parameters :

arr_in: ndarray :

The n-dimensional input array.

block_shape: tuple :

The shape of the block. Each dimension must divide evenly into the corresponding dimensions of arr_in.

Returns :

arr_out: ndarray :

Block view of the input array.

Examples

>>> import numpy as np
>>> from skimage.util.shape import view_as_blocks
>>> A = np.arange(4*4).reshape(4,4)
>>> A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> B = view_as_blocks(A, block_shape=(2, 2))
>>> B[0, 0]
array([[0, 1],
       [4, 5]])
>>> B[0, 1]
array([[2, 3],
       [6, 7]])
>>> B[1, 0, 1, 1]
13
>>> A = np.arange(4*4*6).reshape(4,4,6)
>>> A  
array([[[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17],
        [18, 19, 20, 21, 22, 23]],
       [[24, 25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34, 35],
        [36, 37, 38, 39, 40, 41],
        [42, 43, 44, 45, 46, 47]],
       [[48, 49, 50, 51, 52, 53],
        [54, 55, 56, 57, 58, 59],
        [60, 61, 62, 63, 64, 65],
        [66, 67, 68, 69, 70, 71]],
       [[72, 73, 74, 75, 76, 77],
        [78, 79, 80, 81, 82, 83],
        [84, 85, 86, 87, 88, 89],
        [90, 91, 92, 93, 94, 95]]])
>>> B = view_as_blocks(A, block_shape=(1, 2, 2))
>>> B.shape
(4, 2, 3, 1, 2, 2)
>>> B[2:, 0, 2]  
array([[[[52, 53],
         [58, 59]]],
       [[[76, 77],
         [82, 83]]]])

view_as_windows

skimage.util.view_as_windows(arr_in, window_shape)

Rolling window view of the input n-dimensional array.

Windows are overlapping views of the input array, with adjacent windows shifted by a single row or column (or an index of a higher dimension).

Parameters :

arr_in: ndarray :

The n-dimensional input array.

window_shape: tuple :

Defines the shape of the elementary n-dimensional orthotope (better know as hyperrectangle [R134]) of the rolling window view.

Returns :

arr_out: ndarray :

(rolling) window view of the input array.

Notes

One should be very careful with rolling views when it comes to memory usage. Indeed, although a ‘view’ has the same memory footprint as its base array, the actual array that emerges when this ‘view’ is used in a computation is generally a (much) larger array than the original, especially for 2-dimensional arrays and above.

For example, let us consider a 3 dimensional array of size (100, 100, 100) of float64. This array takes about 8*100**3 Bytes for storage which is just 8 MB. If one decides to build a rolling view on this array with a window of (3, 3, 3) the hypothetical size of the rolling view (if one was to reshape the view for example) would be 8*(100-3+1)**3*3**3 which is about 203 MB! The scaling becomes even worse as the dimension of the input array becomes larger.

References

[R134](1, 2) http://en.wikipedia.org/wiki/Hyperrectangle

Examples

>>> import numpy as np
>>> from skimage.util.shape import view_as_windows
>>> A = np.arange(4*4).reshape(4,4)
>>> A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> window_shape = (2, 2)
>>> B = view_as_windows(A, window_shape)
>>> B[0, 0]
array([[0, 1],
       [4, 5]])
>>> B[0, 1]
array([[1, 2],
       [5, 6]])
>>> A = np.arange(10)
>>> A
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> window_shape = (3,)
>>> B = view_as_windows(A, window_shape)
>>> B.shape
(8, 3)
>>> B
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6],
       [5, 6, 7],
       [6, 7, 8],
       [7, 8, 9]])
>>> A = np.arange(5*4).reshape(5, 4)
>>> A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])
>>> window_shape = (4, 3)
>>> B = view_as_windows(A, window_shape)
>>> B.shape
(2, 2, 4, 3)
>>> B  
array([[[[ 0,  1,  2],
         [ 4,  5,  6],
         [ 8,  9, 10],
         [12, 13, 14]],
        [[ 1,  2,  3],
         [ 5,  6,  7],
         [ 9, 10, 11],
         [13, 14, 15]]],
       [[[ 4,  5,  6],
         [ 8,  9, 10],
         [12, 13, 14],
         [16, 17, 18]],
        [[ 5,  6,  7],
         [ 9, 10, 11],
         [13, 14, 15],
         [17, 18, 19]]]])