当我使用ndimage.label(img)
从scipy
包中导入来标记灰度 PNG 图像时,它的行为是这样的。
我有两张图片,其中一些形状由以下人员制作Photoshop
:
第一张图片:
test_one http://imageshack.us/a/img140/8669/onehx.png
我在上面的图片上执行此代码。
>>> from scipy.misc import imread
>>> from scipy.ndimage import (label,find_objects)
>>> img=imread('first.jpg')
>>> x,y = label(img)
>>> print y # Prints exactly "4" shapes ,which is right.
4
>>> f=find_objects(x)
>>> print f # Returns exactly the "4" slices of the considered shapes.
[(slice(16L, 61L, None), slice(149L, 189L, None)),
(slice(30L, 40L, None), slice(60L, 90L, None)),
(slice(50L, 70L, None), slice(20L, 120L, None)),
(slice(96L, 149L, None), slice(130L, 186L, None))]
到目前为止,它工作正常。
但是当我用光滑的刷子制作一个形状时,如下所示:
第二张图片:
test_one http://imageshack.us/a/img822/5696/twozg.png
我在第二张图片上执行此代码
>>> from scipy.misc import imread
>>> from scipy.ndimage import (label,find_objects)
>>> img=imread('second.jpg')
>>> x,y = label(img)
>>>print y # Prints more than "5" shapes ,which is wrong.
6
>>> f=find_objects(x)
>>> print f # Return more than the "5" slices of the considered shapes.
#But still has the "5" slices of the "5" considered shapes
#among the other slices which I'm confused of.
[(slice(16L, 61L, None), slice(149L, 189L, None)),
(slice(30L, 40L, None), slice(60L, 90L, None)),
(slice(50L, 70L, None), slice(20L, 120L, None)),
(slice(96L, 149L, None), slice(130L, 186L, None)),
(slice(126L, 170L, None), slice(65L, 109L, None)),
(slice(127L, 128L, None), slice(79L, 80L, None))] #This is the extra object.
我只想知道为什么ndimage.label(img)
当我使用光滑的刷子时标记的形状比考虑的形状要多。
是的,它可以标记考虑的形状,但为什么要额外标记以及如何摆脱额外标记的形状。
注意:
(1)多余的形状不是偶数形状,它们是有点薄的黑色区域。!!
(2) 如果图像是 RGB 格式,它的行为方式相同。
(3) 用光滑画笔绘制的形状中的非零值模式如下所示:
>>> obj_6 #Not quite right but it's similar to this structure
array([[ 0, 0, 1, 1, 1, 1, 0, 0],
[ 0, 1, 6, 12, 15, 9, 3, 0],
[ 0, 7, 24, 50, 57, 35, 12, 1],
[ 2, 14, 52, 105, 119, 74, 24, 3],
[ 2, 16, 60, 122, 139, 86, 29, 4],
[ 1, 10, 37, 77, 88, 54, 18, 3],
[ 0, 3, 12, 25, 29, 18, 5, 1],
[ 0, 0, 1, 4, 5, 3, 1, 0]], dtype=uint8)
(4) 全面了解:
一:
二:
感谢您的耐心等待。
更新(1):
为了清楚起见,我发布了两张图片和相关结果: