|
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 | + |
2 | 7 | import math
|
| 8 | + |
3 | 9 | 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) |
4 | 25 |
|
5 |
| -def Representational(r,g,b): |
6 |
| - return (0.299*r+0.287*g+0.114*b) |
7 | 26 |
|
8 | 27 | 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) |
12 | 30 |
|
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] |
18 | 35 |
|
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) |
21 | 39 |
|
22 |
| - orignalPixelAt = calculate(orignal_image) |
23 |
| - compressedPixelAt = calculate(compressed_image) |
| 40 | + diff = originalPixelAt - compressedPixelAt |
24 | 41 |
|
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) |
27 | 44 |
|
28 |
| - error = error/(height*width) |
| 45 | + # MSR = error_sum/(height*width) |
| 46 | + PSNR = -(10*math.log10(error/(255*255))) |
| 47 | + return format(PSNR) |
29 | 48 |
|
30 |
| - #MSR = error_sum/(height*width) |
31 |
| - PSNR = -(10*math.log10(error/(255*255))) |
32 | 49 |
|
33 |
| - print("PSNR value is {}".format(PSNR)) |
| 50 | +def main(): |
34 | 51 |
|
| 52 | + # Loading images (original image and compressed image) |
| 53 | + original = cv2.imread('original_image.png') |
| 54 | + contrast = cv2.imread('compressed_image.png', 1) |
35 | 55 |
|
36 |
| -if __name__ == '__main__': |
37 |
| - main() |
| 56 | + original2 = cv2.imread('PSNR-example-base.png') |
| 57 | + contrast2 = cv2.imread('PSNR-example-comp-10.jpg', 1) |
38 | 58 |
|
| 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