根据 SNR 在图像上添加白噪声
Add white-noise on image based on SNR
我想在具有不同 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)
我的问题:
log10(mean(original)/ std(original + white*255))
算SNR吗?(根据wiki)
如果可以,我可以只修改*255
这个号码来修改信噪比吗?
如果不是,如何计算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
我想在具有不同 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)
我的问题:
log10(mean(original)/ std(original + white*255))
算SNR吗?(根据wiki)如果可以,我可以只修改
*255
这个号码来修改信噪比吗?如果不是,如何计算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