给出自适应均值滤波去除批量声呐图像噪声的python代码
时间: 2023-10-27 07:04:44 浏览: 93
当然可以!以下是一个使用自适应均值滤波器去除批量声呐图像噪声的Python代码示例:
```python
import cv2
import numpy as np
def adaptive_mean_filter(image, window_size, threshold):
filtered_image = np.zeros_like(image)
pad_size = window_size // 2
padded_image = cv2.copyMakeBorder(image, pad_size, pad_size, pad_size, pad_size, cv2.BORDER_REFLECT)
for i in range(pad_size, padded_image.shape[0] - pad_size):
for j in range(pad_size, padded_image.shape[1] - pad_size):
window = padded_image[i - pad_size:i + pad_size + 1, j - pad_size:j + pad_size + 1]
median = np.median(window)
deviation = np.median(np.abs(window - median))
if deviation > threshold:
filtered_image[i - pad_size, j - pad_size] = median
else:
filtered_image[i - pad_size, j - pad_size] = padded_image[i, j]
return filtered_image
# 读取图像
image = cv2.imread("noisy_image.png", 0)
# 设置窗口大小和阈值
window_size = 3
threshold = 10
# 应用自适应均值滤波器
filtered_image = adaptive_mean_filter(image, window_size, threshold)
# 显示结果
cv2.imshow("Original Image", image)
cv2.imshow("Filtered Image", filtered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请注意,此代码需要使用OpenCV库进行图像处理。要运行此代码,请确保已安装OpenCV库,并将待处理的图像文件名替换为`noisy_image.png`。您还可以根据需要调整窗口大小和阈值参数。
阅读全文