简体   繁体   English

Python中的散斑(李过滤器)

[英]Speckle ( Lee Filter) in Python

I am trying to do speckle noise removal in satellite SAR image.I am not getting any package which does speckle noise removal in SAR image.我正在尝试在卫星 SAR 图像中去除斑点噪声。我没有得到任何在 SAR 图像中去除斑点噪声的包。 I have tried pyradar but it works with python 2.7 and I am working on Anaconda with python 3.5 on windows.我尝试过 pyradar,但它适用于 python 2.7,我正在 Windows 上使用 python 3.5 使用 Anaconda。 Also Rsgislib is available but it is on Linux. Rsgislib 也可用,但它在 Linux 上。 Joseph meiring has also given a Lee filter code on github but it fails to work. Joseph Meiring 也在 github 上给出了 Lee 过滤器代码,但它无法工作。 : https://github.com/reptillicus/LeeFilter : https://github.com/reptillicus/LeeFilter

Kindly, can anyone share the python script for Speckle Filter or how to proceed for speckle filter design in python.请问,任何人都可以分享斑点过滤器的python脚本或如何在python中进行斑点过滤器设计。

This is a fun little problem.这是一个有趣的小问题。 Rather than try to find a library for it, why not write it from the definition?与其尝试为它找到一个库,为什么不从定义中编写它呢?

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

If you don't want the window to be a square of size x size, just replace uniform_filter with something else (convolution with a disk, gaussian filter, etc).如果您不希望窗口是大小为 x 大小的正方形,只需将uniform_filter替换为其他内容(与磁盘、高斯滤波器等进行卷积)。 Any type of (weighted) averaging filter will do, as long as it is the same for calculating both img_mean and img_square_mean .任何类型的(加权)平均滤波器都可以,只要计算img_meanimg_square_mean

The Lee filter seems rather old-fashioned as a filter. Lee 过滤器作为过滤器似乎相当老式。 It won't behave well at edges because for any window that has an edge in it, the variance is going to be much higher than the overall image variance, and therefore the weights (of the unfiltered image relative to the filtered image) are going to be close to 1.它不会在边缘表现良好,因为对于任何有边缘的窗口,方差将远高于整体图像方差,因此(未过滤图像相对于过滤图像的权重)将接近于 1。

An example:一个例子:

from pylab import *
import numpy as np
img = np.random.normal(0.5, 0.1, (100,100))
img[:,:50] += 0.25
imshow(img, vmin=0, vmax=1, cmap='gray')
imshow(lee_filter(img, 20), vmin=0, vmax=1, cmap='gray')

带边缘的嘈杂图像 李过滤

As you can see the noise reduction is very good in general, but much weaker along the edge.如您所见,降噪总体上非常好,但沿边缘要弱得多。

I'm not familiar with SAR so I don't know if Lee filter has some features that make it particularly good for speckle in SAR, but you may want to look into modern edge-aware denoisers, like guided filter or bilateral filter.我对 SAR 不熟悉,所以我不知道 Lee 滤波器是否有一些特性使其特别适合 SAR 中的散斑,但您可能想研究现代边缘感知降噪器,如引导滤波器或双边滤波器。

In general it is very difficult to see the effect of a noise filter by eye in a 2D plot.通常,在 2D 图中很难通过肉眼看到噪声滤波器的效果。 Let me demonstrate this by example.让我通过例子来证明这一点。 Suppose we have this noisy picture:假设我们有这张嘈杂的图片:

噪声示例

Let me now convert this image to a 3d mesh plot.现在让我将此图像转换为 3d 网格图。 Then it will look like this.然后它看起来像这样。 The noise becomes very clear but also the differences in depth between the left and right side of the picture.噪点变得非常清晰,但画面左侧和右侧之间的深度差异也很明显。

网格图

The library findpeaks contains many filters which are utilized from various (old python 2) libraries and rewritten to python 3. Applying the filters is very easy as shown below.findpeaks包含许多过滤器,这些过滤器从各种(旧 python 2)库中使用并重写为 python 3。应用过滤器非常容易,如下所示。 Note that this example seems not very representative for a SAR image as there is no speckle noise.请注意,此示例对于 SAR 图像似乎不是很有代表性,因为没有散斑噪声。 A mean or median filter seems to perform very well in this example.在这个例子中,均值或中值滤波器似乎表现得很好。 In speckle noise images where local heights are important, such mean/median filters can remove the peaks and thus destroy the signal of interest.在局部高度很重要的散斑噪声图像中,这种均值/中值滤波器可以去除峰值,从而破坏感兴趣的信号。

Install by:安装方式:

pip install findpeaks

Run by:运行者:

from findpeaks import findpeaks

# Read image
img = cv2.imread('noise.png')

filters = [None, 'lee','lee_enhanced','kuan', 'fastnl','bilateral','frost','median','mean']

for getfilter in filters:
    fp = findpeaks(method='topology', scale=False, denoise=getfilter, togray=True, imsize=False, window=15)
    fp.fit(img)
    fp.plot_mesh(wireframe=False, title=str(getfilter), view=(30,30))

比较方法

If you directly want to use the denoising filters, it can be done as following:如果你想直接使用去噪滤波器,可以按如下方式完成:

import findpeaks
import matplotlib.pyplot as plt

# Read image
img = cv2.imread('noise.png')

# filters parameters
# window size
winsize = 15
# damping factor for frost
k_value1 = 2.0
# damping factor for lee enhanced
k_value2 = 1.0
# coefficient of variation of noise
cu_value = 0.25
# coefficient of variation for lee enhanced of noise
cu_lee_enhanced = 0.523
# max coefficient of variation for lee enhanced
cmax_value = 1.73

# Some pre-processing
# Make grey image
img = findpeaks.stats.togray(img)
# Scale between [0-255]
img = findpeaks.stats.scale(img)

# Denoising
# fastnl
img_fastnl = findpeaks.stats.denoise(img, method='fastnl', window=winsize)
# bilateral
img_bilateral = findpeaks.stats.denoise(img, method='bilateral', window=winsize)
# frost filter
image_frost = findpeaks.frost_filter(img, damping_factor=k_value1, win_size=winsize)
# kuan filter
image_kuan = findpeaks.kuan_filter(img, win_size=winsize, cu=cu_value)
# lee filter
image_lee = findpeaks.lee_filter(img, win_size=winsize, cu=cu_value)
# lee enhanced filter
image_lee_enhanced = findpeaks.lee_enhanced_filter(img, win_size=winsize, k=k_value2, cu=cu_lee_enhanced, cmax=cmax_value)
# mean filter
image_mean = findpeaks.mean_filter(img, win_size=winsize)
# median filter
image_median = findpeaks.median_filter(img, win_size=winsize)


plt.figure(); plt.imshow(img_fastnl, cmap='gray'); plt.title('Fastnl')
plt.figure(); plt.imshow(img_bilateral, cmap='gray'); plt.title('Bilateral')
plt.figure(); plt.imshow(image_frost, cmap='gray'); plt.title('Frost')
plt.figure(); plt.imshow(image_kuan, cmap='gray'); plt.title('Kuan')
plt.figure(); plt.imshow(image_lee, cmap='gray'); plt.title('Lee')
plt.figure(); plt.imshow(image_lee_enhanced, cmap='gray'); plt.title('Lee Enhanced')
plt.figure(); plt.imshow(image_mean, cmap='gray'); plt.title('Mean')
plt.figure(); plt.imshow(image_median, cmap='gray'); plt.title('Median')

If you want to play around with the library, more examples can be found over here .如果您想使用该库,可以在此处找到更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM