from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# 读取图像并转换为灰度图像
image = Image.open("/Users/a123/Downloads/circle.jpeg").convert('L')
p_mat = np.array(image) # 将图像转换为像素矩阵
# 生成高斯噪声
mean = 0 # 噪声的均值
stddev = 100 # 噪声的标准差
noise = np.random.normal(mean, stddev, p_mat.shape) # 生成噪声
noisy_p_mat = p_mat - noise # 将噪声应用到像素矩阵中
noisy_p_mat = np.clip(noisy_p_mat, 0, 255).astype(np.uint8) # 限制像素值在0-255之间
height, width = noisy_p_mat.shape # 获取图像的高度和宽度
# 定义滑动窗口的大小
window_shape = 20
# 方形区域平滑函数
def square_smoothing(matrix, window_shape):
processed_mat = matrix.copy() # 创建副本用于处理
for i in range(height):
for j in range(width):
if i - window_shape < 0 or j - window_shape < 0 or i + window_shape + 1 >= height or j + window_shape + 1 >= width:
processed_mat[i, j] = 0 # 边界外像素设为0
else:
# 获取当前像素周围的41x41方形区域
neighbourhood = matrix[i - window_shape:i + window_shape + 1, j - window_shape:j + window_shape + 1]
# 计算该区域的平均值
av = np.mean(neighbourhood)
processed_mat[i, j] = np.int8(av) # 将当前像素设置为平均值
return processed_mat
# 水平和垂直方向平滑函数
def hv_smoothing(matrix, window_shape):
processed_mat = matrix.copy() # 创建副本用于处理
for i in range(height):
for j in range(width):
if i - window_shape < 0 or j - window_shape < 0 or i + window_shape + 1 >= height or j + window_shape + 1 >= width:
processed_mat[i, j] = 0 # 边界外像素设为0
else:
neighbourhood = []
# 仅选择水平和垂直方向上的像素,排除最靠近的4个像素
for k in range(4, window_shape):
neighbourhood.append(matrix[i, j+k]) # 右边的像素
neighbourhood.append(matrix[i + k, j]) # 下方的像素
neighbourhood.append(matrix[i - k, j]) # 上方的像素
neighbourhood.append(matrix[i, j - k]) # 左边的像素
# 计算这些像素的平均值
av = np.mean(neighbourhood)
processed_mat[i, j] = np.int8(av) # 将当前像素设置为平均值
return processed_mat
# 对角线方向平滑函数
def diagonal_smoothing(matrix, window_shape):
processed_mat = matrix.copy() # 创建副本用于处理
for i in range(height):
for j in range(width):
if i - window_shape < 0 or j - window_shape < 0 or i + window_shape + 1 >= height or j + window_shape + 1 >= width:
processed_mat[i, j] = 0 # 边界外像素设为0
else:
# 获取当前像素周围的41x41方形区域
neighbourhood = matrix[i - window_shape:i + window_shape + 1, j - window_shape:j + window_shape + 1]
# 选取主对角线和反对角线的像素,并排除靠近中心的4个像素
main_diag = np.diagonal(neighbourhood)
anti_diag = np.diagonal(np.fliplr(neighbourhood))
av = np.mean(np.concatenate((main_diag[4:], anti_diag[4:]))) # 计算平均值
processed_mat[i, j] = np.int8(av) # 将当前像素设置为平均值
return processed_mat
# 分别应用三种平滑处理
square_mat = square_smoothing(noisy_p_mat, window_shape)
hv_mat = hv_smoothing(noisy_p_mat, window_shape)
diag_mat = diagonal_smoothing(noisy_p_mat, window_shape)
# 绘制结果
plt.figure(figsize=(15, 5))
# 绘制原始图像
plt.subplot(1, 5, 1)
plt.imshow(p_mat, cmap='gray')
plt.title(' ORIGIN')
plt.axis('off')
# 绘制带噪声的图像
plt.subplot(1, 5, 2)
plt.imshow(noisy_p_mat, cmap='gray')
plt.title('NOISY')
plt.axis('off')
# 绘制方形区域平滑处理后的图像
plt.subplot(1, 5, 3)
plt.imshow(square_mat, cmap='gray')
plt.title(' SQURE')
plt.axis('off')
# 绘制水平和垂直方向平滑处理后的图像
plt.subplot(1, 5, 4)
plt.imshow(hv_mat, cmap='gray')
plt.title('+')
plt.axis('off')
# 绘制对角线方向平滑处理后的图像
plt.subplot(1, 5, 5)
plt.imshow(diag_mat, cmap='gray')
plt.title('')
plt.axis('off')
# 显示所有图像
plt.tight_layout()
plt.show()
11-28
2874
04-20
981
04-19
7346