forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSNR.py
More file actions
38 lines (25 loc) · 811 Bytes
/
PSNR.py
File metadata and controls
38 lines (25 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
import math
import cv2
def Representational(r,g,b):
return (0.299*r+0.287*g+0.114*b)
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]
orignalPixelAt = calculate(orignal_image)
compressedPixelAt = calculate(compressed_image)
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))
if __name__ == '__main__':
main()