Re: function to measure the (local) density of 1s in a binary 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 написал:
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
: 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.
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 On Thursday, May 26, 2016 at 9:40:40 AM UTC-6, Egor Panfilov wrote:
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 написал:
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
javascript:>: 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...@googlegroups.com javascript:. To post to this group, send email to scikit...@googlegroups.com javascript:. 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