【问题标题】:i want to create salt and pepper noise function (PIL and Numpy)我想创建椒盐噪声函数(PIL 和 Numpy)
【发布时间】:2018-11-03 13:44:43
【问题描述】:

我想创建椒盐噪声函数。 输入是noise_density,即输出图像中作为噪声的像素数量,它应该返回的值是噪声图像数据源

def salt_pepper(noise_density):



noisesource = ColumnDataSource(data={'image': [noiseImage]})
return noisesource

【问题讨论】:

    标签: python numpy python-imaging-library


    【解决方案1】:

    这个函数返回一个[密度]x[密度]像素的图像,使用numpy生成一个随机数组,使用PIL从数组中生成图像本身。

    def salt_pepper(density):
        imarray = numpy.random.rand(density,density,3) * 255
        return Image.fromarray(imarray.astype('uint8')).convert('L')
    

    现在,例如,您可以运行

    salt_pepper(500)
    

    生成一个 500x500px 的图像文件。

    当然,一定要

    import numpy
    from PIL import Image
    

    【讨论】:

    • 考虑添加至少一些 cmets - 解释,代码的作用以及它如何解决问题中的问题,很少无法改善答案。
    【解决方案2】:

    我想出了一个矢量化解决方案,我确信可以改进/简化它。尽管界面与请求的界面不完全一样,但代码非常简单(而且速度很快?),我相信它可以很容易地适应。

    import numpy as np
    from PIL import Image
    
    def salt_and_pepper(image, prob=0.05):
        # If the specified `prob` is negative or zero, we don't need to do anything.
        if prob <= 0:
            return image
    
        arr = np.asarray(image)
        original_dtype = arr.dtype
    
        # Derive the number of intensity levels from the array datatype.
        intensity_levels = 2 ** (arr[0, 0].nbytes * 8)
    
        min_intensity = 0
        max_intensity = intensity_levels - 1
    
        # Generate an array with the same shape as the image's:
        # Each entry will have:
        # 1 with probability: 1 - prob
        # 0 or np.nan (50% each) with probability: prob
        random_image_arr = np.random.choice(
            [min_intensity, 1, np.nan], p=[prob / 2, 1 - prob, prob / 2], size=arr.shape
        )
    
        # This results in an image array with the following properties:
        # - With probability 1 - prob: the pixel KEEPS ITS VALUE (it was multiplied by 1)
        # - With probability prob/2: the pixel has value zero (it was multiplied by 0)
        # - With probability prob/2: the pixel has value np.nan (it was multiplied by np.nan)
        # We need to to `arr.astype(np.float)` to make sure np.nan is a valid value.
        salt_and_peppered_arr = arr.astype(np.float) * random_image_arr
    
        # Since we want SALT instead of NaN, we replace it.
        # We cast the array back to its original dtype so we can pass it to PIL.
        salt_and_peppered_arr = np.nan_to_num(
            salt_and_peppered_arr, nan=max_intensity
        ).astype(original_dtype)
    
        return Image.fromarray(salt_and_peppered_arr)
    

    您可以像这样加载黑白版本的Lena

    lena = Image.open("lena.ppm")
    bwlena = Image.fromarray(np.asarray(lena).mean(axis=2).astype(np.uint8))
    

    最后,你可以保存几个例子:

    salt_and_pepper(bwlena, prob=0.1).save("sp01lena.png", "PNG")
    salt_and_pepper(bwlena, prob=0.3).save("sp03lena.png", "PNG")
    

    结果:

    https://i.ibb.co/J2y9HXS/sp01lena.png

    https://i.ibb.co/VTm5Vy2/sp03lena.png

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2021-11-12
      • 2021-04-09
      • 2013-01-04
      • 2013-03-14
      • 2020-05-05
      • 2013-10-04
      相关资源
      最近更新 更多