用Scipy进行图像侵蚀和扩张

时间:2013-04-06 17:25:10

标签: python image image-processing scipy

我正在尝试使用scipy来执行图像的erosiondilation。使用scipy似乎非常简单 - > binary_erosion / dialation。但是,输出完全不是预期的。

这是我的基本代码:

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)

这是输出:

enter image description here

扩张的输出只是原始“flower.png”

的完全白色图像

我相信我必须指定一个更好的内核或面具,但我不确定为什么我得到绿色输出的侵蚀和完全白色输出扩张。

2 个答案:

答案 0 :(得分:13)

我使用二进制侵蚀而不是grey erosion数组。我使用flatten=true将原始图像转换为灰度图像:

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

然后叫:

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

虽然它是灰度的,却得到了很好的侵蚀图片。

答案 1 :(得分:2)

你有两个问题:如@theta的评论所述,二元操作期望输入仅包含0和1.第二个问题是nd中的ndimage ---你提供的形状(nx, ny, 3)的数组。最后一个长度为3的轴被认为是第三个空间维度,而不是三个颜色通道。

相关问题