Skip to content

Commit a6ad58d

Browse files
committed
Added PSNR calculator for image compression quality analysis
1 parent 765a326 commit a6ad58d

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

Analysis/Compression_Analysis/PSNR.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
import math
3+
import cv2
4+
5+
def Representational(r,g,b):
6+
return (0.299*r+0.287*g+0.114*b)
7+
8+
def calculate(img):
9+
array = np.zeros(img.shape)
10+
b,g,r = cv2.split(img)
11+
pixelAt = Representational(r,g,b)
12+
return pixelAt
13+
14+
def main():
15+
16+
#Loading images (orignal image and compressed image)
17+
orignal_image = cv2.imread('orignal_image.png',1)
18+
compressed_image = cv2.imread('compressed_image.png',1)
19+
20+
#Getting image height and width
21+
height,width = orignal_image.shape[:2]
22+
23+
#Error initialization
24+
error_sum = 0.0
25+
26+
orignalPixelAt = 0.0
27+
compressedPixelAt = 0.0
28+
29+
orignalPixelAt = calculate(orignal_image)
30+
compressedPixelAt = calculate(compressed_image)
31+
diff = orignalPixelAt - compressedPixelAt
32+
error = np.sum(np.abs(diff) ** 2)
33+
34+
error = error/(height*width)
35+
36+
37+
#MSR = error_sum/(height*width)
38+
PSNR = -(10*math.log10(error/(255*255)))
39+
40+
print("PSNR value is {}".format(PSNR))
41+
42+
43+
if __name__ == '__main__':
44+
main()
45+
Loading
29.3 KB
Loading
81.9 KB
Loading

0 commit comments

Comments
 (0)