椒盐噪声的实现原理为,随机地将图像中的一定比例的像素值取极大或者极小:

这里给出 Python 下的一种实现,可简单地转换为其他:

def salt_and_pepper_noise(x, v):
    # x 表示原始无噪图像,行数表示图像的个数,列数表示单幅图像的像素数。
    x_noise = x.copy()
    n_features = x.shape[1]
    mn = x.min()
    mx = x.max()
    for i, sample in enumerate(x):
        mask = np.random.randint(0, n_features, v)
        for m in mask:
            if np.random.rand() < .5:
                x[i][m] = mn
            else:
                x[i][m] = mx
    return x_noise

相关文章:

  • 2021-04-28
  • 2021-05-21
  • 2021-10-17
  • 2021-08-31
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-27
  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案