代码之家  ›  专栏  ›  技术社区  ›  vdogsandman

向图像添加泊松噪声

  •  7
  • vdogsandman  · 技术社区  · 13 年前

    我有一些图像需要添加泊松噪声的增量,以便更彻底地分析它们。我知道你可以在MATLAB中做到这一点,但你是如何在Python中做到的?到目前为止,搜索没有结果。

    5 回复  |  直到 9 年前
        1
  •  18
  •   yuxiang.li    10 年前

    赫尔德的回答是正确的。我只想补充一个事实,泊松噪声不是加性的,你不能把它作为高斯噪声来加。

    根据你想要实现的目标,这里有一些建议:

    • 模拟低光噪声图像(如果峰值=1,则它将非常噪声)

      import numpy as np
      image = read_image("YOUR_IMAGE")  # need a rescale to be more realistic
      noisy = np.random.poisson(image / 255.0 * PEAK) / PEAK * 255  # noisy image
      
    • 在干净图像的顶部添加噪波层

      import numpy as np
      image = read_image("YOUR_IMAGE") 
      noisemap = create_noisemap() 
      noisy = image + np.random.poisson(noisemap)  
      

    然后,如果您愿意,您可以将结果裁剪为0-255(我使用PIL,所以我使用255而不是1)。

        2
  •  17
  •   Helder    11 年前

    事实上,保罗的回答毫无意义。

    泊松噪声与信号有关!使用他提供的这些命令,后来添加到图像中的噪声与信号无关。

    为了使其与信号相关,您可以将图像传递给NumPy的泊松函数:

    filename = 'myimage.png'
    img = (scipy.misc.imread(filename)).astype(float)
    
    noise_mask = numpy.random.poisson(img)
    
    noisy_img = img + noise_mask
    
        3
  •  5
  •   moi    6 年前

    你可以使用 skimage.util.random_noise :

    from skimage.util import random_noise
    
    noisy = random_noise(img, mode="poisson")
    
        4
  •  -1
  •   Community Mohan Dere    6 年前

    来自项目 1.4.4-“泊松分布的高斯近似” 属于 Chapter 1 的 book :

    对于较大的平均值,泊松分布良好 近似的 通过平均值和方差等于泊松随机变量平均值的高斯分布:

    P()N(,)

    然后,我们可以从正态分布N(0,1)生成泊松噪声,用的平方根缩放其标准偏差,并将其添加到图像中,该值为:

    # Image size
    M, N = 1000, 1000
    
    # Generate synthetic image
    image = np.tile(np.arange(0,N,dtype='float64'),(M,1)) * 20
    
    # -- sqrt(mu) * normal(0,1) --
    poisson_noise = np.sqrt(image) * np.random.normal(0, 1, image.shape)
    
    # Add the noise to the mu values
    noisy_image = image + poisson_noise
    
    plt.figure(figsize=(10,10))
    plt.subplot(2,2,1)
    plt.title('Image')
    plt.imshow(image,'gray')
    plt.subplot(2,2,2)
    plt.title('Noisy image noise')
    plt.imshow(noisy_image,'gray')  
    plt.subplot(2,2,3)
    plt.title('Image profile')
    plt.plot(image[0,:])
    plt.subplot(2,2,4)
    plt.title('Noisy image profile')
    plt.plot(noisy_image[0,:])
    
    print("Synthetic image mean: {}".format(image[:,1].mean()))
    print("Synthetic image variance: {}".format(image[:,1].var()))    
    print("Noisy image mean: {}".format(noisy_image[:,1].mean()))
    print("Noisy image variance: {}".format(noisy_image[:,1].var()))
    

    由于泊松噪声是信号相关的,随着我们增加潜在信号,噪声方差也会增加,正如我们在这一行剖面中看到的那样:

    image

    单列中的统计数据输出:

    合成图像平均值:20.0

    合成图像方差:0.0

    噪声图像平均值:19.931120555821597

    噪声图像方差:19.39456713877459

    进一步参考:[ 1 ][ 2 ]

        5
  •  -8
  •   Paul    8 年前

    如果您可以使用numpy/scipy,那么以下内容应该会有所帮助。我建议您将数组强制转换为float以进行中间计算,然后再强制转换回uint8以进行输出/显示。由于泊松噪声全部>=0时,您需要决定在返回uint8时如何处理数组溢出。你可以根据你的目标进行缩放或截断。

    filename = 'myimage.png'
    imagea = (scipy.misc.imread(filename)).astype(float)
    
    poissonNoise = numpy.random.poisson(imagea).astype(float)
    
    noisyImage = imagea + poissonNoise
    
    #here care must be taken to re cast the result to uint8 if needed or scale to 0-1 etc...