function to measure the (local) density of 1s in a binary 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

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.h... [2] http://scikit-image.org/docs/dev/api/skimage.morphology.html#module-skimage.... 2016-05-26 18:11 GMT+03:00 Matteo <matteo.niccoli@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@googlegroups.com. To post to this group, send email to scikit-image@googlegroups.com. To view this discussion on the web, visit https://groups.google.com/d/msgid/scikit-image/1b36234e-79ed-4eec-9c4a-ea1b6... <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.
participants (2)
-
Egor Panfilov
-
Matteo