python数字图像处理-图像噪声与去噪算法

图像噪声

椒盐噪声

概述: 椒盐噪声(salt & pepper noise)是数字图像的一个常见噪声,所谓椒盐,椒就是黑,盐就是白,椒盐噪声就是在图像上随机出现黑色白色的像素。椒盐噪声是一种因为信号脉冲强度引起的噪声,产生该噪声的算法也比较简单。

给一副数字图像加上椒盐噪声的步骤如下:

  1. 指定信噪比 SNR (其取值范围在[0, 1]之间)
  2. 计算总像素数目 SP, 得到要加噪的像素数目 NP = SP * (1-SNR)
  3. 随机获取要加噪的每个像素位置P(i, j)
  4. 指定像素值为255或者0。
  5. 重复3,4两个步骤完成所有像素的NP个像素
  6. 输出加噪以后的图像

高斯噪声

概述: 加性高斯白噪声(Additive white Gaussian noise,AWGN)在通信领域中指的是一种功率谱函数是常数(即白噪声), 且幅度服从高斯分布的噪声信号. 这类噪声通常来自感光元件, 且无法避免.

去噪算法

中值滤波

概述: 中值滤波是一种非线性空间滤波器, 它的响应基于图像滤波器包围的图像区域中像素的统计排序, 然后由统计排序结果的值代替中心像素的值. 中值滤波器将其像素邻域内的灰度中值代替代替该像素的值. 中值滤波器的使用非常普遍, 这是因为对于一定类型的随机噪声, 它提供了一种优秀的去噪能力, 比小尺寸的均值滤波器模糊程度明显要低. 中值滤波器对处理脉冲噪声(也称椒盐噪声)非常有效, 因为该噪声是以黑白点叠加在图像上面的.

与中值滤波相似的还有最大值滤波器和最小值滤波器.

均值滤波

概述: 均值滤波器的输出是包含在滤波掩模领域内像素的简单平均值. 均值滤波器最常用的目的就是减噪. 然而, 图像边缘也是由图像灰度尖锐变化带来的特性, 所以均值滤波还是存在不希望的边缘模糊负面效应.

均值滤波还有一个重要应用, 为了对感兴趣的图像得出一个粗略描述而模糊一幅图像. 这样, 那些较小物体的强度与背景揉合在一起了, 较大物体变得像斑点而易于检测.掩模的大小由即将融入背景中的物体尺寸决定.

代码

#encoding: utf-8

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import math
import random
import cv2
import scipy.misc
import scipy.signal
import scipy.ndimage

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
    mu = 0
    sigma = 0.12
    
    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, (8, 8))
    Grey_gs_mf = scipy.ndimage.median_filter(Grey_gs, (8, 8))

    # 均值滤波
    n = 3
    window = np.ones((n, n)) / n ** 2
    Grey_sp_me = convert_2d(Grey_sp)
    Grey_gs_me = convert_2d(Grey_gs)

    plt.subplot(321)
    plt.title('Grey salt and pepper noise')
    plt.imshow(Grey_sp, cmap='gray')
    plt.subplot(322)
    plt.title('Grey gauss noise')
    plt.imshow(Grey_gs, cmap='gray')

    plt.subplot(323)
    plt.title('Grey salt and pepper noise (medium)')
    plt.imshow(Grey_sp_mf, cmap='gray')
    plt.subplot(324)
    plt.title('Grey gauss noise (medium)')
    plt.imshow(Grey_gs_mf, cmap='gray')

    plt.subplot(325)
    plt.title('Grey salt and pepper noise (mean)')
    plt.imshow(Grey_sp_me, cmap='gray')
    plt.subplot(326)
    plt.title('Grey gauss noise (mean)')
    plt.imshow(Grey_gs_me, cmap='gray')
    plt.show()

    


def main():
    img = np.array(Image.open('LenaRGB.bmp'))
    add_salt_noise(img)



if __name__ == '__main__':
    main()

见https://github.com/wangshub/python-image-process
 

  • 0
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Zhang-Suen算法是一种经典的图像细化算法,能够将二值图像中的线条、边缘等细化成一条像素宽度的线条。该算法的核心思想是通过迭代地删除图像中的像素,直到不能再继续细化为止。 以下是一个简单的Python实现: ```python import numpy as np import cv2 # 读取图像并转换为二值图像 img = cv2.imread('input.jpg', cv2.IMREAD_GRAYSCALE) _, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # 定义两个函数用于实现Zhang-Suen算法 def neighbours(x, y, image): """获取像素(x, y)的8邻域像素""" neig = [image[x-1][y-1], image[x][y-1], image[x+1][y-1], image[x-1][y], image[x+1][y], image[x-1][y+1], image[x][y+1], image[x+1][y+1]] return neig def zhangSuen(image): """Zhang-Suen算法""" rows, cols = image.shape changing1 = changing2 = [(-1,-1)] while changing1 or changing2: # 阶段1 changing1 = [] for x in range(1, rows - 1): for y in range(1, cols - 1): neig = neighbours(x, y, image) if image[x][y] == 255 and 2 <= sum(neig) <= 6 and \ neig[0]*neig[2]*neig[4] == 0 and \ neig[2]*neig[4]*neig[6] == 0: changing1.append((x,y)) for x, y in changing1: image[x][y] = 0 # 阶段2 changing2 = [] for x in range(1, rows - 1): for y in range(1, cols - 1): neig = neighbours(x, y, image) if image[x][y] == 255 and 2 <= sum(neig) <= 6 and \ neig[0]*neig[2]*neig[6] == 0 and \ neig[0]*neig[4]*neig[6] == 0: changing2.append((x,y)) for x, y in changing2: image[x][y] = 0 return image # 进行Zhang-Suen算法细化 thinned = zhangSuen(img) # 显示结果 cv2.imshow('Thinned Image', thinned) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个示例中,我们首先使用cv2.imread()函数读取输入图像,并使用cv2.threshold()函数将其转换为二值图像。然后,我们定义了两个函数neighbours()和zhangSuen(),用于实现Zhang-Suen算法。zhangSuen()函数通过迭代地调用neighbours()函数获取像素的8邻域像素,并根据算法的条件来判断是否要删除该像素。最后,我们使用zhangSuen()函数对输入图像进行细化处理,并将结果保存到thinned变量中。最后,我们使用cv2.imshow()函数显示细化后的图像,并使用cv2.waitKey()和cv2.destroyAllWindows()函数来等待用户按下任意键,然后关闭所有窗口。 需要注意的是,Zhang-Suen算法只适用于二值图像,即黑白图像。如果你的输入图像是彩色图像,则需要先将其转换为灰度图像,并使用阈值处理将其转换为二值图像,然后再进行细化处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值