apply_padded

Import packages

[1]:
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()

Create image and variables

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

plt.figure(figsize=[4, 4])
plt.axis(False)
plt.imshow(im);
../../../_images/examples_filters_howtos_apply_padded_4_0.svg

Apply filter function

A good use case if the application of the skeletonize_3d, which has edge artifacts that can be reduced if the image is pre-padded. ### pad_width

[3]:
func = skimage.morphology.skeletonize_3d
[4]:
x = ps.filters.apply_padded(im=im, pad_width=1, func=func, pad_val=1)
xx = ps.filters.apply_padded(im=im, pad_width=100, func=func, pad_val=1)

fig, ax = plt.subplots(1, 2, figsize=[8, 4]);
ax[0].imshow(x);
ax[1].imshow(xx);
ax[0].axis(False)
ax[1].axis(False)
ax[0].set_title('pad_width = 1')
ax[1].set_title('pad_width = 100');
../../../_images/examples_filters_howtos_apply_padded_7_0.svg

func

It’s also useful for morphological operations. Note that additional keyword arguments are passed to the function.

[5]:
func = skimage.morphology.binary_opening
[6]:
x = ps.filters.apply_padded(im=im, pad_width=0, func=func, pad_val=1, selem=ps.tools.ps_disk(15))
xx = ps.filters.apply_padded(im=im, pad_width=50, func=func, pad_val=1, selem=ps.tools.ps_disk(15))

fig, ax = plt.subplots(1, 2, figsize=[8, 4]);
ax[0].imshow(x)
ax[1].imshow(xx)
ax[0].axis(False)
ax[1].axis(False)
ax[0].set_title('With no padding')
ax[1].set_title('With padding');
../../../_images/examples_filters_howtos_apply_padded_10_0.svg

pad_val

The value used to pad can be specified. It is almost always desired to be 1. Setting to 0 may be useful for applying distance transforms:

[7]:
func = edt
[8]:
x = ps.filters.apply_padded(im=im, pad_width=10, func=func, pad_val=1)
xx = ps.filters.apply_padded(im=im, pad_width=10, func=func, pad_val=0)

fig, ax = plt.subplots(1, 2, figsize=[8, 4]);
ax[0].imshow(x);
ax[1].imshow(xx);
ax[0].axis(False)
ax[1].axis(False)
ax[0].set_title('pad_val = 1')
ax[1].set_title('pad_val = 0');
../../../_images/examples_filters_howtos_apply_padded_13_0.svg