How to do it...

You need to perform the following steps to be able to apply some of the morphological operators to a binary image using the functions from the scipy.ndimage.morphology module:

  1. Read the input giraffe image and convert it into a binary image using thresholding. Obtain the optimal threshold with Otsu's algorithm and then change the pixels with values above the threshold to white and black:
im = rgb2gray(imread('images/giraffe.jpg'))thres = threshold_otsu(im)im = (im > thres).astype(np.uint8)
  1. Erode the image with a 2 x 2 square structuring element (SE/kernel) and invert the image:
eroded = binary_erosion(im, structure=np.ones((2,2)), iterations=20)[20:,20:]eroded = 1 - eroded
  1. Now, dilate the eroded image with an 11 x 11 square ...

Get Python Image Processing Cookbook 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.