原理
- 概念:在图像上随机地添加了黑色和白色地像素点,使得图像看起来像是撒了椒盐。
- 盐噪声:白色噪声,是高灰度噪声
- 椒噪声:黑色噪声,是低灰度噪声
- 去除方法:中值滤波
实战
def imgaug_example(image, p=0.03):
"""
args:
image: np.ndarray
p: 将像素替换为椒盐噪声的概率。 取值范围(0.0, 0.03)
"""
augmenter = iaa.SaltAndPepper(p=p)
image = augmenter(image=image)
return image
def torch_example(image, p=0.03):
"""
args:
image: torch.Tensor
p: 将像素替换为椒盐噪声的概率。 取值范围(0.0, 0.03)
"""
_, imh, imw = image.shape
num_salt = int(p * imh * imw)
num_salt_0 = random.randint(0, num_salt)
num_salt_255 = num_salt - num_salt_0
coords1 = [torch.randint(low=0, high=i - 1, size=(num_salt_0,)) for i in [imh, imw]]
image[:, coords1[0], coords1[1]] = 0
coords2 = [torch.randint(low=0, high=i - 1, size=(num_salt_255,)) for i in [imh, imw]]
image[:, coords2[0], coords2[1]] = 255
return image
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/05f64b2c1f54544987829b3cc1afae97.png#pic_center)