【发布时间】:2022-01-20 00:06:25
【问题描述】:
我正在尝试使用 Python 向图像添加高斯噪声。我正在使用下面的代码来定义我的函数添加噪声。到目前为止,它工作正常,但我必须在没有来自 numpy 的现成命令的情况下这样做。我只想使用 cv2。 代码如下:
def add_noise(img):
mean = 0
var = 10
sigma = var ** 1.5
gaussian = np.random.normal(mean, sigma, (512,512))
#np.zeros((224, 224), np.float32
noisy_image = np.zeros(img.shape, np.float32)
if len(img.shape) == 2:
noisy_image = img + gaussian
else:
noisy_image[:, :, 0] = img[:, :, 0] + gaussian
noisy_image[:, :, 1] = img[:, :, 1] + gaussian
noisy_image[:, :, 2] = img[:, :, 2] + gaussian
cv2.normalize(noisy_image, noisy_image, 0, 255, cv2.NORM_MINMAX, dtype=-1)
noisy_image = noisy_image.astype(np.uint8)
return noisy_image
我可以在不使用 numpy 并且特别是 np.random.normal 或其他准备好的命令的情况下编写它吗? 或者如何在没有 numpy 的情况下实现 np.random.normal;
提前谢谢你!
【问题讨论】:
-
没有 numpy 但有 cv2?你使用哪个库有什么关系? OpenCV 有
cv2.randn,其作用与np.random.normal相同。 -
你知道你正在添加灰色噪音吗?
-
灰色是什么意思?这不是高斯噪声吗?
标签: python image-processing gaussian noise