# fftmorphology#

Applies morphological operations (dilation/erosion/opening/closing) on binary images using scipy’s fast Fourier transform convolution method.

import matplotlib.pyplot as plt
import numpy as np
import porespy as ps
from skimage.morphology import disk
import inspect
ps.visualization.set_mpl_style()
inspect.signature(ps.filters.fftmorphology)

<Signature (im, strel, mode='opening')>


## im#

The input image is a binary image, with True (or 1) values indicating the phase on which the morphological operation is performed.

np.random.seed(10)
im = ps.generators.blobs(shape=[500,500])
fig, ax = plt.subplots(1, 1, figsize=[12, 5])
ax.imshow(im, origin='lower', interpolation='none')
ax.axis(False);


## strel#

The structuring element to use for the morphological operation. Note that for a 2D and 3D images, the strucuring elements must be 2D and 3D, respectively. By default the fftmorphology operation is morphological opening.

im_opening = ps.filters.fftmorphology(im, strel=disk(5))
im_diff=im_opening*1.0-im
fig, ax = plt.subplots(1, 3, figsize=[15, 5])
ax[0].imshow(im, origin='lower', interpolation='none')
ax[0].axis(False)
ax[1].imshow(im_opening, origin='lower', interpolation='none')
ax[1].axis(False)
ax[2].imshow(im_diff, origin='lower', interpolation='none')
ax[2].axis(False);


## mode#

By default the fftmorphology operation is morphological opening. Let’s try different operations:

im_opening = ps.filters.fftmorphology(im, strel=disk(5))
im_closing = ps.filters.fftmorphology(im, strel=disk(5), mode='closing')
im_erosion = ps.filters.fftmorphology(im, strel=disk(5), mode='erosion')
im_dilation = ps.filters.fftmorphology(im, strel=disk(5), mode='dilation')
fig, ax = plt.subplots(1, 5, figsize=[15, 5])
ax[0].imshow(im, origin='lower', interpolation='none')
ax[0].set_title('original')
ax[0].axis(False)
ax[1].imshow(im_opening, origin='lower', interpolation='none')
ax[1].set_title('opening')
ax[1].axis(False)
ax[2].imshow(im_closing, origin='lower', interpolation='none')
ax[2].set_title('closing')
ax[2].axis(False);
ax[3].imshow(im_erosion, origin='lower', interpolation='none')
ax[3].set_title('erosion')
ax[3].axis(False);
ax[4].imshow(im_dilation, origin='lower', interpolation='none')
ax[4].set_title('dilation')
ax[4].axis(False);