quantimpy.morphology module

This module performs various morphological operations on 2D and 3D Numpy 1 arrays. These computations can handle both isotropic and anisotropic image resolutions.

Notes

To perform morphological operations this library uses the Euclidean distance transform 2. These transforms are computed using the MLAEDT-3D library.

quantimpy.morphology.close(dilation, dist, res=None)

Morphologically close a binary Numpy array

This function is an alias for the function erode(). Together with the dilate() function this function performs the morphological closing operation on the binary Numpy array dilation. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘dilation’ array.

Parameters
  • dilation (ndarray, bool) – Dilation can be either a 2D or 3D array of data type bool.

  • dist ({int, float}) – The distance away from the interface to which an array is closed in the same unit of length as used in the resolution.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns a morphologically closed Numpy array. The return data type is bool.

Return type

ndarray, bool

See also

erode, dilate, open

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphologcally closed image can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (square)
from quantimpy import morphology as mp

image = np.zeros([127,128],dtype=bool)
image[17:65,16:64] = square(48,dtype=bool)
image[65:113,64:112] = square(48,dtype=bool)

dilation = mp.dilate(image,10)
closing  = mp.close(dilation,10)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(closing[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (cube)
from quantimpy import morphology as mp

image = np.zeros([128,128,128],dtype=bool)
image[16:64,16:64,16:64] = cube(48,dtype=bool)
image[64:112,64:112,64:112] = cube(48,dtype=bool)

dilation = mp.dilate(image,10)
closing  = mp.close(dilation,10)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(closing[:,:,64])
plt.show()
quantimpy.morphology.close_map(dilation_map, res=None)

Compute a morphological closing map of a Numpy array

Together with the dilate_map() function this function computes a morphological closing map of the Numpy array dilation_map. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘dilation’ array.

Parameters
  • dilation_map (ndarray, float) – Dilation_map can be either a 2D or 3D array of data type float.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns a distance map of a morphologically closed Numpy array. The return data type is uint16.

Return type

ndarray, uint16

See also

dilate_map, open_map

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphological closing map can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (square)
from quantimpy import morphology as mp

image = np.zeros([128,128],dtype=bool)
image[16:64,16:64] = square(48,dtype=bool)
image[64:112,64:112] = square(48,dtype=bool)

dilationMap = mp.dilate_map(image)
closingMap = mp.close_map(dilationMap)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(closingMap[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (cube)
from quantimpy import morphology as mp

image = np.zeros([128,128,128],dtype=bool)
image[16:64,16:64,16:64] = cube(48,dtype=bool)
image[64:112,64:112,64:112] = cube(48,dtype=bool)

dilationMap = mp.dilate_map(image)
closingMap = mp.close_map(dilationMap)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(closingMap[:,:,64])
plt.show()
quantimpy.morphology.dilate(image, dist, res=None)

Morphologically dilate a binary Numpy array

This function performs the morphological dilation operation on the binary Numpy array image. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘image’ array.

Parameters
  • image (ndarray, bool) – Image can be either a 2D or 3D array of data type bool.

  • dist ({int, float}) – The distance away from the interface to which an array is dilated in the same unit of length as used in the resolution.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns a morphologically dilated Numpy array. The return data type is bool.

Return type

ndarray, bool

See also

erode, dilate_map

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphologcally dilated image can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (square)
from quantimpy import morphology as mp

image = np.zeros([128,128],dtype=bool)
image[16:112,16:112] = square(96,dtype=bool)

dilation = mp.dilate(image,10)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(dilation[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (cube)
from quantimpy import morphology as mp

image = np.zeros([128,128,128],dtype=bool)
image[16:112,16:112,16:112] = cube(96,dtype=bool)

dilation = mp.dilate(image,10)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(dilation[:,:,64])
plt.show()
quantimpy.morphology.dilate_map(image, res=None)

Compute a morphological dilation map of a Numpy array

This function computes a morphological dilation map of the binary Numpy array image. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘dilation’ array.

Parameters
  • image (ndarray, bool) – Image can be either a 2D or 3D array of data type bool.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns a dilation map of a binary Numpy array. The return data type is uint16.

Return type

ndarray, uint16

See also

erode_map, close_map

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphological closing map can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (disk)
from quantimpy import morphology as mp

image = np.zeros([127,128],dtype=bool)
image[16:113,16:113] = disk(48,dtype=bool)

dilationMap = mp.dilate_map(image)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(dilationMap[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (ball)
from quantimpy import morphology as mp

image = np.zeros([129,127,128],dtype=bool)
image[15:112,14:111,16:113] = ball(48,dtype=bool)

dilationMap = mp.dilate_map(image)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(dilationMap[:,:,64])
plt.show()
quantimpy.morphology.erode(image, dist, res=None)

Morphologically erode a binary Numpy array

This function performs the morphological erosion on the binary Numpy array image. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘image’ array.

Parameters
  • image (ndarray, bool) – Image can be either a 2D or 3D array of data type bool.

  • dist ({int, float}) – The distance away from the interface to which an array is dilated in the same unit of length as used in the resolution.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns a morphologically eroded Numpy array. The return data type is bool.

Return type

ndarray, bool

See also

erode_map, dilate

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphologcally dilated image can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (square)
from quantimpy import morphology as mp

image = np.zeros([128,128],dtype=bool)
image[16:112,16:112] = square(96,dtype=bool)

erosion = mp.erode(image,10)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(erosion[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (cube)
from quantimpy import morphology as mp

image = np.zeros([128,128,128],dtype=bool)
image[16:112,16:112,16:112] = cube(96,dtype=bool)

erosion = mp.erode(image,10)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(erosion[:,:,64])
plt.show()
quantimpy.morphology.erode_map(image, res=None)

Compute a morphological erosion map of a Numpy array

This function computes a morphological erosion map of the binary Numpy array image. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘image’ array.

Parameters
  • image (ndarray, bool) – Image can be either a 2D or 3D array of data type bool.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns an erosion map of a binary Numpy array. The return data type is uint16.

Return type

ndarray, uint16

See also

erode, open_map

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphological erosion map can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (disk)
from quantimpy import morphology as mp

image = np.zeros([128,128],dtype=bool)
image[16:113,16:113] = disk(48,dtype=bool)

erosionMap = mp.erode_map(image)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(erosionMap[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (ball)
from quantimpy import morphology as mp

image = np.zeros([129,127,128],dtype=bool)
image[15:112,14:111,16:113] = ball(48,dtype=bool)

erosionMap = mp.erode_map(image)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(erosionMap[:,:,64])
plt.show()
quantimpy.morphology.open(erosion, dist, res=None)

Morphologically open a binary Numpy array

This function is an alias for the function dilate(). Together with the erode() function this function performs the morphological opening operation on the binary Numpy array erosion. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘dilation’ array.

Parameters
  • erosion (ndarray, bool) – Erosion can be either a 2D or 3D array of data type bool.

  • dist ({int, float}) – The distance away from the interface to which an array is opened in the same unit of length as used in the resolution.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns a morphologically opened Numpy array. The return data type is bool.

Return type

ndarray, bool

See also

dilate, erode, close

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphologcally opened image can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (square)
from quantimpy import morphology as mp

image = np.zeros([128,128],dtype=bool)
image[16:112,16:112] = square(96,dtype=bool)

erosion = mp.erode(image,10)
opening = mp.open(erosion,10)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(opening[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (cube)
from quantimpy import morphology as mp

image = np.zeros([128,128,128],dtype=bool)
image[16:112,16:112,16:112] = cube(96,dtype=bool)

erosion = mp.erode(image,10)
opening = mp.open(erosion,10)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(opening[:,:,64])
plt.show()
quantimpy.morphology.open_map(erosion_map, res=None)

Compute a morphological opening map of a Numpy array

Together with the erode_map() function this function computes a morphological opening map of the Numpy array erosion_map. Both 2D and 3D arrays are supported. Optionally, the (anisotropic) resolution of the array can be provided using the Numpy array res. When a resolution array is provided it needs to be of the same dimension as the ‘dilation’ array.

Parameters
  • erosion_map (ndarray, float) – Erosion_map can be either a 2D or 3D array of data type float.

  • res (ndarray, {int, float}, optional) – By default the resolution is assumed to be 1 <unit of length>/pixel in all directions. If a resolution is provided it needs to be of the same dimension as the dilation array and all elements of the resolution array need to be larger than or equal to one.

Returns

out – This function returns a distance map of a morphologically closed Numpy array. The return data type is uint16.

Return type

ndarray, uint16

See also

dilate_map, open_map

Examples

These examples use the skimage Python package 3 and the Matplotlib Python package 4. For a 2D image a morphological closing map can be computed using the following example:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (square)
from quantimpy import morphology as mp

image = np.zeros([128,128],dtype=bool)
image[16:112,16:112] = square(96,dtype=bool)

erosionMap = mp.erode_map(image)
openingMap = mp.open_map(erosionMap)

plt.gray()
plt.imshow(image[:,:])
plt.show()

plt.gray()
plt.imshow(openingMap[:,:])
plt.show()

For a 3D image the following example can be used:

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (cube)
from quantimpy import morphology as mp

image = np.zeros([128,128,128],dtype=bool)
image[16:112,16:112,16:112] = cube(96,dtype=bool)

erosionMap = mp.erode_map(image)
openingMap = mp.open_map(erosionMap)

plt.gray()
plt.imshow(image[:,:,64])
plt.show()

plt.gray()
plt.imshow(openingMap[:,:,64])
plt.show()

References

1

Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt et al., “Array programming with NumPy”, Nature, vol. 585, pp 357-362, 2020, doi:10.1038/s41586-020-2649-2

2

Ingemar Ragnemalm, “Fast erosion and dilation by contour processing and thresholding of distance maps”, Pattern recognition letters, vol. 13, no. 3, pp 161-166, 1992, doi:10.1016/0167-8655(92)90055-5

3(1,2,3,4,5,6,7,8)

Stéfan van der Walt, Johannes L. Schönberger, Juan Nunez-Iglesias, François Boulogne, Joshua D. Warner, Neil Yager, Emmanuelle Gouillart, Tony Yu and the scikit-image contributors. “scikit-image: Image processing in Python.” PeerJ 2:e453 (2014) doi: 10.7717/peerj.453

4(1,2,3,4,5,6,7,8)

John D. Hunter, “Matplotlib: A 2D Graphics Environment”, Computing in Science & Engineering, vol. 9, no. 3, pp. 90-95, 2007. doi:10.1109/MCSE.2007.55