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
0
投稿
猜你喜欢
- 索引是加速表内容访问的主要手段,特别对涉及多个表的连接的查询更是如此。这是数据库优化中的一个重要内容,我们要了解为什么需要索引,索引如何工作
- 今天我们就用一篇文章,带大家梳理matplotlib、seaborn、plotly、pyecharts的绘图原理,让大家学起来不再那么费劲!
- pandas中的DataFrame中可以根据某个属性的同一值进行聚合分组,可以选单个属性,也可以选多个属性:代码示例:import pand
- 私有变量表示方法在变量前加上两个下划线的是私有变量。class Teacher(): def __init__(self,nam
- 本文实例讲述了php 使用 __call实现重载功能。分享给大家供大家参考,具体如下:<?php/** * Created by Ph
- 目前lnmp一键安装包比较多的问题就是502 Bad Gateway,大部分情况下原因是在安装php前,脚本中某些lib包可能没有安装上,造
- 一、概念介绍箱型图(box-plot),又称为箱线图,盒型图,盒须图。在数据探索阶段或者描述性分析过程中,我们常常用于展示多类连
- 认知心理学家Donald A. Norman将人类的行动分解成七个阶段:1. 确定目标2. 确定意图3. 明确行动内容4. 执行5. 感知外
- strIn 为 输入的Email地址字符串变量 返回为true或falsereturn Regex.IsMatch(strIn, @&quo
- 阅读上一篇教程:WEB2.0网页制作标准教程(7)CSS学习入门 CSS布局与传统表格(table)布局最大的区别在于:原来的定位都是采用表
- 一、爬山法简介爬山法(climbing method)是一种优化算法,其一般从一个随机的解开始,然后逐步找到一个最优解(局部最优)。 假定所
- 原理1.使用python中的mtplotlib库。2.立体爱心面公式点画法(实心)代码import matplotlib.pyplot as
- 这篇文章主要介绍了python Opencv计算图像相似度过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- 你说的就是真正的计数器,它只在有新的用户进入网站时,计数器才会加1,忠实可靠。把下列代码放到的global.asa的sessio
- 学习JQUERY就应该从最基本的学起,基本的就应该是语法了,在这里,我们有必要先温习一下JAVASCRIPT的一些知识。语法就不用说了,都是
- 关于list的insert函数list#insert(ind,value)在ind元素前面插入value首先对ind进行预处理:如果ind&
- 行高的概念看上去很简单——文字行的高度,其实,行高所涉及到的基础知识,对于今后理解其它属性也很重要。大片密密麻麻的文字往往会让人觉得乏味,因
- 原文链接:https://vien.tech/article/157前言本文将介绍ubuntu系统下如何定时执行shell脚本、python
- 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,可是也有它们的不同之处,这段时间刚学完函数与存储过程,来给自己
- 一、分支结构为了限定用户正规操作,也为了更好的控制程序的逻辑,必须在适当时引入条件结构。Python 条件语句是通过一条或多条语句的执行结果