图像处理L1

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BabiMonkey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值