Skip to main content
 首页 » 编程设计

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

2024年12月31日7tintown

我想向具有不同 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