I researched the lee filter according to this: https://pro.arcgis.com/en/pro-app/3.0/help/analysis/raster-functions/speckle-function.htm.
I want it to reduce noise, but it is adding instead.
I have this python function to apply the lee filter to an image:
from scipy.ndimage.filters import uniform_filter
from scipy.ndimage.measurements import variance
def leeFilter(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 = np.var(img)
img_weights = img_variance / (img_variance + overall_variance)
img_output = img_mean + img_weights * (img - img_mean)
return img_output
Then, I'm applying it to my image as such:
currImg = cv2.imread(os.path.join(currPath,f'{i}.png'))
# Get red, green and blue channels
red = currImg[:,:,0]
green = currImg[:,:,1]
blue = currImg[:,:,2]
# Apply the filter
red = lee_filter(red,15)
green = lee_filter(green,15)
blue = lee_filter(blue,15)
# merge channels
currImg[:,:,0] = red
currImg[:,:,1] = green
currImg[:,:,2] = blue
cv2.imwrite(os.path.join(newPath,f'{i}.png'),currImg)
The problem is that using a window size of 15, it looks like it is adding and not lowering the noise of the images. Below is a comparison with the before and after images.
Cris is right about the estimated noise level, but the main issue is arithmetic overflow - result of applying the computations to
uint8elements.We may convert the input to
float32beforelee_filter, and convert the output back touint8afterlee_filter.Code sample:
Before (

before.png):After (

after.png):