Nick
Nick

Reputation: 9373

Image erosion and dilation with Scipy

I am trying to use scipy to do erosion and dilation of an image. It seems pretty straightforward using scipy -> binary_erosion / dialation. However, the output is not at all what is expected.

Here is my basic code:

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import numpy as np
import Image

#im = Image.open('flower.png')
im = ndimage.imread('flower.png')
im = ndimage.binary_erosion(im).astype(np.float32)
scipy.misc.imsave('erosion.png', im)


im2 = Image.open('flower.png')
im2 = ndimage.binary_dilation(im2)
scipy.misc.imsave('dilation.png', im2)

This is the output:

enter image description here

The output for dilation is just a completely white image for the original "flower.png"

I believe that I must specify a better kernel or mask but am not really sure why I am getting a green output for erosion and completely white output for dilation.

Upvotes: 13

Views: 17994

Answers (2)

Nick
Nick

Reputation: 9373

I was using the binary erosion instead of the grey erosion array. I converted the original image to greyscale by using flatten=true like so:

im = scipy.misc.imread('flower.png', flatten=True).astype(np.uint8)

then called:

im1 = ndimage.grey_erosion(im, size=(15,15))

And got a nicely eroded picture, although it is greyscale.

Upvotes: 13

pv.
pv.

Reputation: 35115

You have two problems: as noted in the comment by @theta, binary ops expect input consisting only of 0 and 1. The second issue is the nd in ndimage --- you supply in an array of shape (nx, ny, 3). The last length-3 axis is considered to be a third spatial dimension, not the three color channels.

Upvotes: 2

Related Questions