Python实现图像去噪方式(中值去噪和均值去噪)


Posted in Python onDecember 18, 2019

实现对图像进行简单的高斯去噪和椒盐去噪。

代码如下:

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

效果如下

Python实现图像去噪方式(中值去噪和均值去噪)

以上这篇Python实现图像去噪方式(中值去噪和均值去噪)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现去除列表中重复元素的方法总结【7种方法】
Feb 16 Python
华为校园招聘上机笔试题 扑克牌大小(python)
Apr 22 Python
pandas 如何分割字符的实现方法
Jul 29 Python
Python3 chardet模块查看编码格式的例子
Aug 14 Python
python 叠加等边三角形的绘制的实现
Aug 14 Python
Python面向对象之私有属性和私有方法应用案例分析
Dec 31 Python
TensorFlow 读取CSV数据的实例
Feb 05 Python
Pytorch中.new()的作用详解
Feb 18 Python
python给视频添加背景音乐并改变音量的具体方法
Jul 19 Python
Python中的流程控制详解
Feb 18 Python
Python爬虫+tkinter界面实现历史天气查询的思路详解
Feb 22 Python
python 机器学习的标准化、归一化、正则化、离散化和白化
Apr 16 Python
python 中值滤波,椒盐去噪,图片增强实例
Dec 18 #Python
Django中使用MySQL5.5的教程
Dec 18 #Python
Python hashlib加密模块常用方法解析
Dec 18 #Python
Python实现中值滤波去噪方式
Dec 18 #Python
详解Python中字符串前“b”,“r”,“u”,“f”的作用
Dec 18 #Python
Python字典底层实现原理详解
Dec 18 #Python
Python利用PyExecJS库执行JS函数的案例分析
Dec 18 #Python
You might like
PHP curl 获取响应的状态码的方法
2014/01/13 PHP
PHP Cookie学习笔记
2016/08/23 PHP
phpStudy 2016 使用教程详解(支持PHP7)
2017/10/18 PHP
JS操作XML中DTD介绍及使用方法分析
2019/07/04 PHP
jquery validator 插件增加日期比较方法
2010/02/21 Javascript
JavaScript中为元素加上name属性的方法
2011/05/09 Javascript
js 判断脚本加载完毕的代码
2011/07/13 Javascript
jQuery 翻牌或百叶窗效果(内容三秒自动切换)
2012/06/14 Javascript
自定义右键属性覆盖浏览器默认右键行为实现代码
2013/02/02 Javascript
jQuery的图片滑块焦点图插件整理推荐
2014/12/07 Javascript
创建、调用JavaScript对象的方法集锦
2014/12/24 Javascript
Vue.JS入门教程之列表渲染
2016/12/01 Javascript
canvas滤镜效果实现代码
2017/02/06 Javascript
BootStrap表单控件之文本域textarea
2017/05/23 Javascript
自定义vue全局组件use使用、vuex的使用详解
2017/06/14 Javascript
JavaScript简单实现动态改变HTML内容的方法示例
2018/12/25 Javascript
vue+egg+jwt实现登录验证的示例代码
2019/05/18 Javascript
python中__call__方法示例分析
2014/10/11 Python
python通过pip更新所有已安装的包实现方法
2017/05/19 Python
Python序列类型的打包和解包实例
2019/12/21 Python
python爬虫开发之Request模块从安装到详细使用方法与实例全解
2020/03/09 Python
新手学习Python2和Python3中print不同的用法
2020/06/09 Python
python安装后的目录在哪里
2020/06/21 Python
英国、欧洲和全球租车服务:Avis英国
2016/08/29 全球购物
BASIC HOUSE官方旗舰店:韩国著名的服装品牌
2018/09/27 全球购物
Europcar比利时:租车
2019/08/26 全球购物
澳大利亚领先的女性运动服品牌:Lorna Jane
2020/06/19 全球购物
为什么会有内存对齐
2016/10/10 面试题
如何开发一个JQuery插件
2016/07/28 面试题
销售会计工作职责
2013/12/02 职场文书
授权委托书公证
2014/09/14 职场文书
小学优秀教师先进事迹材料
2014/12/16 职场文书
单位政审意见范文
2015/06/04 职场文书
2015国庆节66周年标语
2015/07/30 职场文书
人民币使用说明书
2019/04/17 职场文书
Golang解析JSON对象
2022/04/30 Golang