Python与FPGA——中值滤波

本文介绍了中值滤波的基本原理,如何在Python中使用3步中值法实现,并展示了在FPGA中的具体实现过程,对比了中值滤波与均值滤波在去除椒盐噪声方面的效果。文章探讨了在实际应用中选择Python还是FPGA的因素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

  上一章讲了均值滤波,实现比较容易。今天我们开始中值滤波,难度升级。


一、中值滤波

  中值滤波的原理就是在以下3x3窗口里面选出9个像素的中值,一种方法是先选择使用排序算法,对9个像素排序,然后选出中值。另外一种方法是使用并行3步中值法,我们在Python以及FPGA中都采用3步中值法,方便实现。

在这里插入图片描述

二、增加噪音

  还是采用椒盐增噪。

img = plt.imread("lenna.png")
img = img * 255#图像是[0-1]--->[0-255]
img = img.astype(np.uint8)
prob = 0.92#生成盐噪生的概率为1-0.92=0.08
noise = np.random.choice((0, 255), size=img.shape, p=[prob, 1 - prob])#产生和图像维度一样的噪音矩阵
noise_img = np.where(noise == 0, img, 255).astype(np.uint8)#生成噪音图像,椒(0黑色)盐噪声的盐(255白色)
plt.imsave("noise.png", noise_img)#保存噪音图像,用于FPGA去噪
fig = plt.figure(figsize=(10, 6))
ax = plt.subplot(1, 2, 1)
ax.set_title("raw image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(img)
ax = plt.subplot(1, 2, 2)
ax.set_title("salt image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(noise_img)

在这里插入图片描述

三、Python实现中值滤波

def image_gray(image):
    gray = np.dot(image[:, :, ...], [0.299, 0.587, 0.114])#等同0.299 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 2]
    return gray.astype(np.uint8)
    
def median_filter(image, n):
    h, w = image.shape
    filtered_image = np.zeros((h, w))
    m = int((n - 1) / 2)
    for i in range(m, h - m):
        for j in range(m, w - m):
            kernel = image[i - m: i + m 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值