Skip to content

Commit 9097977

Browse files
vasutomarharshildarji
authored andcommitted
Added PSNR calculator for image compression quality analysis (TheAlgorithms#475)
* Added PSNR calculator for image compression quality analysis * Removed unused variables and redundancy
1 parent 315a357 commit 9097977

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Analysis/Compression_Analysis/PSNR.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
b,g,r = cv2.split(img)
10+
pixelAt = Representational(r,g,b)
11+
return pixelAt
12+
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)
18+
19+
#Getting image height and width
20+
height,width = orignal_image.shape[:2]
21+
22+
orignalPixelAt = calculate(orignal_image)
23+
compressedPixelAt = calculate(compressed_image)
24+
25+
diff = orignalPixelAt - compressedPixelAt
26+
error = np.sum(np.abs(diff) ** 2)
27+
28+
error = error/(height*width)
29+
30+
#MSR = error_sum/(height*width)
31+
PSNR = -(10*math.log10(error/(255*255)))
32+
33+
print("PSNR value is {}".format(PSNR))
34+
35+
36+
if __name__ == '__main__':
37+
main()
38+
Loading
29.3 KB
Loading
81.9 KB
Loading

0 commit comments

Comments
 (0)