计算图像的信噪比,单位为db(分贝)

2024-04-19 18:01:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个脚本,将高斯噪声添加到图像中,如何计算该图像的信噪比

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

Tags: of图像imgnplinesomemeansigma
1条回答
网友
1楼 · 发布于 2024-04-19 18:01:16

信噪比=μ/σ

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

所以

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)

相关问题 更多 >