Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 630
Nick Avatar asked Apr 06 '13 17:04

Nick


People also ask

How do you dilate an image in Python?

dilate() is an OpenCV function in Python that applies a morphological filter to images. The cv2. dilate() method takes two inputs, of which one is our input image; the second is called the structuring element or kernel, which decides the nature of the operation. Image dilation Increases the object area.

What is erosion and dilation in image processing?

Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. The number of pixels added or removed from the objects in an image depends on the size and shape of the structuring element used to process the image.

How do you erode an image in Python?

To perform erosion on images, use cv2. erode() method. The erode() is generally performed on binary images. The erode() method requires two inputs; one is an input image, and the second is called a structuring element or kernel, which decides the nature of the operation.

What is erosion and dilation in OpenCV?

Erosion and Dilation are morphological image processing operations. OpenCV morphological image processing is a procedure for modifying the geometric structure in the image. In morphism, we find the shape and size or structure of an object.


1 Answers

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.

like image 126
Nick Avatar answered Oct 01 '22 04:10

Nick