python - 以db(分贝)计算图像的信噪比
问题描述
我创建了一个向图像添加高斯噪声的脚本,如何计算该图像的 SNR?
def noisy(img,sigma): # noise function
# ...
img = np.asarray(img,'double') # numpy-array of shape (N, M);
embeadead_line = img[0] # first line/row should not be changed
img = np.delete(img, (0), axis=0)
mean = 0.0 # some constant
std = sigma # some constant (standard deviation)double
noisy_img = img + np.random.normal(mean, std, img.shape).astype(np.double)
noisy_img_clipped = np.clip(noisy_img, 0, 255).astype(np.uint8) # we might get out of bounds due to noise
noisy_img_clipped = np.insert(noisy_img_clipped, 0, embeadead_line, 0) # insert embedead line back
return noisy_img_clipped
解决方案
信噪比 = μ/σ
其中:
μ
是平均值或预期值,σ
是标准偏差
所以...
image_sd = np.std(noisy_img_clipped);
image_mean = np.mean(noisy_img_clipped);
image_snr = image_mean / image_sd;
如果您想以 dB 为单位,这可能会很棘手,因为您需要额外的常数,具体取决于您测量的内容。如果您不知道它是什么,您可以使用 1,但通常以 dB 为单位显示未知源的 SNR 被认为是一种不好的方法。
import math
image_snr_db = 1 * math.log(image_snr,2);
如果您询问 PSNR,则方程不同,它需要 2 张图像。原始的和模糊的。使用 OpenCV,您可以这样计算:
import cv2
img1 = cv2.imread(original_image)
img2 = cv2.imread(noisy_image)
psnr = cv2.PSNR(img1, img2)
推荐阅读
- javascript - 如果所有类都相同,如果只有文本不同,如何选择元素?
- node.js - 类型“未定义”不可分配给类型“字符串 | 细绳[]'
- r - R:按字母倒序排列图表中的列
- aem - 杰克逊图书馆中是否有 Sling JsonItemWriter 的替代品
- python - 在 pandas 中预处理我的数据后,如何在加载 Power Bi 之前删除不需要的数据帧?
- r - 我需要通过 plotly 制作一个热图,以显示更多信息
- python - JSON 和 Python 加密:TypeError:字节类型的对象不是 JSON 可序列化的
- python - PyPDF2 调整文本或图像大小
- android - Flutter 上的简单可扩展列也不会有 Title > expandable
- awk - 在将 RS 设置为另一件事时,为什么 GNU Awk 的 POSIX 模式不将新行视为字段?