高斯噪声没有覆盖主要图像-Python

时间:2018-12-10 09:48:58

标签: python computer-vision gaussian

我正在尝试将高斯噪声添加到图像(“ pepper.jpg”)中。如结果所示(“ noisey pepper.png”),它可以工作;但噪音并未覆盖“辣椒”,而是在周围。应用噪音时我缺少什么吗? 任何建议将不胜感激。

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)

enter image description here

enter image description here

关于, 贝鲁兹

1 个答案:

答案 0 :(得分:0)

您转换为 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))

enter image description here

当然,这样的 sigma 你不会看到噪音的。请看一下这个(sigma为14):

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

enter image description here