extract_regions#

Import packages#

import numpy as np
import porespy as ps
import scipy.ndimage as spim
import matplotlib.pyplot as plt
import skimage
ps.visualization.set_mpl_style()
/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/openpnm/algorithms/_invasion_percolation.py:358: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
  def _find_trapped_pores(inv_seq, indices, indptr, outlets):  # pragma: no cover

Generate image for testing#

To illustrate this function, we need an image containing many labelled regions. This can obtained by generating some blobs, then using scipy.label.

np.random.seed(0)
im = ps.generators.blobs([500, 500], blobiness=2, porosity=0.4)
regions = spim.label(im)[0]
fig, ax = plt.subplots(1, 1, figsize=[4, 4])
ax.axis(False)
ax.imshow(regions);
../../../_images/fc1671fcd0f0cc9ed8898ec07d79d2e991822e69003c6c851638c8af1a681f4e.png

Apply tool#

In it’s basic form, this function is equivalent to just obtaining a boolean mask like regions == 22, but it has a few more features including extracting a sub-image that just contains the regions, and also finding multiple regions at once.

reg1 = ps.tools.extract_regions(regions=regions, labels=[22], trim=False)
reg2 = ps.tools.extract_regions(regions=regions, labels=[22], trim=True)
reg3 = ps.tools.extract_regions(regions=regions, labels=[22, 23], trim=True)
fig, ax = plt.subplots(1, 3, figsize=[8, 4])
ax[0].axis(False)
ax[0].imshow(reg1)
ax[1].axis(False)
ax[1].imshow(reg2);
ax[2].axis(False)
ax[2].imshow(reg3);
../../../_images/6454b1547450dbc293347b1645fbb604b0cbd0f53bcfd227653b5d5c74864301.png