Reputation: 147
So I want to measure the max pixel and average of pixels in each label(in multiple array) using scipy. for example
(img , other is a numpy array of an opened tif)
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
So for each label I want to find the min value of the pixel, the max value of the pixel and the average of the intensities(the area of each label I can count) for img and merge. And I want to be able to make an histogram with these counts per label(connected area in img). (I know how to make the histograms once I have the np array or list)
I was thinking in making a loop for each label and then make a binary structure only with that label and measure the values. Is there any fast scipy/numpy method to do it without going through a loop?
Thanks!
Upvotes: 2
Views: 1796
Reputation: 1452
You can use labeled_comprehension
to do this all in one shot:
#!/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
Upvotes: 1