Replacing NaN with 3x3 average

Hi,

I currently have images that contain some NaN values within them. I would like SNAP to replace these NaN pixels with the average value of the 3x3 matrix of pixels around the NaN pixel - is there a way to do this? I do not want to do this operation for the whole image, as this would distort my results.

Many thanks,
Ludo

I think it is possible to apply kernal filter,

Following you could find more details, but not in SNAP

in case the NaN pixels are only single pixels you can

  1. calculate an average product using the 3x3 mean (or median) filter. No more NaNs should be left then in the output raster (let’s say it is named F3).
  2. open the band maths of your original product (O1): and enter something like
    IF NaN THEN F3 ELSE O1
    The output shoult have replaced these NaNs by the filtered pixels wherever one is NaN.
2 Likes

@ABraun but in your case the value of whole band will be charged, I got from our colleague post, that he wants to replace the nan by matrix 3×3 around , without affects the other values,

@falahfakhri I don’t think so. The filtered image is computed as an intermediate product, but only those filtered values which lie at NaN positions will be replaced in the final (third) product after the band maths, all others remain the original (ELSE O1)…

works great if the NaN areas are small, otherwise you need a larger filter

0 read your NaN value

In my case it is -32768, leaved it checked for the filtering
grafik

1 Filter the image

original with NaNs (left) and 3x3 filtered (right)

2 disable NaN value in the properties

3 band maths

calculate the corrected band (replacing -32768)

4 result

original (left) and corrected (middle) band, Only the NaNs were replaced, the rest is the original file (as seen in the cyan maximum in the middle and the patterns on the right.

2 Likes

Thank you both for you help - this has solved my problem!