自适应中值滤波有代码吗
时间: 2024-05-13 22:18:39 浏览: 85
当然,以下是自适应中值滤波的 Python 代码示例:
```python
import cv2
import numpy as np
def adaptive_median_filter(img, max_window_size):
"""自适应中值滤波"""
height, width = img.shape
result = np.zeros((height, width), dtype=np.uint8)
img_pad = cv2.copyMakeBorder(img, 1, 1, 1, 1, cv2.BORDER_DEFAULT)
for i in range(1, height + 1):
for j in range(1, width + 1):
window_size = 3
while window_size <= max_window_size:
window = img_pad[i - 1:i + 2, j - 1:j + 2]
z_min, z_max, z_med = np.min(window), np.max(window), np.median(window)
if z_min < z_med < z_max:
if z_min < img_pad[i, j] < z_max:
result[i - 1, j - 1] = img_pad[i, j]
else:
result[i - 1, j - 1] = z_med
break
else:
window_size += 2
if window_size > max_window_size:
result[i - 1, j - 1] = z_med
break
return result
```
其中,`img` 为待处理的图像,`max_window_size` 为最大窗口尺寸,可以根据具体情况调整。函数返回处理后的图像。
阅读全文