[英]Gaussian noise is not covering the main image-Python
I'm trying to add Gaussian noise to an image("pepper.jpg"). 我正在尝试将高斯噪声添加到图像(“ pepper.jpg”)中。 It works, as shown in the result ("noisy pepper.png"); 如结果所示(“ noisey pepper.png”),它可以工作; but the noise is not covering the "peppers" and it is around it. 但噪音并未覆盖“辣椒”,而是在周围。 Is there something I'm missing in applying noise? 应用噪音时我缺少什么吗? Any advice would be appreciated. 任何意见,将不胜感激。
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)
Regards, Behrouz 此致Behrouz
Your convertion to uint8 isn't complex. 您转换为uint8并不复杂。 Here is what you are looking for: 这是您要寻找的:
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))
Of course witch such sigma you ain't gonna see the noise. 当然,巫婆这种西格玛,你不会看到噪音的。 Please take look at this one (sigma will be around 14): 请看一看(sigma为14):
..
var = 200
sigma = var ** 0.5
..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.