-1

I have a numpy 2d array (image) with values >=0.0 but there are some indices with value 'nan'. When I try to plot this figure it looks like the below image. The 0 in the image array becomes black and nan becomes white.

enter image description here

Now I want to apply gaussian smoothing on this array such that the after smoothing when I try to plot smoothed array, the nan values from the previous array appears white only.

rahul saxena
  • 109
  • 1
  • 3
  • 14
  • Why do you think you need a Gaussian filter? There are other questions and answers to how to filter data that contains NaN (like https://stackoverflow.com/a/36307291/512111). – j08lue Oct 02 '17 at 13:25

1 Answers1

0

When doing a gaussian filter of an image, any pixel close to a nan pixel will also turn into a nan, since its new value is the weighted sum over all neighboring pixels covered by the convolution kernel.

Therefore, smoothing this particular image using, for example, scipy.ndimage.filters.gaussian_filter() will turn a lot of your zero values into nans and you'll lose a lot of information.

To smooth this (apparently binary) data, I would replace the nans by some other value, of course maintaining contrast to the non-nan pixels. For example, turn the zeros into ones, and the nans into zeros, then apply the filter.

  • 2
    [`astropy.convolution`](http://docs.astropy.org/en/stable/convolution/) supports NaN aware Gaussian filtering. – j08lue Oct 02 '17 at 13:26