Re-design psnr.py code and change image names (#592)

* Change some Image File names & re-code the psnr algorithm (conserving both methods). Also Added new example.

* Selected psnr method and reformat some code from arithmetic_analysis
This commit is contained in:
Rafael García Cuéllar
2018-11-05 18:19:08 +01:00
committed by Harshil
parent 39912aed57
commit beafe3656f
10 changed files with 53 additions and 50 deletions

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 MiB

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 476 KiB

View File

Before

Width:  |  Height:  |  Size: 82 KiB

After

Width:  |  Height:  |  Size: 82 KiB

View File

@@ -1,38 +1,39 @@
import numpy as np
"""
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
Soruce: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
"""
import math
import cv2
import numpy as np
def Representational(r,g,b):
return (0.299*r+0.287*g+0.114*b)
def psnr(original, contrast):
mse = np.mean((original - contrast) ** 2)
if mse == 0:
return 100
PIXEL_MAX = 255.0
PSNR = 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
return PSNR
def calculate(img):
b,g,r = cv2.split(img)
pixelAt = Representational(r,g,b)
return pixelAt
def main():
#Loading images (orignal image and compressed image)
orignal_image = cv2.imread('orignal_image.png',1)
compressed_image = cv2.imread('compressed_image.png',1)
#Getting image height and width
height,width = orignal_image.shape[:2]
# Loading images (original image and compressed image)
original = cv2.imread('original_image.png')
contrast = cv2.imread('compressed_image.png', 1)
orignalPixelAt = calculate(orignal_image)
compressedPixelAt = calculate(compressed_image)
original2 = cv2.imread('PSNR-example-base.png')
contrast2 = cv2.imread('PSNR-example-comp-10.jpg', 1)
diff = orignalPixelAt - compressedPixelAt
error = np.sum(np.abs(diff) ** 2)
error = error/(height*width)
#MSR = error_sum/(height*width)
PSNR = -(10*math.log10(error/(255*255)))
print("PSNR value is {}".format(PSNR))
# Value expected: 29.73dB
print("-- First Test --")
print(f"PSNR value is {psnr(original, contrast)} dB")
# # Value expected: 31.53dB (Wikipedia Example)
print("\n-- Second Test --")
print(f"PSNR value is {psnr(original2, contrast2)} dB")
if __name__ == '__main__':
main()
main()