我想计算给定 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
我以前曾提出过类似的问题,但是所有提供的解决方案最终都无法得到正确的输出值。 有人有办法解决这个问题吗? 请勿使用除numpy、scipy和基本包之外的其他软件包。