function to measure the (local) density of 1s in a binary image

Egor Panfilov multicolor.mood at gmail.com
Thu May 26 11:29:57 EDT 2016


Hi Matteo!

The function you're looking for is as simple as convolution [1] with a
typical structuring element [2] (we have square, circle and many more). I
suppose, this can also be called as 'mean filter':

Here is an example:

> import numpy as np
> import scipy.ndimage as ndi
> from skimage.morphology import square
>
> img = np.random.uniform(size=(5, 5))
> img = (img > 0.5).astype(np.float)
> sem = square(3)/3**2   # Don't forget to normalize
> out = ndi.convolve(img, sem, mode='wrap')
>


print(img)
> print(sem)
> print(out)


Results to:

> [[ 1.  0.  0.  1.  0.]
>  [ 1.  0.  0.  0.  0.]
>  [ 0.  0.  1.  1.  0.]
>  [ 0.  0.  0.  0.  1.]
>  [ 1.  0.  1.  0.  0.]]
> [[ 0.11111111  0.11111111  0.11111111]
>  [ 0.11111111  0.11111111  0.11111111]
>  [ 0.11111111  0.11111111  0.11111111]]
> [[ 0.33333333  0.44444444  0.22222222  0.22222222  0.44444444]
>  [ 0.22222222  0.33333333  0.33333333  0.33333333  0.44444444]
>  [ 0.22222222  0.22222222  0.22222222  0.33333333  0.33333333]
>  [ 0.22222222  0.33333333  0.33333333  0.44444444  0.33333333]
>  [ 0.33333333  0.33333333  0.22222222  0.33333333  0.44444444]]


Cheers,
Egor

[1]
http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.convolve.html#scipy.ndimage.convolve

[2]
http://scikit-image.org/docs/dev/api/skimage.morphology.html#module-skimage.morphology

2016-05-26 18:11 GMT+03:00 Matteo <matteo.niccoli at gmail.com>:

> Is there a function in scikit-image to calculate the the local density of
> 1s in a binary image?
>
> By local density of 1s I mean:
> 1) count the number of pixels that have value of 1 in a running/sliding
> window (preferrably circular but square would work too)
> 2) divide by the total number of pixels in the running window
>
> Thanks
> Matteo
>
> --
> 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/1b36234e-79ed-4eec-9c4a-ea1b659069f3%40googlegroups.com
> <https://groups.google.com/d/msgid/scikit-image/1b36234e-79ed-4eec-9c4a-ea1b659069f3%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/20160526/b3ce11d5/attachment.html>


More information about the scikit-image mailing list