superdsm.image

class superdsm.image.Image(model=None, mask=None, full_mask=None, offset=(0, 0))

Bases: object

This class facilitates the work with images, image masks, and image regions.

static create_from_array(img, mask=None, normalize=True)

Creates an instance from an image and a mask.

get_map(normalized=True, pad=0)

Returns two 2D arrays corresponding to the pixel coordinates.

See get_pixel_map() for details.

get_region(mask, shrink=False)

Returns the image region specified by a mask.

Parameters:
  • mask – The binary mask used to specify the image region.

  • shrink – If True, the image region will be reduced.

Returns:

The image region specified by the mask.

shrink_mask(mask)

Reduces a mask so it can be used to access this image.

superdsm.image.bbox(mask, include_end=False)

Returns the bounding box of a mask.

Parameters:

include_end – If True, then the pair of last indices bbox[0][1] and bbox[1][1] is included in the specified ranges. It is excluded otherwise.

Returns:

Tuple (bbox, sel), where bbox[0] are the first and last indices of the rows and bbox[1] are the first and last indices of the columns, and sel is a numpy slice corresponding to that image region.

>>> import superdsm.image
>>> import numpy as np
>>> mask = np.array([[0, 0, 0, 0, 0],
...                  [0, 0, 0, 1, 0],
...                  [0, 0, 1, 1, 0],
...                  [0, 0, 1, 0, 0]])
>>> superdsm.image.bbox(mask.astype(bool))
(array([[1, 4],
       [2, 4]]), (slice(1, 4, None), slice(2, 4, None)))
>>> superdsm.image.bbox(mask.astype(bool), include_end=True)
(array([[1, 3],
       [2, 3]]), (slice(1, 3, None), slice(2, 3, None)))
superdsm.image.get_pixel_map(shape, normalized=False)

Returns two 2D arrays corresponding to pixel coordinates of an array of the given shape.

The first array corresponds to the row indices (coordinates), the second array corresponds to the column indices (coordinates). The coordinates are normalized to the range between 0 and 1 if normalized is True.

Returns:

The two 2D arrays, encapsulated as a single 3D array.

>>> import superdsm.image
>>> superdsm.image.get_pixel_map((6, 3))
array([[[0., 0., 0.],
        [1., 1., 1.],
        [2., 2., 2.],
        [3., 3., 3.],
        [4., 4., 4.],
        [5., 5., 5.]],

       [[0., 1., 2.],
        [0., 1., 2.],
        [0., 1., 2.],
        [0., 1., 2.],
        [0., 1., 2.],
        [0., 1., 2.]]])
>>> superdsm.image.get_pixel_map((6, 3), normalized=True)
array([[[0. , 0. , 0. ],
        [0.2, 0.2, 0.2],
        [0.4, 0.4, 0.4],
        [0.6, 0.6, 0.6],
        [0.8, 0.8, 0.8],
        [1. , 1. , 1. ]],

       [[0. , 0.5, 1. ],
        [0. , 0.5, 1. ],
        [0. , 0.5, 1. ],
        [0. , 0.5, 1. ],
        [0. , 0.5, 1. ],
        [0. , 0.5, 1. ]]])
superdsm.image.normalize_image(img)

Normalizes the image intensities to the range from 0 to 1.

The original image img is not modified.

Returns:

The normalized image.