高斯噪声未覆盖主图像-Python

Gaussian noise is not covering the main image-Python

我正在尝试向图像添加高斯噪声 ("pepper.jpg")。有效,如结果所示("noisy pepper.png");但是噪音并没有覆盖 "peppers" 而是在它周围。我在应用噪音时缺少什么吗? 如有任何建议,我们将不胜感激。

import cv2
import numpy as np

img = cv2.imread("pepper.jpg",0)
row, col = img.shape
mean = 0
var = 0.3
sigma = var ** 0.5
gauss = np.random.normal(mean, sigma, (row, col))
gauss = gauss.reshape(row, col)
noisyp = gauss + img
noisyp = noisyp.astype('uint8')
cv2.imwrite('noisy pepper.png', noisyp)

此致, 贝鲁兹

您对 uint8 的转换并不复杂。 这是您要查找的内容:

import cv2
import numpy as np


def convert_to_uint8(image_in):
    temp_image = np.float64(np.copy(image_in))
    cv2.normalize(temp_image, temp_image, 0, 255, cv2.NORM_MINMAX, dtype=-1)

    return temp_image.astype(np.uint8)


img = cv2.imread("pepper.jpg", 0)
row, col = img.shape
mean = 0
var = 0.3
sigma = var ** 0.5
gauss = np.random.normal(mean, sigma, (row, col))
gauss = gauss.reshape(row, col)
noisy_pepper = gauss + img

cv2.imwrite('noisy pepper.png', convert_to_uint8(noisy_pepper))

当然 这样的 sigma 你不会看到噪音。请看这个(sigma 将在 14 左右):

..
var = 200
sigma = var ** 0.5
..