自适应中值滤波python代码
时间: 2023-11-20 07:55:58 浏览: 246
以下是自适应中值滤波的Python代码:
```python
import numpy as np
from skimage.morphology import square
from skimage.filters import median
def adaptive_median_filter(img, max_window_size):
img_out = np.zeros_like(img)
img_pad = np.pad(img, max_window_size, mode='symmetric')
for i in range(img.shape[0]):
for j in range(img.shape[1]):
window_size = 3
while window_size <= max_window_size:
window = img_pad[i:i+window_size, j:j+window_size]
median_val = np.median(window)
min_val = np.min(window)
max_val = np.max(window)
if min_val < median_val < max_val:
img_out[i, j] = median_val
break
else:
window_size += 2
if window_size > max_window_size:
img_out[i, j] = median(img_pad[i:i+3, j:j+3], selem=square(3))
break
return img_out
```
其中,`adaptive_median_filter`函数接受两个参数:`img`表示输入的图像,`max_window_size`表示最大的窗口大小。该函数返回经过自适应中值滤波后的图像。
阅读全文