我正在制作音乐识别程序,并且作为它的一部分,我需要从png(2200x1700像素)中找到numpy数组的最大连接区域。我目前的解决方案如下。加速numpy过滤
labels, nlabels = ndimage.label(blobs)
cutoff = len(blobs)*len(blobs[0])/nlabels
blobs_found = 0
x = []
t1 = time()
for n in range(1, nlabels+1):
squares = np.where(labels==n)
if len(squares[0]) < cutoff:
blobs[squares] = 0
else:
blobs_found += 1
blobs[squares] = blobs_found
x.append(squares - np.amin(squares, axis=0, keepdims=True))
nlabels = blobs_found
print(time() - t1)
这个工作,但它需要约6.5秒的运行。有没有一种方法可以从此代码中删除循环(或以其他方式加速)?
这是一个有趣的问题,你可以使[mcve](http://stackoverflow.com/help/mcve)? – BPL