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

Egor Panfilov multicolor.mood at gmail.com
Fri May 13 07:01:53 EDT 2016


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

2016-05-13 12:54 GMT+03:00 'Robin Wilson' via scikit-image <
scikit-image at googlegroups.com>:

> 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
>
> --
> 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+unsubscribe at googlegroups.com.
> To post to this group, send email to scikit-image at 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
> <https://groups.google.com/d/msgid/scikit-image/87b5d106-00d9-44bf-a89b-bff09ca7d00b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20160513/8c5408ab/attachment.html>


More information about the scikit-image mailing list