commit 9acae9195324ee6a0c176c27c16078d331dd5001 Author: Cx330 <1487537121@qq.com> Date: Sat Apr 12 21:33:11 2025 +0800 完成高斯滤波、反谐波均值滤波、自适应中值滤波 diff --git a/adaptive_median_filter.py b/adaptive_median_filter.py new file mode 100644 index 0000000..117b555 --- /dev/null +++ b/adaptive_median_filter.py @@ -0,0 +1,41 @@ +import numpy as np + +# 自适应中值滤波 +def adaptive_median_filter(image, max_window_size=7): + padded_image = np.pad(image, max_window_size // 2, mode='edge') + filtered_image = image.copy() + height, width = image.shape + + for i in range(height): + for j in range(width): + window_size = 3 + while True: + half = window_size // 2 + i1, i2 = i + max_window_size//2 - half, i + max_window_size//2 + half + 1 + j1, j2 = j + max_window_size//2 - half, j + max_window_size//2 + half + 1 + window = padded_image[i1:i2, j1:j2].flatten() + window = np.sort(window) + + Z_min = window[0] + Z_max = window[-1] + Z_med = window[len(window) // 2] + Z_xy = padded_image[i + max_window_size//2, j + max_window_size//2] + + A1 = Z_med - Z_min + A2 = Z_med - Z_max + + if A1 > 0 and A2 < 0: # Level A + B1 = Z_xy - Z_min + B2 = Z_xy - Z_max + if B1 > 0 and B2 < 0: + filtered_image[i, j] = Z_xy + else: + filtered_image[i, j] = Z_med + break + else: + window_size += 2 + if window_size > max_window_size: + filtered_image[i, j] = Z_med + break + + return filtered_image \ No newline at end of file diff --git a/contraharmonic_mean_filter.py b/contraharmonic_mean_filter.py new file mode 100644 index 0000000..6841d02 --- /dev/null +++ b/contraharmonic_mean_filter.py @@ -0,0 +1,22 @@ +import numpy as np + +#反谐波均值滤波 +def contraharmonic_mean_filter(img, kernel_size=3, Q=1.5): + img = img.astype(np.float64) + filtered_img = np.zeros_like(img) + + k = kernel_size // 2 + height, width = img.shape + + for i in range(k, height - k): + for j in range(k, width - k): + window = img[i - k:i + k + 1, j - k:j + k + 1] + numerator = np.power(window, Q + 1).sum() + denominator = np.power(window, Q).sum() + + if denominator != 0: + filtered_img[i, j] = numerator / denominator + else: + filtered_img[i, j] = img[i, j] + + return np.clip(filtered_img, 0, 255).astype(np.uint8) \ No newline at end of file diff --git a/file/circuitboard-gaussian.tif b/file/circuitboard-gaussian.tif new file mode 100644 index 0000000..6817983 Binary files /dev/null and b/file/circuitboard-gaussian.tif differ diff --git a/file/circuitboard-pepper.tif b/file/circuitboard-pepper.tif new file mode 100644 index 0000000..b43fac5 Binary files /dev/null and b/file/circuitboard-pepper.tif differ diff --git a/file/circuitboard-salt.tif b/file/circuitboard-salt.tif new file mode 100644 index 0000000..debb594 Binary files /dev/null and b/file/circuitboard-salt.tif differ diff --git a/file/circuitboard-saltandpep.tif b/file/circuitboard-saltandpep.tif new file mode 100644 index 0000000..8eb8b97 Binary files /dev/null and b/file/circuitboard-saltandpep.tif differ diff --git a/gaussian.py b/gaussian.py new file mode 100644 index 0000000..8505a07 --- /dev/null +++ b/gaussian.py @@ -0,0 +1,25 @@ +import cv2 +import matplotlib.pyplot as plt +plt.rcParams['font.sans-serif'] = ['simHei'] +plt.rcParams['axes.unicode_minus'] = False + +#读取照片 +image_gaussian = cv2.imread("file/circuitboard-gaussian.tif") + +#高斯滤波 +kernel_size_gaussian = (5,5) #核的大小 +sigmax = 0 #标准差 +image_gaussian2 = cv2.GaussianBlur(image_gaussian,kernel_size_gaussian,sigmax) + +plt.subplot(1,2,1) +plt.imshow(image_gaussian, cmap='gray') +plt.title('1.高斯噪声') +plt.axis('off') + +plt.subplot(1,2,2) +plt.imshow(image_gaussian2, cmap='gray') +plt.title('1.高斯滤波_高斯噪声') +plt.axis('off') + +plt.tight_layout() +plt.show() \ No newline at end of file diff --git a/pepper_and_salt.py b/pepper_and_salt.py new file mode 100644 index 0000000..293f0fe --- /dev/null +++ b/pepper_and_salt.py @@ -0,0 +1,24 @@ +import cv2 +import matplotlib.pyplot as plt +plt.rcParams['font.sans-serif'] = ['simHei'] +plt.rcParams['axes.unicode_minus'] = False +from adaptive_median_filter import adaptive_median_filter + +#读取照片 +image_saltandpep = cv2.imread("file/circuitboard-saltandpep.tif", cv2.IMREAD_GRAYSCALE) + +# 自适应中值滤波 +image_saltandpep2 = adaptive_median_filter(image_saltandpep) + +plt.subplot(1,2,1) +plt.imshow(image_saltandpep, cmap='gray') +plt.title('3.胡椒噪声和盐噪声') +plt.axis('off') + +plt.subplot(1,2,2) +plt.imshow(image_saltandpep2, cmap='gray') +plt.title('3.自适应中值滤波_胡椒噪声和盐噪声') +plt.axis('off') + +plt.tight_layout() +plt.show() \ No newline at end of file diff --git a/pepper_pep.py b/pepper_pep.py new file mode 100644 index 0000000..0a9c435 --- /dev/null +++ b/pepper_pep.py @@ -0,0 +1,42 @@ +import cv2 +import matplotlib.pyplot as plt +plt.rcParams['font.sans-serif'] = ['simHei'] +plt.rcParams['axes.unicode_minus'] = False +from contraharmonic_mean_filter import contraharmonic_mean_filter + +#读取照片 +image_pepper = cv2.imread("file/circuitboard-pepper.tif", cv2.IMREAD_GRAYSCALE) +image_salt = cv2.imread("file/circuitboard-salt.tif", cv2.IMREAD_GRAYSCALE) + +#反谐波均值滤波_胡椒噪声 +kernel_size_pepper = 3 #核的大小 +Q_pepper = 1.5 #滤波器阶数 +image_pepper2 = contraharmonic_mean_filter(image_pepper,kernel_size_pepper,Q_pepper) + +#反谐波均值滤波_盐噪声 +kernel_size_salt = 3 #核的大小 +Q_salt = -1.5 #滤波器阶数 +image_salt2 = contraharmonic_mean_filter(image_salt,kernel_size_salt,Q_salt) + +plt.subplot(2,2,1) +plt.imshow(image_pepper, cmap='gray') +plt.title('2.胡椒噪声') +plt.axis('off') + +plt.subplot(2,2,2) +plt.imshow(image_pepper2, cmap='gray') +plt.title('2.反谐波均值滤波_胡椒噪声') +plt.axis('off') + +plt.subplot(2,2,3) +plt.imshow(image_salt, cmap='gray') +plt.title('2.盐噪声') +plt.axis('off') + +plt.subplot(2,2,4) +plt.imshow(image_salt2, cmap='gray') +plt.title('2.反谐波均值滤波_盐噪声') +plt.axis('off') + +plt.tight_layout() +plt.show() \ No newline at end of file