【问题标题】:Image erosion and dilation with Scipy使用 Scipy 进行图像腐蚀和膨胀
【发布时间】:2013-03-29 01:06:10
【问题描述】:

我正在尝试使用 scipy 来处理图像的erosion 和dilation。使用 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)

这是输出:

膨胀的输出只是原始“flower.png”的全白图像

我相信我必须指定一个更好的内核或掩码,但我不确定为什么我会得到绿色输出来表示腐蚀而完全白色输出来表示膨胀。

【问题讨论】:

  • 二进制运算符期望二进制输入与您的不同

标签: python image image-processing scipy


【解决方案1】:

我使用的是二进制腐蚀而不是 grey erosion 数组。我使用flatten=true 将原始图像转换为灰度,如下所示:

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

然后调用:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      相关资源
      最近更新 更多