95. 几何均值滤波器

2025-08-18  本文已影响0人  大龙10

7. 图像复原与重建索引

一、 仅噪声存在的空间滤波图像复原

二、几何均值滤波器(Geometric mean filter)

三、例程

import cv2
import numpy as np
from matplotlib import pyplot as plt

# 9.9: 几何均值滤波器 (Geometric mean filter)
img = cv2.imread(r"E:/OpenCV/Fig0507b.tif", 0)  # flags=0 读取为灰度图像
img_h = img.shape[0]
img_w = img.shape[1]

# 算术平均滤波 (Arithmentic mean filter)
kSize = (3,3)
kernalMean = np.ones(kSize, np.float32) / (kSize[0]*kSize[1])  # 生成归一化盒式核
imgAriMean = cv2.filter2D(img, -1, kernalMean)

# 几何均值滤波器 (Geometric mean filter)
m, n = 3, 3
order = 1/(m*n)
kernalMean = np.ones((m,n), np.float32)  # 生成盒式核

hPad = int((m-1) / 2)
wPad = int((n-1) / 2)
imgPad = np.pad(img.copy(), ((hPad, m-hPad-1), (wPad, n-wPad-1)), mode="edge")

imgGeoMean = img.copy()
for i in range(hPad, img_h + hPad):
    for j in range(wPad, img_w + wPad):
        prod = np.prod(imgPad[i-hPad:i+hPad+1, j-wPad:j+wPad+1]*1.0)
        imgGeoMean[i-hPad][j-wPad] = np.power(prod, order)

plt.figure(figsize=(9, 6))
plt.subplot(131), plt.axis('off'), plt.title("Original")
plt.imshow(img, cmap='gray', vmin=0, vmax=255)
plt.subplot(132), plt.axis('off'), plt.title("Arithmentic mean filter")
plt.imshow(imgAriMean, cmap='gray', vmin=0, vmax=255)
plt.subplot(133), plt.axis('off'), plt.title("Geometric mean filter")
plt.imshow(imgGeoMean, cmap='gray', vmin=0, vmax=255)
plt.tight_layout()
plt.show()

四、资料

youcans_的博客:
https://blog.csdn.net/youcans/article/details/122834842
上一篇 下一篇

猜你喜欢

热点阅读