首页 > 解决方案 > 以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

标签: pythonimage

解决方案


信噪比 = μ/σ

其中: μ是平均值或预期值,σ是标准偏差

所以...

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)

推荐阅读