Extracting the boundary 

The erosion operation can be used to extract the boundary of a binary image—we just need to subtract the eroded image from the input binary image to extract the boundary. The following code block implements this:

from skimage.morphology import binary_erosionim = rgb2gray(imread('../images/horse-dog.jpg'))threshold = 0.5im[im < threshold] = 0im[im >= threshold] = 1boundary = im - binary_erosion(im)plot_images_horizontally(im, boundary, 'boundary',sz=(18,9))

The following screenshot shows the output of the previous code block:

Get Hands-On Image Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.