这似乎是一个渗滤问题。如果您安装了scipy,则以下链接为您提供了答案。
http://dragly.org/2013/03/25/working-with-percolation-clusters-in-
python/
from pylab import *from scipy.ndimage import measurementsz2 = array([[0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0], [0,0,1,0,1,0,0,0,1,0], [0,0,0,0,0,0,1,0,1,0], [0,0,0,0,0,0,1,0,0,0], [0,0,0,0,1,0,1,0,0,0], [0,0,0,0,0,1,1,0,0,0], [0,0,0,1,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0]])
lw, num = measurements.label(z2)print lwarray([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 2, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 4, 0, 3, 0], [0, 0, 0, 0, 0, 0, 4, 0, 0, 0], [0, 0, 0, 0, 5, 0, 4, 0, 0, 0], [0, 0, 0, 0, 0, 4, 4, 0, 0, 0], [0, 0, 0, 6, 0, 4, 0, 0, 0, 0], [0, 0, 0, 0, 7, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
下面将计算它们的面积。
area = measurements.sum(z2, lw, index=arange(lw.max() + 1))print area[ 0. 2. 1. 2. 6. 1. 1. 1.]
这给出了您期望的结果,尽管我认为您的眼睛将有一个包含8个成员的集群。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)