【问题标题】:Split image containing multiple, irregularly shaped images in Python在 Python 中拆分包含多个不规则形状图像的图像
【发布时间】:2015-05-17 18:46:24
【问题描述】:

给定一个包含多个不规则大小和形状的图像(为简单起见,此处显示为圆形)的图像:

...我该怎么做:

  1. 检测子图像
  2. 将子图像分割并保存为单独的文件?

理想情况下,我正在寻找 python 解决方案。我已经尝试过“连接分量分析”算法和质心测量,但第一个算法对于给定的不均匀图像会崩溃,我不确定如何应用第二个算法来提取单独的图像。

请注意,我不是在问关于将图像分割成大小相等、均匀的部分,这在 SO 上已被多次询问和回答。

感谢您提供的任何帮助。

【问题讨论】:

标签: python image image-processing connected-components


【解决方案1】:

如果我们可以假设背景是统一的并且与子图像不同,那么以下方法应该有效:

  1. 通过简单地屏蔽背景颜色来执行背景减除(此外,如果子图像的内部部分可以包含背景颜色,则洪水填充算法在此处会更好地工作)。

  2. 执行连通分量分析。

这是上面给出的图像的python示例:

from scipy import ndimage
import matplotlib.pyplot as plt

# Load
img = ndimage.imread("image.png")

# Threshold based on pixel (0,0) assumed to be background
bg = img[0, 0]
mask = img != bg
mask = mask[:, :, 0]  # Take the first channel for RGB images

# Connected components
label_im, nb_labels = ndimage.label(mask)

# Plot
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.imshow(img, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(132)
plt.imshow(mask, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(133)
plt.imshow(label_im, cmap=plt.cm.spectral)
plt.axis('off')
plt.subplots_adjust(wspace=0.02, hspace=0.02, top=1, bottom=0, left=0, right=1)
plt.show()

图像的结果(任意形状):

现在,剩下的任务是根据 label_im 值保存/存储每个子图像。

【讨论】:

  • 看起来不错,谢谢!!如果背景大多是统一的(例如白色桌子上的物体图片),是否有更好的方法来确定背景蒙版?
  • 有很多方法,但您可以在这里查看我的答案:stackoverflow.com/a/30283109/1576602 以获得建议。基本上,这些方法中的大多数都是通过建立背景和前景的颜色分布模型,然后执行分割来工作的。
猜你喜欢
  • 2018-01-20
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多