trim_extrema#

Trims local extremes in the greyscale values of an image. It is useful for flattening peaks.

Import Packages#

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

im#

The function works on 2D or 3D image with greyscale values, such as a distance transform:

im = ps.generators.blobs(shape=[50, 50], porosity=0.7)
dt = edt(im)

plt.figure(figsize=[6, 6])
plt.imshow(dt/im)
plt.axis(False);
../../../_images/76e055872a699b7904c06a2d333c8eaf01616e94680aeaf042a2298e63f10d52.png
dt1 = ps.filters.trim_extrema(im=dt, h=1, mode='maxima')
dt2 = ps.filters.trim_extrema(im=dt, h=2, mode='maxima')

fig, ax = plt.subplots(2, 2, figsize=[12, 12])

stripe = np.zeros_like(dt)
stripe[10, :] = dt.max()/2

ax[0][0].imshow(dt1 + stripe)
ax[0][0].set_title('h = 25')
ax[0][0].axis(False)
ax[0][1].plot(dt[10, :])
ax[0][1].plot(dt1[10, :])
ax[0][1].set_title('Line Profile')

ax[1][0].imshow(dt2 + stripe)
ax[1][0].axis(False)
ax[1][0].set_title('h = 10')
ax[1][1].plot(dt[10, :])
ax[1][1].plot(dt2[10, :])
ax[1][1].set_title('Line Profile');
../../../_images/8793b452f91b40082617e83e58aa3a876e53f516c26807318c5a45bb53268e3f.png

mode#

The available modes are maxima, minima, and extrema. These modes specify which extremes to remove. Minima and maxima modes trim the maximum and minimum extremes respectively. The extrema mode trims both extremes.

dt1 = ps.filters.trim_extrema(im=dt, h=1, mode='minima')
dt2 = ps.filters.trim_extrema(im=dt, h=2, mode='minima')

fig, ax = plt.subplots(2, 2, figsize=[12, 12])

ax[0][0].imshow(dt1 + stripe)
ax[0][0].set_title('h = 25')
ax[0][0].axis(False)
ax[0][1].plot(dt[10, :])
ax[0][1].plot(dt1[10, :])
ax[0][1].set_title('Line Profile')

ax[1][0].imshow(dt2 + stripe)
ax[1][0].axis(False)
ax[1][0].set_title('h = 10')
ax[1][1].plot(dt[10, :])
ax[1][1].plot(dt2[10, :])
ax[1][1].set_title('Line Profile');
../../../_images/4260bf47633ddb3dd422c27b169546bc94d31044da193f1190118e7a08babc10.png
dt1 = ps.filters.trim_extrema(im=dt, h=1, mode='extrema')
dt2 = ps.filters.trim_extrema(im=dt, h=2, mode='extrema')

fig, ax = plt.subplots(2, 2, figsize=[12, 12])

ax[0][0].imshow(dt1 + stripe)
ax[0][0].set_title('h = 25')
ax[0][0].axis(False)
ax[0][1].plot(dt[10, :])
ax[0][1].plot(dt1[10, :])
ax[0][1].set_title('Line Profile')

ax[1][0].imshow(dt2 + stripe)
ax[1][0].axis(False)
ax[1][0].set_title('h = 10')
ax[1][1].plot(dt[10, :])
ax[1][1].plot(dt2[10, :])
ax[1][1].set_title('Line Profile');
../../../_images/193902c78ee4f84a3dc1a48e4c6e3e1dcda6888155498164f451aa0244a03da8.png