Skip to content

Commit 057291f

Browse files
committed
Change some Image File names & re-code the psnr algorithm (conserving both methods). Also Added new example.
1 parent fa86d3d commit 057291f

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed
Loading
Loading
Loading

analysis/compression_analysis/psnr.py

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,71 @@
1-
import numpy as np
1+
"""
2+
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
3+
1º Method: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
4+
2º Method: Incorrect ????
5+
"""
6+
27
import math
8+
39
import cv2
10+
import numpy as np
11+
12+
# This method is the really work as expected, but however I want to preserve the other method (psnr2)
13+
def psnr(original, contrast):
14+
mse = np.mean((original - contrast) ** 2)
15+
if mse == 0:
16+
return 100
17+
PIXEL_MAX = 255.0
18+
PSNR = 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
19+
return PSNR
20+
21+
22+
def Representational(r, g, b):
23+
# Formula to determine brightness of RGB color
24+
return (0.299*r+0.287*g+0.114*b)
425

5-
def Representational(r,g,b):
6-
return (0.299*r+0.287*g+0.114*b)
726

827
def calculate(img):
9-
b,g,r = cv2.split(img)
10-
pixelAt = Representational(r,g,b)
11-
return pixelAt
28+
b, g, r = cv2.split(img)
29+
return Representational(r, g, b)
1230

13-
def main():
14-
15-
#Loading images (orignal image and compressed image)
16-
orignal_image = cv2.imread('orignal_image.png',1)
17-
compressed_image = cv2.imread('compressed_image.png',1)
31+
# The 1º Method really works better
32+
def psnr2(original, contrast):
33+
# Getting image height and width
34+
height, width = original.shape[:2]
1835

19-
#Getting image height and width
20-
height,width = orignal_image.shape[:2]
36+
# Calculate the RGB Proportion for each Image and get the difference.
37+
originalPixelAt = calculate(original)
38+
compressedPixelAt = calculate(contrast)
2139

22-
orignalPixelAt = calculate(orignal_image)
23-
compressedPixelAt = calculate(compressed_image)
40+
diff = originalPixelAt - compressedPixelAt
2441

25-
diff = orignalPixelAt - compressedPixelAt
26-
error = np.sum(np.abs(diff) ** 2)
42+
# Calculate the error
43+
error = np.sum(np.abs(diff) ** 2) / (height * width)
2744

28-
error = error/(height*width)
45+
# MSR = error_sum/(height*width)
46+
PSNR = -(10*math.log10(error/(255*255)))
47+
return format(PSNR)
2948

30-
#MSR = error_sum/(height*width)
31-
PSNR = -(10*math.log10(error/(255*255)))
3249

33-
print("PSNR value is {}".format(PSNR))
50+
def main():
3451

52+
# Loading images (original image and compressed image)
53+
original = cv2.imread('original_image.png')
54+
contrast = cv2.imread('compressed_image.png', 1)
3555

36-
if __name__ == '__main__':
37-
main()
56+
original2 = cv2.imread('PSNR-example-base.png')
57+
contrast2 = cv2.imread('PSNR-example-comp-10.jpg', 1)
3858

59+
# Value expected: 29.73dB
60+
print("-- First Test --")
61+
print(f"1º Method: \n PSNR value is {psnr(original, contrast)} dB")
62+
print(f"2º Method: \n PSNR value is {psnr2(original, contrast)} dB \n")
63+
64+
# # Value expected: 31.53dB (Wikipedia Example)
65+
print("-- Second Test --")
66+
print(f"1º Method: \n PSNR value is {psnr(original2, contrast2)} dB")
67+
print(f"2º Method: \n PSNR value is {psnr2(original2, contrast2)} dB")
68+
69+
70+
if __name__ == '__main__':
71+
main()

0 commit comments

Comments
 (0)