apply_padded#

Applies padding to an image before passing through specified function

import numpy as np
import porespy as ps
import scipy.ndimage as spim
import matplotlib.pyplot as plt
import skimage
from edt import edt
ps.visualization.set_mpl_style()
[03:25:24] ERROR    PARDISO solver not installed, run `pip install pypardiso`. Otherwise,          _workspace.py:56
                    simulations will be slow. Apple M chips not supported.                                         

im#

Generate a test image. Can be 2D or 3D image.

im = np.zeros([300, 300])
im = ps.generators.rsa(im, r=20, volume_fraction=0.4) == 0

plt.figure(figsize=[6, 6])
plt.axis(False)
plt.imshow(im);
/tmp/ipykernel_2489/1290140162.py:2: DeprecationWarning: Call to deprecated function (or staticmethod) rsa. (This function will be renamed random_spheres in a future version)
  im = ps.generators.rsa(im, r=20, volume_fraction=0.4) == 0
../../../_images/6e52f5e14cb95cda2a90cd282946eda08bd2e4b87b7c2ce817bed54e1e502914.png

func#

A good use case is skeletonize_3d, which has edge artifacts that can be reduced if the image is pre-padded. It works on 2D or 3D images.

func = skimage.morphology.skeletonize_3d

pad_width#

The amount of padding to add to each axis

x1 = ps.filters.apply_padded(im=im, pad_width=1, func=func, pad_val=1)
x2 = ps.filters.apply_padded(im=im, pad_width=100, func=func, pad_val=1)

fig, ax = plt.subplots(1, 2, figsize=[12, 12]);
ax[0].imshow(x1);
ax[0].axis(False)
ax[0].set_title('pad_width = 1')
ax[1].imshow(x2);
ax[1].axis(False)
ax[1].set_title('pad_width = 100');
../../../_images/e9ae01145e2fa4b0c02b0b1e85002bb9cc648f0800b59f523e70a10733316475.png

It’s also useful for morphological operations such as binary_opening

func = skimage.morphology.binary_opening
x3 = ps.filters.apply_padded(im=im, pad_width=0, func=func, pad_val=1, footprint=ps.tools.ps_disk(15))
x4 = ps.filters.apply_padded(im=im, pad_width=50, func=func, pad_val=1, footprint=ps.tools.ps_disk(15))

fig, ax = plt.subplots(1, 2, figsize=[12, 12]);
ax[0].imshow(x3)
ax[0].axis(False)
ax[0].set_title('With no padding')
ax[1].imshow(x4)
ax[1].axis(False)
ax[1].set_title('With padding');
../../../_images/850f7a95a2ce41b1f3b3612976190ed391522ec4f21becf4eabe5f1f72972c53.png

pad_val#

The value to be placed in padded voxels. It is almost always 1 but setting to 0 may be useful for applying a distance transform.

func = edt
x5 = ps.filters.apply_padded(im=im, pad_width=10, func=func, pad_val=1)
x6 = ps.filters.apply_padded(im=im, pad_width=10, func=func, pad_val=0)

fig, ax = plt.subplots(1, 2, figsize=[12, 12]);
ax[0].imshow(x5);
ax[0].axis(False)
ax[0].set_title('pad_val = 1')
ax[1].imshow(x6);
ax[1].axis(False)
ax[1].set_title('pad_val = 0');
../../../_images/cb8bb2bda8b49ff10ff7272071ccd4b56f39f9b1b1e269678219752e3022a301.png