python - 如何使用 numpy 生成具有选定空间分布的噪声?

标签 python numpy image-processing matplotlib noise

我想以与此类似的方式向图像添加噪声:

import numpy as np
import matplotlib.pyplot as plt

myImage = plt.imread('Pikachu.png')
noise = np.random.normal(0, 0.1, (myImage.shape[0], myImage.shape[1]))

noisyImage = myImage + noise

但是,我需要图像中心的噪点更加强烈,并且随着离中心越远,噪点就越不强烈。

理想情况下,我可以调节噪声的空间分布参数,以便我的噪声变量包含:

  • 初始化:所有图像上都有强烈且均匀的噪声
  • 早期阶段:中心噪声强烈,边缘噪声较少
  • 中期:中心有噪音,边缘不再有噪音
  • 后期:所有图像上不再有噪声(全为零)

有谁知道有什么办法吗?非常感谢任何帮助!

最佳答案

您可以用作起点的东西:

import numpy as np
import matplotlib.pyplot as plt

def gauss2D(shape,sx=1,sy=1):
    """
    unnormalized 2D gauss centered on mean value, 
    given shape and standard dev (sx and sy).
    """
    mx = shape[0]/2
    my = shape[1]/2

    return np.exp( -0.5*(
        ((np.arange(shape[0])[:,None]-mx)/sx)**2+
        ((np.arange(shape[0])[None,:]-my)/sy)**2
        ))#/(2*np.pi*sx*sy)

width,height = 64,64
my_img = np.zeros((width,height,3))+0.9
fig = plt.figure()
ax = fig.gca()
N=5
for i in range(N):
    my_img[:,:,:]=0.5 #gray bg image
    w = N*100/(4**(2*i))
    A = (1-.1*(i+1))
    noise =A*np.random.normal(0,w,(width,height))*gauss2D((width,height),10,10)
    plt.imshow(my_img+noise[:,:,None]) #noise affects rgb equally
    plt.title(i)
    plt.show()

输出:

enter image description here enter image description here enter image description here enter image description here enter image description here

这里的噪声是从高斯分布中采样的,但均匀分布应该可以正常工作。

重要的部分是通过高斯对噪声进行加权以获得您想要的效果。

您可能需要调整A(振幅)和w(扩展)以满足您的需求(可能只是两个列表)。您希望尽早获得高振幅和扩展,然后首先减小扩展(可能会增加振幅),然后将振幅减小到零。

关于python - 如何使用 numpy 生成具有选定空间分布的噪声?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38132250/

相关文章:

python - numpy 矩阵。将全 0 移动到每行的末尾

c++ - OpenCV 矩阵运算导致 YUV 图像颜色损坏

python-3.x - 如何将 bool 数据类型为 True 的 numpy 零数组索引?

python - odoo-bin : error: option --addons-path: no such directory: '../mymodules'

python - 我如何将两个不同字典中的值相乘

python - 在 GAE(日历 API)的 Python 中解析来自 InsertCalendar 的原子响应

python - 在 Python 中为数据创建直方图

python - Numpy reshape 行为异常

python - 如何根据特定列在 Pandas Dataframe 中向上移动行

image-processing - 图像处理和计算机视觉