所以我想用scipy测量每个标签(多个数组中)的最大像素和像素平均值。 例如
(img,其他是一个打开的tif的numpy数组)
import numpy as np
import scipy.ndimage as ndi
a = img > 200
labels,nb = ndi.labels(a)
merge = np.zeros(img.shape)
merge = other * a
因此,对于每个标签,我想找到像素的最小值,像素的最大值和img和合并的强度平均值(我可以计算的每个标签的面积)。我希望能够为每个标签(img中的连接区域)制作直方图。 (一旦我有了np数组或列表,我知道如何制作直方图)
我正在考虑为每个标签创建一个循环,然后仅使用该标签创建二进制结构并测量值。有没有快速的scipy / numpy方法可以在不经过循环的情况下完成它?
谢谢!
答案 0 :(得分:1)
您可以一次性使用labeled_comprehension
执行此操作:
#!/usr/bin/env python2.7
import numpy as np
from scipy import ndimage as nd
hist = []
def analyze(x):
xmin = x.min()
xmax = x.max()
xmean = x.mean()
xhist = np.histogram(x, range=(xmin, xmax))
hist.append({'min': xmin,
'max': xmax,
'mean': xmean,
'hist': xhist})
return 1
a = np.array([[1, 2, 0, 0],
[5, 3, 0, 4],
[0, 0, 0, 7],
[9, 3, 0, 0]])
lbl, nlbl = nd.label(a)
lbls = np.arange(1, nlbl + 1)
nd.labeled_comprehension(a, lbl, lbls, analyze, float, -1)
print a
print
print lbl
print
print hist