我想计算给定numpy阵列结构的周长。周长我的意思是numpy数组中结构的确切周长。该结构可以包括孔。计算numpy阵列的周长
我目前的形式给出的是这样的:
import numpy
a = numpy.zeros((6,6), dtype=numpy.int)
a[1:5, 1:5] = 1;a[3,3] = 0
# Way 1
s = ndimage.generate_binary_structure(2,1)
c = ndimage.binary_dilation(a,s).astype(a.dtype)
b = c - a
numpy.sum(b) # The result, however the hole is calculated as 1, although there are 4 edges
# Way 2
b = ndimage.distance_transform_cdt(a == 0,metric='taxicab') == 1
b = b.astype(int)
numpy.sum(b) # same as above
正如你可以看到它显示所有相邻的细胞,但是它们的总和不等于补丁的周长。示例数组中的孔计算为1,但它正确地具有4条边。有不同形状的大孔有类似的问题。
我在过去曾问过类似的问题,但都提供了解决方案,但最终无法解决正确的输出值问题。 有人有一个想法如何做到这一点? 请不要包含比numpy,scipy和基础包更多的其他软件包。
在这个例子中你期望什么值? – Eric
一个整数值。在上述测试数据集中,最终值应该是20,因为有20个边。 – Curlew