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发送SMTP邮件的教程
Apr 29 Python
在Python的列表中利用remove()方法删除元素的教程
May 21 Python
浅谈Python由__dict__和dir()引发的一些思考
Oct 30 Python
Python数据分析之双色球基于线性回归算法预测下期中奖结果示例
Feb 08 Python
Python实现的查询mysql数据库并通过邮件发送信息功能
May 17 Python
python实现关闭第三方窗口的方法
Jun 28 Python
python批量将excel内容进行翻译写入功能
Oct 10 Python
python实现布隆过滤器及原理解析
Dec 08 Python
django框架中ajax的使用及避开CSRF 验证的方式详解
Dec 11 Python
python求前n个阶乘的和实例
Apr 02 Python
浅谈keras使用预训练模型vgg16分类,损失和准确度不变
Jul 02 Python
python计算列表元素与乘积详情
Aug 05 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
颠覆常识!无色透明的咖啡诞生了(中日双语)
2021/03/03 咖啡文化
不用mod_rewrite直接用php实现伪静态化页面代码
2008/10/04 PHP
ThinkPHP让分页保持搜索状态的方法
2014/07/02 PHP
PhpStorm2020 + phpstudyV8 +XDebug的教程详解
2020/09/17 PHP
jQuery 剧场版 你必须知道的javascript
2009/05/27 Javascript
JS中如何设置readOnly的值
2013/12/25 Javascript
浅谈javascript的Array.prototype.slice.call
2015/08/31 Javascript
jQuery实现的简单百分比进度条效果示例
2016/08/01 Javascript
javascript验证手机号和实现星号(*)代替实例
2016/08/16 Javascript
jQuery EasyUI Draggable拖动组件
2017/03/01 Javascript
Bootstrap Table快速完美搭建后台管理系统
2017/09/20 Javascript
解读vue生成的文件目录结构及说明
2017/11/27 Javascript
详解Vue iview IE浏览器不兼容报错(Iview Bable polyfill)
2019/01/07 Javascript
微信小程序 swiper 组件遇到的问题及解决方法
2019/05/26 Javascript
JavaScript命名空间模式实例详解
2019/06/20 Javascript
详解微信小程序支付流程与梳理
2019/07/16 Javascript
微信小程序自定义tabBar在uni-app的适配详解
2019/09/30 Javascript
JS实现星星海特效
2019/12/24 Javascript
vue+element实现图片上传及裁剪功能
2020/06/29 Javascript
调试Python程序代码的几种方法总结
2015/04/28 Python
python实现简易通讯录修改版
2018/03/13 Python
Python实现的基于优先等级分配糖果问题算法示例
2018/04/25 Python
python3.9实现pyinstaller打包python文件成exe
2020/12/13 Python
Python基于argparse与ConfigParser库进行入参解析与ini parser
2021/02/02 Python
Python爬虫爬取ts碎片视频+验证码登录功能
2021/02/22 Python
简单掌握CSS3将文字描边及填充文字颜色的方法
2016/03/07 HTML / CSS
HTML5声音录制/播放功能的实现代码
2018/05/03 HTML / CSS
泰坦健身器材:Titan Fitness
2018/02/13 全球购物
马来西亚网上花店:FlowerAdvisor马来西亚
2020/01/03 全球购物
捷克购买家具网站:JENA nábytek
2020/03/19 全球购物
英国运动风奢侈品购物网站:Maison De Fashion
2020/08/28 全球购物
Loreto Gallo英国:欧洲领先的在线药房
2021/01/21 全球购物
应届生高等护理求职信
2013/10/12 职场文书
带薪年假请假条
2014/02/04 职场文书
行政文员实习自我鉴定范文
2014/09/14 职场文书
董存瑞观后感
2015/06/11 职场文书