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

23 views
Skip to first unread message

Matteo

unread,
May 26, 2016, 11:11:55 AM5/26/16
to scikit-image
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

Egor Panfilov

unread,
May 26, 2016, 11:29:59 AM5/26/16
to scikit...@googlegroups.com
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


--
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/1b36234e-79ed-4eec-9c4a-ea1b659069f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Egor Panfilov

unread,
May 26, 2016, 11:40:40 AM5/26/16
to scikit-image
By the way :), we do have such filter in `skimage`: http://scikit-image.org/docs/dev/api/skimage.filters.rank.html#mean , but it is selective to input dtype and doesn't support many boundary conditions' alternatives.

Cheers,
Egor

четверг, 26 мая 2016 г., 18:29:59 UTC+3 пользователь Egor Panfilov написал:
To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image+unsubscribe@googlegroups.com.

Matteo

unread,
May 26, 2016, 12:02:31 PM5/26/16
to scikit-image
Hi Egor

For soem reason had not occurred to me to think of it as a mean filter.
I like your ndimage + skimage solution, which will allow using circle, maybe I'll even try a diamond. Thanks a lot,
Matteo
To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages