gpt4 book ai didi

python - 根据 SNR 在图像上添加白噪声

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:56 27 4
gpt4 key购买 nike

我想向具有不同 SNR 级别的原始图像添加白噪声,但不确定该怎么做。

原始图像是(256, 128) 我正在使用acoustics 包来添加噪声。

original = cv2.imread(path)
white = acoustics.generator.white(256*128).reshape(256, 128)
out = original + white*255

cv2.imwrite(path, out)

我的问题:

  1. log10(mean(original)/std(original + white*255)) 算作 SNR 吗?(根据 wiki )

  2. 如果是这样,我可以只修改*255这个数字来修改SNR吗?

  3. 如果不是,如何计算SNR值?

最佳答案

关键事实是(这是数学,不是代码)

SNR = mean(s) / std(n)

将噪声乘以某个常数 A 会产生新的 SNR -- SNR_new

mean(s) / std(A*n) 
= mean(s) / (A * std(n))
= (1 / A) * (mean(s) / std(n))
= SNR / A
= SNR_new

所以向后工作,我认为这是 python 中的正确方法是:

def add_noise(signal, snr):
'''
signal: np.ndarray
snr: float

returns -> np.ndarray
'''

# Generate the noise as you did
noise = acoustics.generator.white(signal.size).reshape(*signal.shape)
# For the record I think np.random.random does exactly the same thing

# work out the current SNR
current_snr = np.mean(signal) / np.std(noise)

# scale the noise by the snr ratios (smaller noise <=> larger snr)
noise *= (current_snr / snr)

# return the new signal with noise
return signal + noise

关于python - 根据 SNR 在图像上添加白噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com