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()

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/72dceb04e5a17a392c3a5bdd2d7270c32811f10808ee41149a91003225ab0a64.svg
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/6a9bf4be6af959b4c177c4928fbceaf0be7849937cd5f5a6595efe9938ce805e.svg

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/b5975e39adccb9874dc096df94b61031e0706c50a09226f33103be32e6662053.svg
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/abb93e2e0fd983e11ddc2d95d08e30388e027fc9c83dfa18d46193232834a2c7.svg