Tuesday 15 December 2020

apply filters on images when there is no data pixels

I have image that contains many no data pixels. The image is 2d numpy array and the no-data values are "None". Whenever I try to apply on it filters, seems like the none values are taken into account into the kernel and makes my pixels dissapear.

For example, I have this image:
enter image description here

I have tried to apply on it the lee filter with this function (taken from Speckle ( Lee Filter) in Python):

from scipy.ndimage.filters import uniform_filter
from scipy.ndimage.measurements import variance

def lee_filter(img, size):
    img_mean = uniform_filter(img, (size, size))
    img_sqr_mean = uniform_filter(img**2, (size, size))
    img_variance = img_sqr_mean - img_mean**2

    overall_variance = variance(img)

    img_weights = img_variance / (img_variance + overall_variance)
    img_output = img_mean + img_weights * (img - img_mean)
    return img_output

but the results looks like this:
enter image description here

with the warnning:

UserWarning: Warning: converting a masked element to nan. dv = np.float64(self.norm.vmax) - np.float64(self.norm.vmin)

I have also tried to use the library findpeaks.


from findpeaks import findpeaks
import findpeaks
#lee enhanced filter
image_lee_enhanced = findpeaks.lee_enhanced_filter(img, win_size=3, cu=0.25)

but I get the same blank image. When I used median filter on the same image with ndimage is worked no problem.

My question is how can I run those filters on the image without letting the None values interrupt the results?

edit: I prefer not to set no value pixels to 0 because the pixel range value is between -50-1 (is an index values). In addition i'm afraid that if I change it to any other value e.g 9999) it will also influence the filter (am I wrong?)

Edit 2: I have read Cris Luengo answer and I have tried to apply something similar with the scipy.ndimage median filter as I have realized that the result is disorted as well.

This is the original image:

enter image description here

I have tried masking the Null values:


idx = np.ma.masked_where(img,img!=None)[:,1]
median_filter_img = ndimage.median_filter(img[idx].reshape(491, 473), size=10)
zeros = np.zeros([img.shape[0],img.shape[1]])

zeros[idx] = median_filter_img

The results looks like this (color is darker to see the problem in the edges):

enter image description here

As it can bee seen, seems like the edges values are inflluences by the None values. I have done this also with img!=0 but got the same problem.

(just to add: the pixels vlues are between 1 to -35)



from apply filters on images when there is no data pixels

No comments:

Post a Comment