Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
消除面积比小于treshold python的连接像素数_Python_Arrays_Mask_Connected Components - Fatal编程技术网

消除面积比小于treshold python的连接像素数

消除面积比小于treshold python的连接像素数,python,arrays,mask,connected-components,Python,Arrays,Mask,Connected Components,我在这个问题上看了下面的例子:它非常接近我所需要的 然而,那里的分析是基于连接像素的数量,比如说,现在我想连同低于一定数量像素的区域一起移除,也就是纵横比不同于“正方形”的区域 例如,在下图(左面板)中,假设我有一条1900像素的红线,这意味着使用treshold # now remove the labels for label,size in enumerate(label_size): if size < 1800: Z[Zlabeled == label]

我在这个问题上看了下面的例子:它非常接近我所需要的

然而,那里的分析是基于连接像素的数量,比如说,现在我想连同低于一定数量像素的区域一起移除,也就是纵横比不同于“正方形”的区域

例如,在下图(左面板)中,假设我有一条1900像素的红线,这意味着使用treshold

# now remove the labels
for label,size in enumerate(label_size):
    if size < 1800:
        Z[Zlabeled == label] = 0
#现在移除标签
对于标签,枚举中的大小(标签大小):
如果尺寸小于1800:
Z[Z标签==标签]=0
红线不会被消除。但是如果现在我增加阈值(比如说2000),我可能会消除右边面板上的两个大数字,这是我想要的输出。我需要如何修改代码来考虑连接组件的纵横比?


提前感谢:

一个可能的解决方案是使用以这种方式完成的连接组件分析

from scipy.ndimage.measurements import label
structure = np.ones((3, 3), dtype=np.int)


labeled, ncomponents = label(Z, structure)



indices = np.indices(Z.shape).T[:,:,[1, 0]]





for i in range(1,ncomponents):
    pixelcount = (np.sum(labeled==i))

    xs = indices[labeled==i][:,0]
    ys = indices[labeled==i][:,1]
    area = (np.max(xs)-np.min(xs)+2)*(np.max(ys)-np.min(ys)+1)
    if (pixelcount/area<1):
        pass
        labeled[labeled==i] = 0

plt.figure(1)
plt.imshow(labeled,cmap='jet')
来自scipy.ndimage.measurements导入标签
结构=np.one((3,3),数据类型=np.int)
已标记,ncomponents=标签(Z,结构)
指数=np.指数(Z.形状).T[:,:,[1,0]]
对于范围内的i(1,n组分):
像素计数=(np.sum(标记==i))
xs=索引[标记==i][:,0]
ys=索引[标记==i][:,1]
面积=(np.max(xs)-np.min(xs)+2)*(np.max(ys)-np.min(ys)+1)
if(像素计数/面积)