How to median filter a masked array?
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 suspect I have to write it in C, which is an unpleasant prospect. I tried using NaN for points to mask out, but the median filter seems to handle those as "infinity", or something equally inappropriate. In a related vein, has Python come along far enough that it would be reasonable to add support for NaN to numarray -- in the sense that statistics calculations, filters, etc. could be convinced to ignore NaNs? Obviously this support would be contingent on compiling python with IEEE floating point support, but I suspect that's the default on most platforms these days. -- Russell
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
participants (2)
-
Peter Verveer
-
Russell E Owen