Reputation: 89
I create a script that adds gauss noise to an image, how can I calculate SNR of this image?
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
Upvotes: 0
Views: 2163
Reputation: 12036
SNR = μ/σ
where:
μ
is the average or expected value and σ
is the standard deviation
So...
image_sd = np.std(noisy_img_clipped);
image_mean = np.mean(noisy_img_clipped);
image_snr = image_mean / image_sd;
If you want to get it in dB, that could be tricky, as you need additional constant depending of what you are measuring. If you don't know what it is, you could use 1, but generally showing SNR in dB for unknown source is considered as a bad approach.
import math
image_snr_db = 1 * math.log(image_snr,2);
If you are asking about PSNR, the equasion is different and it needs 2 images. The original one and the blurred one. With OpenCV, you could calculate it like this:
import cv2
img1 = cv2.imread(original_image)
img2 = cv2.imread(noisy_image)
psnr = cv2.PSNR(img1, img2)
Upvotes: 1