给图像添加噪声

如果你把图像看作信号,那么噪声就是干扰信号。我们在采集图像时可能因为各种各样的干扰而引入图像噪声。前面提到,我们可以把图像看作一个函数,那么带有噪声的图像,就可以看作是原始图像函数与噪声函数相加的和。
f(x, y) = I(x, y) + noise

常见的噪声有椒盐噪声(salt and pepper noise),为什么叫椒盐噪声?因为图像的像素点由于噪声影响随机变成了黑点(dark spot)或白点(white spot)。这里的“椒”不是我们常见的红辣椒或青辣椒,而是外国的“胡椒”(香料的一种)。我们知道,胡椒是黑色的,盐是白色的,所以才取了这么个形象的名字。

下面是原始图片:
这里写图片描述

我们要给图像添加椒盐噪声,就要把图像的像素点的强度值改为黑点或者白点,黑点的强度值是0,白点的强度值是255。原始图像的强度值区间是[0, 255],那么噪声函数中相应点是255或者是-255,加起来就可以达到是0或255的效果。需要注意,椒盐噪声是随机的改变图像中像素点的值为黑点或白点,并不是对每个像素点都进行操作。

下面是生成10%的盐噪声和椒噪声函数矩阵:

import cv2
import numpy as np
peppers = cv2.imread("peppers.bmp", 0)
row, column = peppers.shape
noise_salt = np.random.randint(0, 256, (row, column))
noise_pepper = np.random.randint(0, 256, (row, column))
rand = 0.1
noise_salt = np.where(noise_salt < rand * 256, 255, 0)
noise_pepper = np.where(noise_pepper < rand * 256, -255, 0)

我们要注意,opencv的图像矩阵类型是uint8,低于0和高于255的值并不截断,而是使用了模操作。即200+60=260 % 256 = 4。所以我们需要先将原始图像矩阵和噪声图像矩阵都转成浮点数类型进行相加操作,然后再转回来。

peppers.astype("float")
noise_salt.astype("float")
noise_pepper.astype("float")
salt = peppers + noise_salt
pepper = peppers + noise_pepper
salt = np.where(salt > 255, 255, salt)
pepper = np.where(pepper < 0, 0, pepper)
cv2.imshow("salt", salt.astype("uint8"))
cv2.imshow("pepper", pepper.astype("uint8"))
cv2.waitKey()

这里写图片描述

这里写图片描述

还有一种常见的噪声是高斯噪声。椒盐噪声出现在随机的像素点位置,而高斯噪声不同,每个像素点都出现噪声。同样的,在opencv中需要将图像矩阵转换成浮点数再进行加法操作,注意这里用了嵌套的where用于截断小于0和大于255的值。

peppers.astype("float")
Gauss_noise = np.random.normal(0, 50, (row, column))
Gauss = peppers + Gauss_noise
Gauss = np.where(Gauss < 0, 0, np.where(Gauss > 255, 255, Gauss))
cv2.imshow("peppers_Gauss", Gauss.astype("uint8"))
cv2.waitKey()

这里写图片描述

Python中有很多库可以用来给图像添加噪声,比较常用的有`numpy`和`opencv`。下面分别介绍一下它们的使用方法。 使用`numpy`给图像添加噪声: ```python import numpy as np import cv2 img = cv2.imread('image.jpg', 0) # 读取灰度图像 # 添加高斯噪声 mean = 0 var = 100 sigma = var ** 0.5 gaussian = np.random.normal(mean, sigma, img.shape) gaussian = gaussian.reshape(img.shape) noisy_img = img + gaussian # 添加椒盐噪声 s_vs_p = 0.5 amount = 0.004 out = np.copy(img) # Salt mode num_salt = np.ceil(amount * img.size * s_vs_p) coords = [np.random.randint(0, i - 1, int(num_salt)) for i in img.shape] out[coords] = 255 # Pepper mode num_pepper = np.ceil(amount * img.size * (1. - s_vs_p)) coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in img.shape] out[coords] = 0 noisy_img = out cv2.imshow('Original Image', img) cv2.imshow('Noisy Image', noisy_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 使用`opencv`给图像添加噪声: ```python import cv2 import numpy as np img = cv2.imread('image.jpg', 0) # 读取灰度图像 # 添加高斯噪声 mean = 0 var = 100 sigma = var ** 0.5 gaussian = np.random.normal(mean, sigma, img.shape) gaussian = gaussian.reshape(img.shape) noisy_img = img + gaussian.astype(np.uint8) # 添加椒盐噪声 s_vs_p = 0.5 amount = 0.004 out = np.copy(img) # Salt mode num_salt = np.ceil(amount * img.size * s_vs_p) coords = [np.random.randint(0, i - 1, int(num_salt)) for i in img.shape] out[coords] = 255 # Pepper mode num_pepper = np.ceil(amount * img.size * (1. - s_vs_p)) coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in img.shape] out[coords] = 0 noisy_img = out cv2.imshow('Original Image', img) cv2.imshow('Noisy Image', noisy_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这里使用的是高斯噪声和椒盐噪声,如果需要添加其他类型的噪声,可以参考相应的文档进行修改。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值