[Numpy-discussion] How to median filter a masked array?

Peter Verveer verveer at embl-heidelberg.de
Wed Jul 14 10:39:59 CDT 2004

On 14 Jul 2004, at 17:47, Russell E Owen wrote:
> I want to 3x3 median filter a masked array (2-d array of ints -- an 
> astronomical image), where the masked data and points off the edge are 
> excluded from the local median calculation. Any suggestions for how to 
> do this efficiently?

I don't think that you can do it very efficiently right now with the 
functions that are available in numarray.

>  I suspect I have to write it in C, which is an unpleasant prospect.

Yes, that is unpleasant, trust me :-) However, in version 1.0 of 
numarray in the nd_image package, I have added some support for writing 
filter functions. The generic_filter() function iterates over the array 
and applies a user-defined filter function at each element. The 
user-defined function can be written in python or in C, and is called 
at each element with the values within the filter-footprint as an 
argument. You would write a function that finds the median of these 
values, excluding the NaNs (or whatever value that flags the mask.) I 
would suggest to prototype this function in python and move that to C 
as soon as it works to your satisfaction. See the numarray manual for 
more details.

Cheers, Peter

More information about the Numpy-discussion mailing list