Python实现图像去噪方式(中值去噪和均值去噪)
作者:初见与告别 时间:2023-04-15 15:38:13
实现对图像进行简单的高斯去噪和椒盐去噪。
代码如下:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import random
import scipy.misc
import scipy.signal
import scipy.ndimage
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=10)
def medium_filter(im, x, y, step):
sum_s = []
for k in range(-int(step / 2), int(step / 2) + 1):
for m in range(-int(step / 2), int(step / 2) + 1):
sum_s.append(im[x + k][y + m])
sum_s.sort()
return sum_s[(int(step * step / 2) + 1)]
def mean_filter(im, x, y, step):
sum_s = 0
for k in range(-int(step / 2), int(step / 2) + 1):
for m in range(-int(step / 2), int(step / 2) + 1):
sum_s += im[x + k][y + m] / (step * step)
return sum_s
def convert_2d(r):
n = 3
# 3*3 滤波器, 每个系数都是 1/9
window = np.ones((n, n)) / n ** 2
# 使用滤波器卷积图像
# mode = same 表示输出尺寸等于输入尺寸
# boundary 表示采用对称边界条件处理图像边缘
s = scipy.signal.convolve2d(r, window, mode='same', boundary='symm')
return s.astype(np.uint8)
def convert_3d(r):
s_dsplit = []
for d in range(r.shape[2]):
rr = r[:, :, d]
ss = convert_2d(rr)
s_dsplit.append(ss)
s = np.dstack(s_dsplit)
return s
def add_salt_noise(img):
rows, cols, dims = img.shape
R = np.mat(img[:, :, 0])
G = np.mat(img[:, :, 1])
B = np.mat(img[:, :, 2])
Grey_sp = R * 0.299 + G * 0.587 + B * 0.114
Grey_gs = R * 0.299 + G * 0.587 + B * 0.114
snr = 0.9
noise_num = int((1 - snr) * rows * cols)
for i in range(noise_num):
rand_x = random.randint(0, rows - 1)
rand_y = random.randint(0, cols - 1)
if random.randint(0, 1) == 0:
Grey_sp[rand_x, rand_y] = 0
else:
Grey_sp[rand_x, rand_y] = 255
#给图像加入高斯噪声
Grey_gs = Grey_gs + np.random.normal(0, 48, Grey_gs.shape)
Grey_gs = Grey_gs - np.full(Grey_gs.shape, np.min(Grey_gs))
Grey_gs = Grey_gs * 255 / np.max(Grey_gs)
Grey_gs = Grey_gs.astype(np.uint8)
# 中值滤波
Grey_sp_mf = scipy.ndimage.median_filter(Grey_sp, (7, 7))
Grey_gs_mf = scipy.ndimage.median_filter(Grey_gs, (8, 8))
# 均值滤波
Grey_sp_me = convert_2d(Grey_sp)
Grey_gs_me = convert_2d(Grey_gs)
plt.subplot(321)
plt.title('加入椒盐噪声',fontproperties=font_set)
plt.imshow(Grey_sp, cmap='gray')
plt.subplot(322)
plt.title('加入高斯噪声',fontproperties=font_set)
plt.imshow(Grey_gs, cmap='gray')
plt.subplot(323)
plt.title('中值滤波去椒盐噪声(8*8)',fontproperties=font_set)
plt.imshow(Grey_sp_mf, cmap='gray')
plt.subplot(324)
plt.title('中值滤波去高斯噪声(8*8)',fontproperties=font_set)
plt.imshow(Grey_gs_mf, cmap='gray')
plt.subplot(325)
plt.title('均值滤波去椒盐噪声',fontproperties=font_set)
plt.imshow(Grey_sp_me, cmap='gray')
plt.subplot(326)
plt.title('均值滤波去高斯噪声',fontproperties=font_set)
plt.imshow(Grey_gs_me, cmap='gray')
plt.show()
def main():
img = np.array(Image.open('E:/pycharm/GraduationDesign/Test/testthree.png'))
add_salt_noise(img)
if __name__ == '__main__':
main()
效果如下
来源:https://blog.csdn.net/m0_37108612/article/details/90638237
标签:Python,中值去噪,均值去噪
0
投稿
猜你喜欢
分组字符合并SQL语句 按某字段合并字符串之一(简单合并)
2024-01-24 11:30:20
让复杂导航设计变得简单
2008-01-07 11:50:00
怎样在JavaScript里写一个swing把数据插入数据库
2024-01-20 09:26:42
MySQL Shell的介绍以及安装
2024-01-28 20:03:18
python中的% 是什么意思,起到什么作用呢
2021-12-12 00:20:58
python中的内置函数max()和min()及mas()函数的高级用法
2023-01-21 18:45:43
Git Submodule管理项目子模块的使用
2023-10-06 20:34:57
Python 用__new__方法实现单例的操作
2023-05-22 08:22:41
python中reload(module)的用法示例详解
2021-01-31 10:49:11
vue 图片裁剪上传组件的实现
2024-05-09 15:14:02
python3解析库BeautifulSoup4的安装配置与基本用法
2022-02-06 14:08:04
SQL Server 2008中的新日期数据类型
2009-03-16 15:05:00
python之 matplotlib和pandas绘图教程
2021-07-16 01:50:11
snoopy PHP版的网络客户端提供本地下载
2023-06-28 00:09:55
MySQL备份与恢复之冷备(1)
2024-01-26 13:37:50
Go语言net包RPC远程调用三种方式http与json-rpc及tcp
2024-05-29 22:06:14
javascript实现自动输出文本(打字特效)
2024-02-25 18:25:14
python操作xml文件示例
2022-06-17 22:38:28
浅谈Python 参数与变量
2023-05-19 12:27:25
Python科学计算包numpy用法实例详解
2021-11-12 15:07:20