Most efficient way to calculate average value of labelled objects within an image

174 views
Skip to first unread message

Robin Wilson

unread,
May 13, 2016, 5:54:51 AM5/13/16
to scikit-image
Hi,

I have a labelled image, where each individual connected object has a unique integer value (eg. as produced from skimage.measure.label), and I want to get the mean value of these pixels from another image (eg. the image that I originally segmented before labelling).

What is the most efficient way to do this? The naive way is to loop over the values in the image calculating it for each one - but I assume that numpy (or skimage itself) has a far better way of doing this...

Thanks,

Robin

Dr Robin Wilson
Research Fellow
University of Southampton, UK

Egor Panfilov

unread,
May 13, 2016, 7:01:55 AM5/13/16
to scikit...@googlegroups.com
Hello Robin!

Without any doubts, there is a better way to do this. You could take a look at the informative documentation section on Indexing (http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#).

The most simple and common solution could be:

import numpy as np

arr = np.random.rand(9).reshape((3, 3))
labels = (arr > 0.5).astype(np.uint)

print(arr)
print(labels)

for label in np.unique(labels):
    mask = labels == label
    print(label, np.mean(arr[mask])) 

Egor

--
You received this message because you are subscribed to the Google Groups "scikit-image" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image...@googlegroups.com.
To post to this group, send email to scikit...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scikit-image/87b5d106-00d9-44bf-a89b-bff09ca7d00b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robin Wilson

unread,
May 13, 2016, 7:16:31 AM5/13/16
to scikit-image
Hi,

I probably didn't explain it very well, but that was the sort of loop I meant when I said "loop over all of the values of the image calculating it for each one" (values was probably a bad choice of word, I meant individual label values).

I was wondering whether there was a more efficient way to do this using some sort of clever numpy or skimage function? That is, without manually looping over each label value (ie. 0 to the number of labels).

Cheers,

Robin

Jaime Fernández del Río

unread,
May 13, 2016, 7:43:23 AM5/13/16
to scikit...@googlegroups.com
On Fri, May 13, 2016 at 1:16 PM, 'Robin Wilson' via scikit-image <scikit...@googlegroups.com> wrote:
Hi,

I probably didn't explain it very well, but that was the sort of loop I meant when I said "loop over all of the values of the image calculating it for each one" (values was probably a bad choice of word, I meant individual label values).

I was wondering whether there was a more efficient way to do this using some sort of clever numpy or skimage function? That is, without manually looping over each label value (ie. 0 to the number of labels).

There's scipy.ndimage.measurements.mean which does just that.

Jaime
 

For more options, visit https://groups.google.com/d/optout.



--
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.

Tamas H.

unread,
May 13, 2016, 1:58:00 PM5/13/16
to scikit...@googlegroups.com
How about this: maskimg is the image with the labels, img is the image you want to analyze, then:

for i in range(1, maskimg.max()):
    img[ maskimg==i].mean()
...

Juan Nunez-Iglesias

unread,
May 16, 2016, 11:04:53 PM5/16/16
to scikit...@googlegroups.com
This seems like a job for skimage.measure.regionprops...?

Reply all
Reply to author
Forward
0 new messages