[Tutor] Masked arrays & scipy.ndimage.<filters>
Koen De Munter
koendemunter at hotmail.com
Fri Jun 1 15:16:06 CEST 2012
----------------------------------------
> Date: Fri, 1 Jun 2012 18:50:15 +1000
> From: steve at pearwood.info
> To: tutor at python.org
> Subject: Re: [Tutor] Masked arrays & scipy.ndimage.<filters>
>
> Koen De Munter wrote:
>> Dear tutors,
>>
>> Given an image, I want to generate another image with the mean values of
>> the pixels in their neighbourhood, thereby ignoring some of the
>> neighbouring pixels (e.g. the padded boundary). I hoped I could use
>> masked arrays for that, but apparently, this does not work.
>
> Define "does not work".
>
> Do you get an exception? Your computer crashes? You get a result, but it's not
> the result you were expecting? Something else?
>
>
>
>> --
>> def fnc(buffer)
>> return numpy.mean(buffer)
>
>> mean = scipy.ndimage.generic_filter(masked_array, fnc, footprint = f, mode = 'constant', cval = 0.0)
>
>
> scipy, even more than numpy, is a fairly specialised library. You may have
> better luck asking this question on a dedicated scipy mailing list.
>
>> Is there a way to get around this issue, or do I have to accept the filter function can not handle masked arrays?
>
> What does the documentation for generic_filter say?
>
> What happens if you test it with a really small, simple example, say an array
> of just five or six values? Can you show us what you expect to get and what
> you actually get?
>
>
>
> --
> Steven
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
Hi Steven,
Thanks for your reply.
1) "does not work"
------------------
With this I mean that each time, all pixels belonging to the neighbourhood are involved in the calculation of the mean value.
My neighbourhood is a circle, defined in a binary square footprint array (1 = neighbourhood, 0 = not neighbourhood).
With
a masked array, I aim to command the filter method as follows: "ignore
those pixels that are identified by the mask array as 'invalid'".
But,
it seems the generic_method doesn't work with masked arrays: it only
takes account for the data part of a masked array, not the mask part.
So: result is not what I expect to get.
For the bigger story I can refer to the following link:
http://stackoverflow.com/questions/10683596/efficiently-calculating-boundary-adapted-neighbourhood-average
2) A small example
------------------
d = array([[0, 0, 0, 0],[0, 1, 6, 0],[0, 0, 0, 0]]) #this represents an image array [1, 6], surrounded by zeroes
d = d.astype(float)
m = array([[1, 1, 1, 1],[1, 0, 0, 1],[1, 1, 1, 1]]) #the ones in the mask have a meaning of 'invalid'
md = numpy.ma.masked_array(d, mask = m)
f = array([[0, 1, 0],[1, 1, 1],[0, 1, 0]]) #small footprint
def fnc(buffer):
return numpy.mean(buffer)
avg = scipy.ndimage.generic_filter(md, fnc, footprint = f, mode = 'constant', cval = 0.0)
--> what I expect to get:
array([[ 0. , 1. , 6. , 0. ],
[ 1. , 3.5, 3.5, 6. ],
[ 0. , 1. , 6. , 0. ]])
--> what I actually get:
array([[ 0. , 0.2, 1.2, 0. ],
[ 0.2, 1.4, 1.4, 1.2],
[ 0. , 0.2, 1.2, 0. ]])
Footprint f has 5 elements. I expect this number to change according to the number of elements that should be ignored.
But that doesn't. The code as I presented above always calculates the mean of the full 5 members of the footprint/neighbourhood.
Regards,
Koen
More information about the Tutor
mailing list