Morphological Filters

class BinaryClosing(*args, **kwds)[source]

Close a binary mask.

Closing is a dilation followed by an erosion. Closing can be used to remove small holes.

Parameters
  • np_mask (np.ndarray (arbitrary shape, int or bool type)) – Numpy array of the binary mask

  • disk_size (int, optional (default is 3)) – Radius of the disk structuring element used for closing.

  • iterations (int, optional (default is 1)) – How many times to repeat the closing.

Returns

Mask after the closing

Return type

np.ndarray

Example

>>> from PIL import Image
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import BinaryClosing
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> binary_closing = BinaryClosing()
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_closed = binary_closing(binary_image)
class BinaryDilation(*args, **kwds)[source]

Dilate a binary mask.

Parameters
  • np_mask (np.ndarray (arbitrary shape, int or bool type)) – Numpy array of the binary mask

  • disk_size (int, optional (default is 5)) – Radius of the disk structuring element used for dilation.

  • iterations (int, optional (default is 1)) – How many times to repeat the dilation.

Returns

Mask after the dilation

Return type

np.ndarray

Example

>>> from PIL import Image
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import BinaryDilation
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> binary_dilation = BinaryDilation()
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_dilated = binary_dilation(binary_image)
class BinaryErosion(*args, **kwds)[source]

Erode a binary mask.

Parameters
  • np_mask (np.ndarray (arbitrary shape, int or bool type)) – Numpy array of the binary mask

  • disk_size (int, optional (default is 5)) – Radius of the disk structuring element used for erosion.

  • iterations (int, optional (default is 1)) – How many times to repeat the erosion.

Returns

Mask after the erosion

Return type

np.ndarray

Example

>>> from PIL import Image
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import BinaryErosion
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> binary_erosion = BinaryErosion(disk_size=6)
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_eroded = binary_erosion(binary_image)
class BinaryFillHoles(*args, **kwds)[source]

Fill the holes in binary objects.

Parameters
  • np_img (np.ndarray (arbitrary shape, int or bool type)) – Numpy array of the binary mask

  • structure (np.ndarray, optional) – Structuring element used in the computation; The default element yields the intuitive result where all holes in the input have been filled.

Returns

Transformation of the initial image input where holes have been filled.

Return type

np.ndarray

Example

>>> from PIL import Image
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import BinaryFillHoles
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> binary_fill_holes = BinaryFillHoles()
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_filled_holes = binary_fill_holes(binary_image)
class BinaryOpening(*args, **kwds)[source]

Open a binary mask.

Opening is an erosion followed by a dilation. Opening can be used to remove small objects.

Parameters
  • np_mask (np.ndarray (arbitrary shape, int or bool type)) – Numpy array of the binary mask

  • disk_size (int, optional (default is 3)) – Radius of the disk structuring element used for opening.

  • iterations (int, optional (default is 1)) – How many times to repeat the opening.

Returns

Mask after the opening

Return type

np.ndarray

Example

>>> from PIL import Image
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import BinaryOpening
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> binary_opening = BinaryOpening()
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_opened = binary_opening(binary_image)
class MorphologicalFilter(*args, **kwds)[source]

Morphological filter base class

class RemoveSmallHoles(*args, **kwds)[source]

Remove holes smaller than a specified size.

Parameters
  • np_img (np.ndarray (arbitrary shape, int or bool type)) – Input mask

  • area_threshold (int, optional (default is 3000)) – Remove small holes below this size.

Returns

Mask with small holes filtered out

Return type

np.ndarray

Example

>>> from PIL import Image
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import RemoveSmallHoles
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> remove_small_holes = RemoveSmallHoles()
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_no_small_holes = remove_small_holes(binary_image)
class RemoveSmallObjects(*args, **kwds)[source]

Remove objects smaller than the specified size.

If avoid_overmask is True, this function can recursively call itself with progressively halved minimum size objects to avoid removing too many objects in the mask.

Parameters
  • np_img (np.ndarray (arbitrary shape, int or bool type)) – Input mask

  • min_size (int, optional) – Minimum size of small object to remove. Default is 3000

  • avoid_overmask (bool, optional (default is True)) – If True, avoid masking above the overmask_thresh percentage.

  • overmask_thresh (int, optional (default is 95)) – If avoid_overmask is True, avoid masking above this threshold percentage value.

Returns

Mask with small objects filtered out

Return type

np.ndarray

Example

>>> from PIL import Image
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import RemoveSmallObjects
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> remove_small_objects = RemoveSmallObjects()
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_no_small_objects = remove_small_objects(binary_image)
class WatershedSegmentation(*args, **kwds)[source]

Segment and label an binary mask with Watershed segmentation 1

The watershed algorithm treats pixels values as a local topography (elevation).

Parameters
  • np_mask (np.ndarray) – Input mask

  • region_shape (int, optional) – The local region within which to search for image peaks is defined as a squared area region_shape x region_shape. Default is 6.

Returns

Labelled segmentation mask

Return type

np.ndarray

References

1

Watershed segmentation. https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_watershed.html

Example

>>> import numpy as np
>>> from histolab.filters.morphological_filters import WatershedSegmentation
>>> mask = np.array([[0,1],[1,0]]) # or np.load("/path/my_array_mask.npy")
>>> watershed_segmentation = WatershedSegmentation()
>>> mask_segmented = watershed_segmentation(mask)
class WhiteTopHat(*args, **kwds)[source]

Return white top hat of an image.

The white top hat of an image is defined as the image minus its morphological opening with respect to a structuring element. This operation returns the bright spots of the image that are smaller than the structuring element.

Parameters
  • np_mask (np.ndarray (arbitrary shape, int or bool type)) – Numpy array of the binary mask

  • structure (np.ndarray, optional) – The neighborhood expressed as an array of 1 and 0. If None, use cross-shaped structuring element (connectivity=1).

Example

>>> from PIL import Image
>>> import numpy as np
>>> from histolab.filters.image_filters import RgbToGrayscale, OtsuThreshold
>>> from histolab.filters.morphological_filters import WhiteTopHat
>>> image_rgb = Image.open("tests/fixtures/pil-images-rgb/tcga-lung-rgb.png")
>>> rgb_to_grayscale = RgbToGrayscale()
>>> otsu_threshold = OtsuThreshold()
>>> white_that = WhiteTopHat(np.ones((5,5)))
>>> image_gray = rgb_to_grayscale(image_rgb)
>>> binary_image = otsu_threshold(image_gray)
>>> image_out = white_that(binary_image)