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

 更新时间:2019年12月18日 09:31:29   作者:初见与告别  
今天小编就为大家分享一篇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()

效果如下

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

相关文章

  • Python接口测试数据库封装实现原理

    Python接口测试数据库封装实现原理

    这篇文章主要介绍了Python接口测试数据库封装实现原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • python输入、数据类型转换及运算符方式

    python输入、数据类型转换及运算符方式

    这篇文章主要介绍了python输入、数据类型转换及运算符方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Python word2vec训练词向量实例分析讲解

    Python word2vec训练词向量实例分析讲解

    这篇文章主要介绍了Python word2vec训练词向量实例分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-12-12
  • Anaconda彻底删除虚拟环境的正确方法

    Anaconda彻底删除虚拟环境的正确方法

    这篇文章主要给大家介绍了关于Anaconda彻底删除虚拟环境的正确方法,要在Anaconda中删除一个虚拟环境,可以按照本文以下步骤进行操作,需要的朋友可以参考下
    2023-10-10
  • Python多线程中比time.sleep更好用的暂停方式

    Python多线程中比time.sleep更好用的暂停方式

    这篇文章主要介绍了Python多线程中比time.sleep更好用的暂停方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • python爬虫爬取某站上海租房图片

    python爬虫爬取某站上海租房图片

    这篇文章主要介绍了学习python爬虫并爬取某站上海租房图片的原理介绍以及相关代码分享,对此有兴趣的朋友学习下。
    2018-02-02
  • Python将多个图像合并输出的实现方法

    Python将多个图像合并输出的实现方法

    这篇文章主要介绍了Python将多个图像合并输出的实现方法,本文介绍了两种将多个图像合并为一个输出的方法:使用PIL库或使用OpenCV和NumPy,这些库都可以使用Python中的简单语法和少量的代码来完成此任务,需要的朋友可以参考下
    2023-06-06
  • pycharm 中mark directory as exclude的用法详解

    pycharm 中mark directory as exclude的用法详解

    今天小编就为大家分享一篇pycharm 中mark directory as exclude的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • 浅谈在django中使用redirect重定向数据传输的问题

    浅谈在django中使用redirect重定向数据传输的问题

    这篇文章主要介绍了浅谈在django中使用redirect重定向数据传输的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 使用Flask和Django中解决跨域请求问题

    使用Flask和Django中解决跨域请求问题

    这篇文章主要介绍了使用Flask和Django中解决跨域请求问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04

最新评论