为什么ndimage.label(image)会在所考虑的内容中标出额外的形状?

Som*_*lse 3 python numpy scipy

当我使用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))]
Run Code Online (Sandbox Code Playgroud)

直到现在,它运作良好.
但是当我用如此处所示的光滑刷子制作形状时:

第二张图片:
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.
Run Code Online (Sandbox Code Playgroud)

我只是想知道为什么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)
Run Code Online (Sandbox Code Playgroud)

(4)全面了解:
一:
在此输入图像描述 二:
在此输入图像描述
谢谢你的耐心.

更新(1):
为了弄清楚,我发布了两张图片和相关结果:

小智 7

要回答你的问题,它标记一个额外区域的原因是因为你使用连接为1的默认结构,并且它不知道寻找对角线连接.

除了Warren所指出的,在需要强大的标签(将对角像素视为连接)的情况下,可以改变这种结构.默认结构如下

In [32]: ndimage.morphology.generate_binary_structure(2, 1).astype("uint8")
Out[32]: 
array([[0, 1, 0],
       [1, 1, 1],
       [0, 1, 0]], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

其连接性为1.这扩展到您正在使用的任何尺寸图像.

如果您想使用标签而不必担心默认情况下获得的小额外内容,您可以通过添加"structure"关键字参数来更改对ndimage.label的调用.结构(或内核)是具有与图像(维度)相同等级的二进制对象,并且可以容易地改变.创建一个完整的排名

In [41]: struct=np.ones((3,3), dtype="bool8")

In [42]: struct
Out[42]: 
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
In [43]: ndimage.label(img, structure=struct)
Run Code Online (Sandbox Code Playgroud)

理论上,这应解决向结果添加小对象的问题.