Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 6249
License: OTHER
Kernel: Python 3
from __future__ import division, print_function %matplotlib inline

Morphological operations

Morphology is the study of shapes. In image processing, some simple operations can get you a long way. The first things to learn are erosion and dilation. In erosion, we look at a pixel’s local neighborhood and replace the value of that pixel with the minimum value of that neighborhood. In dilation, we instead choose the maximum.

import numpy as np from matplotlib import pyplot as plt, cm import skdemo plt.rcParams['image.cmap'] = 'cubehelix' plt.rcParams['image.interpolation'] = 'none'
image = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8) plt.imshow(image)
<matplotlib.image.AxesImage at 0x107af2510>
Image in a Jupyter notebook

The documentation for scikit-image's morphology module is here.

Importantly, we must use a structuring element, which defines the local neighborhood of each pixel. To get every neighbor (up, down, left, right, and diagonals), use morphology.square; to avoid diagonals, use morphology.diamond:

from skimage import morphology sq = morphology.square(width=3) dia = morphology.diamond(radius=1) disk = morphology.disk(radius=30) skdemo.imshow_all(sq, dia, disk)
Image in a Jupyter notebook

The central value of the structuring element represents the pixel being considered, and the surrounding values are the neighbors: a 1 value means that pixel counts as a neighbor, while a 0 value does not. So:

skdemo.imshow_all(image, morphology.erosion(image, sq), shape=(1, 2))
Image in a Jupyter notebook

and

skdemo.imshow_all(image, morphology.dilation(image, sq))
Image in a Jupyter notebook

and

skdemo.imshow_all(image, morphology.dilation(image, dia))
Image in a Jupyter notebook

Erosion and dilation can be combined into two slightly more sophisticated operations, opening and closing. Here's an example:

image = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8) plt.imshow(image)
<matplotlib.image.AxesImage at 0x1092503d0>
Image in a Jupyter notebook

What happens when run an erosion followed by a dilation of this image?

What about the reverse?

Try to imagine the operations in your head before trying them out below.

skdemo.imshow_all(image, morphology.opening(image, sq)) # erosion -> dilation
Image in a Jupyter notebook
skdemo.imshow_all(image, morphology.closing(image, sq)) # dilation -> erosion
Image in a Jupyter notebook

Exercise: use morphological operations to remove noise from a binary image.

from skimage import data, color hub = color.rgb2gray(data.hubble_deep_field()[350:450, 90:190]) plt.imshow(hub)
<matplotlib.image.AxesImage at 0x1092624d0>
Image in a Jupyter notebook

Remove the smaller objects to retrieve the large galaxy.

disk = morphology.disk(radius=8) gal = morphology.opening(hub, disk) plt.imshow(gal) skdemo.imshow_with_histogram(gal)
/Users/nuneziglesiasj/anaconda/lib/python2.7/site-packages/scikit_image-0.11dev-py2.7-macosx-10.5-x86_64.egg/skimage/util/dtype.py:107: UserWarning: Possible precision loss when converting from float64 to uint8 "%s to %s" % (dtypeobj_in, dtypeobj))
(<matplotlib.axes.AxesSubplot at 0x1096b59d0>, <matplotlib.axes.AxesSubplot at 0x109358b10>)
Image in a Jupyter notebookImage in a Jupyter notebook
gal_selector = gal > 50 # use numpy boolean indexing to estimate histogram # of brightness intensity on isolated galaxy skdemo.imshow_all(hub, gal_selector) f = plt.figure() plt.hist(hub[gal_selector], bins=50);
Image in a Jupyter notebookImage in a Jupyter notebook