Python实现图像去噪方式(中值去噪和均值去噪)
作者:初见与告别 发布时间:2023-04-15 15:38:13
标签:Python,中值去噪,均值去噪
实现对图像进行简单的高斯去噪和椒盐去噪。
代码如下:
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


猜你喜欢
- 学一些比较知名的模型对身体有好处噢!什么是VGG16模型VGG是由Simonyan 和Zisserman在文献《Very Deep Conv
- 00 小编的问题小编向我们反馈,从微信里复制出来的图片,会被微信屏蔽,无法显示我们后天采用的是百度编辑器,而且已经做了远程图片本地化,于是检
- 前言在php开发过程中,获取文件扩展名是非常常见的需求。比如我们在上传文件的时候,首先需要判断文件类型是否为我们允许上传的类型。这个时候就需
- file--->setting,选择Editor--->python如下图所示:补充: Pycharm配置显示空格在P
- // 获取地址栏的参数数组function getUrlParams() { var search =
- 一、需求说明:数据库的备份,对于生产环境来说尤为重要,数据库的备份分为物理备份和逻辑备份。物理备份:使用相关的复制命令直接将数据库的数据目录
- Insert 和 Update假设现在你要把下面的数据插入到数据库中.ID = 3TheDate=mktime(0,0,0,8,31,200
- 一、题目内容给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的
- 1.准备工作1.在文件里找到设置2.在项目里找到python解释器,点击右边的加号3.搜素pygame并安装同理下载pgzero安装包2.开
- 今天是我们js入门系列演示的最后一节了(暂时我是这样计划的),但是请朋友们记住,前面的实例你都很熟悉了的话也不代表我们就懂了JS,甚至连入门
- 1、现象系统提示找不到指定的文件:Error running 'hello': Cannot run program &qu
- 本文讲解如何用java实现把数据库的数据写入到txt中 并实现类似下载软件的样子在网页中弹出下载.package datatest;impo
- python要知道怎么用好编译器。当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件。要运行代码
- 第一种方法import pandas as pdfrom collections import Counterdata = '参赛信
- 在开发T-SQL时,经常会需要对字符串进行各种各样的操作,下面介绍常用的字符串函数。一、编码转换1、获取字符的ASCII码:asciiASC
- 在画一些曲线图(linecharts)时,常常会出现多条曲线同时画在一张图上面,这时候就需要对不同的曲线进行不同的标注,以使读者能够清晰地知
- 路由跳转了但界面不显示没有在父路由加上router-view,加上下面的代码即可。<!-- 路由匹配到的组件将显示在这里 -->
- 在为一个客户排除死锁问题时我遇到了一个有趣的包括InnoDB间隙锁的情形。对于一个WHERE子句不匹配任何行的非插入的写操作中,
- 本文实例讲述了python中as用法。分享给大家供大家参考。具体分析如下:import some # some 为一个模组如果想要改变被导入
- 我们今天主要描述的是php和MySQL转义字符,我们大家都知道php和MySQL转义字符的实际应用比例还是占为多数的,如果你对这一技术,心存