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 字典dict使用介绍
Nov 30 Python
python3序列化与反序列化用法实例
May 26 Python
详解Python的Django框架中Manager方法的使用
Jul 21 Python
Python入门_浅谈逻辑判断与运算符
May 16 Python
Numpy数组的保存与读取方法
Apr 04 Python
python获取代码运行时间的实例代码
Jun 11 Python
python中join()方法介绍
Oct 11 Python
Python补齐字符串长度的实例
Nov 15 Python
python中bs4.BeautifulSoup的基本用法
Jul 27 Python
运行tensorflow python程序,限制对GPU和CPU的占用操作
Feb 06 Python
python获取依赖包和安装依赖包教程
Feb 13 Python
Python pygame实现中国象棋单机版源码
Jun 20 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
比file_get_contents稳定的curl_get_contents分享
2012/01/11 PHP
详解PHP序列化反序列化的方法
2015/10/27 PHP
Windows下php+mysql5.7配置教程
2017/05/16 PHP
可输入的下拉框
2006/06/19 Javascript
javascript 限制输入和粘贴(IE,firefox测试通过)
2008/11/14 Javascript
javascript中的变量是传值还是传址的?
2010/04/19 Javascript
iframe 上下滚动条如何默认在下方实现原理
2012/12/10 Javascript
javascript中RegExp保留小数点后几位数的方法分享
2013/08/13 Javascript
浏览器兼容的JS写法总结
2016/04/27 Javascript
JavaScript 对象详细整理总结
2016/09/29 Javascript
浅谈在js传递参数中含加号(+)的处理方式
2016/10/11 Javascript
ionic进入多级目录后隐藏底部导航栏(tabs)的完美解决方案
2016/11/23 Javascript
javascript事件的传播基础实例讲解(35)
2017/02/14 Javascript
微信小程序基于本地缓存实现点赞功能的方法
2017/12/18 Javascript
vue使用Proxy实现双向绑定的方法示例
2019/03/20 Javascript
vue发送websocket请求和http post请求的实例代码
2019/07/11 Javascript
使用python Django做网页
2013/11/04 Python
举例讲解Python中装饰器的用法
2015/04/27 Python
基于python中的TCP及UDP(详解)
2017/11/06 Python
关于PyTorch 自动求导机制详解
2019/08/18 Python
浅析Python+OpenCV使用摄像头追踪人脸面部血液变化实现脉搏评估
2019/10/17 Python
Django 限制访问频率的思路详解
2019/12/24 Python
pandas中read_csv、rolling、expanding用法详解
2020/04/21 Python
python实现磁盘日志清理的示例
2020/11/05 Python
Python爬虫UA伪装爬取的实例讲解
2021/02/19 Python
CSS3转换功能transform主要属性值分析及实现分享
2012/05/06 HTML / CSS
纯CSS3实现滚动的齿轮动画效果
2014/06/05 HTML / CSS
HTML5边玩边学(3)像素和颜色
2010/09/21 HTML / CSS
中国专业的综合网上购物商城:京东
2016/08/02 全球购物
abstract 可以和 virtual 一起使用吗?可以和 override 一起使用吗?
2012/10/15 面试题
文明宿舍获奖感言
2014/02/07 职场文书
毕业实习证明(4篇)
2014/10/28 职场文书
2016年11月份红领巾广播稿
2015/12/21 职场文书
护理工作心得体会
2016/01/22 职场文书
高三物理教学反思
2016/02/20 职场文书
python基于opencv批量生成验证码的示例
2021/04/28 Python