完成高斯滤波、反谐波均值滤波、自适应中值滤波
This commit is contained in:
commit
9acae91953
41
adaptive_median_filter.py
Normal file
41
adaptive_median_filter.py
Normal file
@ -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
|
22
contraharmonic_mean_filter.py
Normal file
22
contraharmonic_mean_filter.py
Normal file
@ -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)
|
BIN
file/circuitboard-gaussian.tif
Normal file
BIN
file/circuitboard-gaussian.tif
Normal file
Binary file not shown.
BIN
file/circuitboard-pepper.tif
Normal file
BIN
file/circuitboard-pepper.tif
Normal file
Binary file not shown.
BIN
file/circuitboard-salt.tif
Normal file
BIN
file/circuitboard-salt.tif
Normal file
Binary file not shown.
BIN
file/circuitboard-saltandpep.tif
Normal file
BIN
file/circuitboard-saltandpep.tif
Normal file
Binary file not shown.
25
gaussian.py
Normal file
25
gaussian.py
Normal file
@ -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()
|
24
pepper_and_salt.py
Normal file
24
pepper_and_salt.py
Normal file
@ -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()
|
42
pepper_pep.py
Normal file
42
pepper_pep.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user