• python实现图像添加噪声、噪声处理、滤波器代码实现


    目录

    加载图像添加噪声

    图像傅里叶变换和反变换并可视化

    图像处理---高通滤波、低通滤波、带通滤波

    低通滤波器---Butterworth低通滤波器、理想低通滤波器、高斯低通滤波器


    加载图像添加噪声

    • 高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声
    • 椒盐噪声也称为脉冲噪声,是图像中经常见到的一种噪声,它是一种随机出现的白点或者黑点,可能是亮的区域有黑色像素或是在暗的区域有白色像素(或是两者皆有)
    • 乘性噪声一般由信道不理想引起,它们与信号的关系是相乘,信号在它在,信号不在他也就不在
    1. import numpy as np
    2. import cv2
    3. import matplotlib.pyplot as plt
    4. import skimage
    5. from skimage import io
    6. import random
    7. #img = cv2.imread('E:/python/CSDN/image/1.bmp',cv2.IMREAD_GRAYSCALE)
    8. img = plt.imread('E:/python/CSDN/image/5.bmp')
    9. fig=plt.figure(figsize=(10, 50))
    10. #显示原图
    11. plt.subplot(12,3,1),plt.imshow(img,'gray'),plt.title('original')
    12. def addGaussNoise(origin,var=0.0005):#添加高斯噪声函数
    13. #var = random.uniform(0.0001, 0.04)
    14. noisy = skimage.util.random_noise(origin, mode='gaussian', var=var)
    15. return noisy
    16. def addSaltNoise(origin,var=0.01):#添加椒盐噪声函数
    17. #var = random.uniform(0.01, 0.2)
    18. noisy = skimage.util.random_noise(origin, mode='s&p', amount=var)
    19. return noisy
    20. def addSpeckleNoise(origin,var=0.001):#添加乘法噪声函数
    21. #var = random.uniform(0.0001, 0.04)
    22. noisy = skimage.util.random_noise(origin, mode='speckle', var=var)
    23. return noisy
    24. img = addGaussNoise(img)
    25. #img = addSaltNoise(img)
    26. #img = addSpeckleNoise(img)
    27. #显示添加高斯噪声的原图
    28. plt.subplot(12,3,4),plt.imshow(img,'gray'),plt.title('img_add_gaussin')

    图像傅里叶变换和反变换并可视化

    1. #进行傅立叶变换,并显示结果
    2. fft2 = np.fft.fft2(img)
    3. plt.subplot(12,3,2),plt.imshow(np.abs(fft2),'gray'),plt.title('fft2')
    4. #将图像变换的原点移动到频域矩形的中心,并显示效果
    5. shift2center = np.fft.fftshift(fft2)
    6. plt.subplot(12,3,3),plt.imshow(np.abs(shift2center),'gray'),plt.title('shift2center')
    7. #保存图片
    8. #cv2.imwrite("E:/python/CSDN/image/fft.jpg",np.abs(fft2))
    9. #cv2.imwrite("E:/python/CSDN/image/fftshift.jpg",np.abs(shift2center))
    10. #由于傅里叶系数的动态范围过大,无法在屏幕上显示,为了便于观察,利用对数变换将这些较大的系数值变小。经过对数变换之后,较高的数值会变成白点,而较小的数值变为黑点。为了将灰度值可视化。
    11. #对傅立叶变换的结果进行对数变换,并显示效果
    12. log_fft2 = np.log(1 + np.abs(fft2))
    13. plt.subplot(12,3,5),plt.imshow(log_fft2,'gray'),plt.title('log_fft2')
    14. #对中心化后的结果进行对数变换,并显示结果
    15. log_shift2center = np.log(1 + np.abs(shift2center))
    16. plt.subplot(12,3,6),plt.imshow(log_shift2center,'gray'),plt.title('log_shift2center')
    17. #原图反变换
    18. f_image = np.fft.ifftshift(shift2center)
    19. image_new = np.fft.ifft2(f_image) # 反变换的结果是复数
    20. image_new = np.abs(image_new)

    图像处理---高通滤波、低通滤波、带通滤波

    •     高通滤波:高频信息通过,低频信息被阻挡;
    •     低通滤波:低频信息通过,高频信息被阻挡;
    •     带通滤波:介于低频和高频之间的一带信息通过,其它信息被阻挡;
    1. #区域越小越接近原图
    2. up_high = 10
    3. left_high = 15
    4. # 掩膜-中心为0-高通滤波
    5. rows,cols = img.shape
    6. print("high pass{},{}".format(rows,cols))
    7. mask0 = np.ones(img.shape)
    8. mask0[int(rows/2-up_high):int(rows/2+up_high), int(cols/2-left_high):int(cols/2+left_high)] = 0
    9. fshift_mask0 = shift2center*mask0
    10. #plt.subplot(12,3,8),plt.imshow(np.abs(fshift_mask0),'gray'),plt.title('fshift_mask0')
    11. # 二维傅里叶反变换
    12. f_image_mask0 = np.fft.ifftshift(fshift_mask0)
    13. image_new0 = np.fft.ifft2(f_image_mask0) # 反变换的结果是复数
    14. image_new0 = np.abs(image_new0)
    15. plt.subplot(12,3,8),plt.imshow(np.abs(image_new0),'gray'),plt.title('highPassFilter')
    16. #保存图片
    17. #cv2.imwrite("E:/python/CSDN/image/fshift_mask_high.jpg",np.abs(fshift_mask0))
    18. #cv2.imwrite("E:/python/CSDN/image/image_new_high.jpg",np.abs(image_new0))
    19. #区域越大越接近原图
    20. up_low = 50
    21. left_low = 15
    22. # 掩膜-中心为1-低通滤波
    23. rows,cols = img.shape
    24. print("low pass {},{}".format(rows,cols))
    25. mask1 = np.zeros(img.shape)
    26. mask1[int(rows/2-up_low):int(rows/2+up_low), int(cols/2-left_low):int(cols/2+left_low)] = 1
    27. fshift_mask1 = shift2center*mask1
    28. #plt.subplot(12,3,10),plt.imshow(np.abs(fshift_mask1),'gray'),plt.title('fshift_mask1')
    29. # 二维傅里叶反变换
    30. f_image_mask1 = np.fft.ifftshift(fshift_mask1)
    31. image_new1 = np.fft.ifft2(f_image_mask1) # 反变换的结果是复数
    32. image_new1 = np.abs(image_new1)
    33. plt.subplot(12,3,9),plt.imshow(np.abs(image_new1),'gray'),plt.title('lowPassFilter')
    34. # 带通滤波
    35. w = 50 #带宽
    36. radius = 20 #带中心到频率平面原点的距离
    37. rows,cols = img.shape
    38. print("low pass {},{}".format(rows,cols))
    39. mask1 = np.ones(img.shape)
    40. for i in range(0, rows):
    41. for j in range(0, cols):
    42. # 计算(i, j)到中心点的距离
    43. from math import sqrt
    44. d = sqrt(pow(i - rows, 2) + pow(j - cols, 2))
    45. if radius - w / 2 < d < radius + w / 2:
    46. mask1[i, j] = 0
    47. else:
    48. mask1[i, j] = 1
    49. fshift_mask1 = shift2center*mask1
    50. #plt.subplot(12,3,10),plt.imshow(np.abs(fshift_mask1),'gray'),plt.title('fshift_mask1')
    51. # 二维傅里叶反变换
    52. f_image_mask1 = np.fft.ifftshift(fshift_mask1)
    53. image_new1 = np.fft.ifft2(f_image_mask1) # 反变换的结果是复数
    54. image_new1 = np.abs(image_new1)
    55. plt.subplot(12,3,7),plt.imshow(np.abs(image_new1),'gray'),plt.title('bandPassFilter')

    低通滤波器---Butterworth低通滤波器、理想低通滤波器、高斯低通滤波器

    低通滤波器的功能是让低频率通过而滤掉或衰减高频,其作用是过滤掉包含在高频中的噪声。即低通滤波的效果是图像去噪声平滑增强,但同时也抑制了图像的边界即过滤掉图像细节,造成图像不同程序上的模糊。

    • 理想低通滤波器的滤波非常尖锐
    • 高斯低通滤波器的滤波则非常平滑
    • 巴特沃斯滤波器介于两者之间,当巴特沃斯低通滤波器的阶数较高时,接近于理想低通滤波器;当巴特沃斯低通滤波器的阶数较高时,则接近于高斯低通滤波器。
    1. # Butterworth低通滤波器的实现函数的定义
    2. def ButterworthPassFilter(image, d, n): # 定义一个Butterworth低通滤波器
    3. f = np.fft.fft2(image) # 快速傅里叶变换算法得到频率分布
    4. fshift = np.fft.fftshift(f) # 将图像中的低频部分移动到图像的中心,默认是在左上角
    5. # fft结果是复数, 其绝对值结果是振幅;取对数的目的是将数据变换到0~255
    6. fimg = np.log(np.abs(fshift))
    7. def make_transform_matrix(d):
    8. # 创建一个与输入图像同大小的全0矩阵,用于存储变化后的图像
    9. transform_matrix = np.zeros(image.shape)
    10. # 中心点值的计算,元组形式
    11. center_point = tuple(map(lambda x: (x - 1) / 2, fimg.shape))
    12. for i in range(transform_matrix.shape[0]): # 行遍历
    13. for j in range(transform_matrix.shape[1]): # 列遍历
    14. def cal_distance(pa, pb): # 欧拉距离计算函数的定义
    15. from math import sqrt
    16. dis = sqrt((pa[0] - pb[0]) ** 2 + (pa[1] - pb[1]) ** 2)
    17. return dis
    18. dis = cal_distance(center_point, (i, j)) # 求出每个点与中心点的距离
    19. # 巴特沃斯低通滤波的数学公式实现
    20. transform_matrix[i, j] = 1 / (1 + (dis / d) ** (2 * n))
    21. return transform_matrix
    22. d_matrix = make_transform_matrix(d) # 调用自定义函数
    23. new_img = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix))) # 生成新图
    24. return new_img
    25. print("ButterworthPassFilter {}".format(img.shape))
    26. butter_25_5 = ButterworthPassFilter(img,15, 5) # Butterworth低通滤波处理
    27. plt.subplot(12,3,10),plt.imshow(butter_25_5,'gray'),plt.title('ButterworthPassFilter')
    28. #cv2.imwrite("E:/python/CSDN/image/ButterworthPassFilter.jpg",butter_25_5)
    29. # 理想低通滤波器的实现函数的定义
    30. def perfectPassFilter(image, d): # 定义一个理想低通滤波器
    31. f = np.fft.fft2(image) # 快速傅里叶变换算法得到频率分布
    32. fshift = np.fft.fftshift(f) # 将图像中的低频部分移动到图像的中心,默认是在左上角
    33. # fft结果是复数, 其绝对值结果是振幅;取对数的目的是将数据变换到0~255
    34. fimg = np.log(np.abs(fshift))
    35. def make_transform_matrix(d):
    36. # 创建一个与输入图像同大小的全0矩阵,用于存储变化后的图像
    37. transform_matrix = np.zeros(image.shape)
    38. # 中心点值的计算,元组形式
    39. center_point = tuple(map(lambda x: (x - 1) / 2, fimg.shape))
    40. for i in range(transform_matrix.shape[0]): # 行遍历
    41. for j in range(transform_matrix.shape[1]): # 列遍历
    42. def cal_distance(pa, pb): # 欧拉距离计算函数的定义
    43. from math import sqrt
    44. dis = sqrt((pa[0] - pb[0]) ** 2 + (pa[1] - pb[1]) ** 2)
    45. return dis
    46. dis = cal_distance(center_point, (i, j)) # 求出每个点与中心点的距离
    47. # 理想低通滤波
    48. if dis<=d: #根据理想低通滤波器产生公式,当D(i,j)<=D0,置为1
    49. transform_matrix[i, j] = 1
    50. return transform_matrix
    51. d_matrix = make_transform_matrix(d) # 调用自定义函数
    52. new_img = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix))) # 生成新图
    53. return new_img
    54. print("perfectPassFilter {}".format(img.shape))
    55. perfectPass = perfectPassFilter(img,20) # 理想低通滤波处理
    56. plt.subplot(12,3,11),plt.imshow(perfectPass,'gray'),plt.title('perfectPassFilter')
    57. #cv2.imwrite("E:/python/CSDN/image/perfectPassFilter.jpg",perfectPass)
    58. # 高斯低通滤波器的实现函数的定义
    59. def gaussinlowPassFilter(image, d): # 定义一个高斯低通滤波器
    60. f = np.fft.fft2(image) # 快速傅里叶变换算法得到频率分布
    61. fshift = np.fft.fftshift(f) # 将图像中的低频部分移动到图像的中心,默认是在左上角
    62. # fft结果是复数, 其绝对值结果是振幅;取对数的目的是将数据变换到0~255
    63. fimg = np.log(np.abs(fshift))
    64. def make_transform_matrix(d):
    65. # 创建一个与输入图像同大小的全0矩阵,用于存储变化后的图像
    66. transform_matrix = np.zeros(image.shape)
    67. # 中心点值的计算,元组形式
    68. center_point = tuple(map(lambda x: (x - 1) / 2, fimg.shape))
    69. for i in range(transform_matrix.shape[0]): # 行遍历
    70. for j in range(transform_matrix.shape[1]): # 列遍历
    71. def cal_distance(pa, pb): # 欧拉距离计算函数的定义
    72. from math import sqrt
    73. dis = sqrt((pa[0] - pb[0]) ** 2 + (pa[1] - pb[1]) ** 2)
    74. return dis
    75. dis = cal_distance(center_point, (i, j)) # 求出每个点与中心点的距离
    76. # 高斯低通滤波
    77. from math import exp
    78. transform_matrix[i, j] = exp(-(dis*dis)/(2*(d**2))); #根据高斯低通滤波器公式H(u,v)=e^-[D^2(u,v)/2*D0^2]
    79. return transform_matrix
    80. d_matrix = make_transform_matrix(d) # 调用自定义函数
    81. new_img = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix))) # 生成新图
    82. return new_img
    83. print("gaussinlowPassFilter {}".format(img.shape))
    84. gaussinlowPassFilter = gaussinlowPassFilter(img,20) # 高斯低通滤波处理
    85. plt.subplot(12,3,12),plt.imshow(gaussinlowPassFilter,'gray'),plt.title('gaussinlowPassFilter')

     各阶段图像示例:

     

  • 相关阅读:
    Set与二分法效率
    SpringCloud - 微服务理论基础
    多方面浅谈互联网技术
    5分钟搞懂分布式可观测性
    16. 文件上传
    Windows Server - DHCP服务介绍及搭建
    electron使用typescript
    互联网Java工程师面试题·MySQL 篇·第二弹
    【数据结构】排序(1)插入排序、选择排序
    Go语言实现原理——Map实现原理
  • 原文地址:https://blog.csdn.net/L888666Q/article/details/126975423