Skip to content

Commit beafe36

Browse files
rafagarciacharshildarji
authored andcommitted
Re-design psnr.py code and change image names (TheAlgorithms#592)
* Change some Image File names & re-code the psnr algorithm (conserving both methods). Also Added new example. * Selected psnr method and reformat some code from arithmetic_analysis
1 parent 39912ae commit beafe36

File tree

10 files changed

+53
-50
lines changed

10 files changed

+53
-50
lines changed
Loading
Loading
Loading

analysis/compression_analysis/psnr.py

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
import numpy as np
1+
"""
2+
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
3+
Soruce: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
4+
"""
5+
26
import math
7+
38
import cv2
9+
import numpy as np
410

5-
def Representational(r,g,b):
6-
return (0.299*r+0.287*g+0.114*b)
11+
def psnr(original, contrast):
12+
mse = np.mean((original - contrast) ** 2)
13+
if mse == 0:
14+
return 100
15+
PIXEL_MAX = 255.0
16+
PSNR = 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
17+
return PSNR
718

8-
def calculate(img):
9-
b,g,r = cv2.split(img)
10-
pixelAt = Representational(r,g,b)
11-
return pixelAt
1219

1320
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)
1821

19-
#Getting image height and width
20-
height,width = orignal_image.shape[:2]
22+
# Loading images (original image and compressed image)
23+
original = cv2.imread('original_image.png')
24+
contrast = cv2.imread('compressed_image.png', 1)
2125

22-
orignalPixelAt = calculate(orignal_image)
23-
compressedPixelAt = calculate(compressed_image)
26+
original2 = cv2.imread('PSNR-example-base.png')
27+
contrast2 = cv2.imread('PSNR-example-comp-10.jpg', 1)
2428

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))
29+
# Value expected: 29.73dB
30+
print("-- First Test --")
31+
print(f"PSNR value is {psnr(original, contrast)} dB")
32+
33+
# # Value expected: 31.53dB (Wikipedia Example)
34+
print("\n-- Second Test --")
35+
print(f"PSNR value is {psnr(original2, contrast2)} dB")
3436

3537

3638
if __name__ == '__main__':
37-
main()
38-
39+
main()

arithmetic_analysis/bisection.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
1515
return
1616
else:
1717
mid = (start + end) / 2
18-
while abs(start - mid) > 0.0000001: # until we achieve precise equals to 10^-7
18+
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
1919
if function(mid) == 0:
2020
return mid
2121
elif function(mid) * function(start) < 0:
@@ -29,5 +29,5 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
2929
def f(x):
3030
return math.pow(x, 3) - 2*x - 5
3131

32-
33-
print(bisection(f, 1, 1000))
32+
if __name__ == "__main__":
33+
print(bisection(f, 1, 1000))

arithmetic_analysis/intersection.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ def intersection(function,x0,x1): #function is the f we want to find its root an
55
x_n1 = x1
66
while True:
77
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
8-
if abs(x_n2 - x_n1)<0.00001 :
8+
if abs(x_n2 - x_n1) < 10**-5:
99
return x_n2
1010
x_n=x_n1
1111
x_n1=x_n2
1212

1313
def f(x):
14-
return math.pow(x,3)-2*x-5
14+
return math.pow(x , 3) - (2 * x) -5
1515

16-
print(intersection(f,3,3.5))
16+
if __name__ == "__main__":
17+
print(intersection(f,3,3.5))
+11-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition
12
import numpy
23

34
def LUDecompose (table):
4-
#table that contains our data
5-
#table has to be a square array so we need to check first
5+
# Table that contains our data
6+
# Table has to be a square array so we need to check first
67
rows,columns=numpy.shape(table)
78
L=numpy.zeros((rows,columns))
89
U=numpy.zeros((rows,columns))
910
if rows!=columns:
10-
return
11+
return []
1112
for i in range (columns):
1213
for j in range(i-1):
1314
sum=0
@@ -22,13 +23,10 @@ def LUDecompose (table):
2223
U[i][j]=table[i][j]-sum1
2324
return L,U
2425

25-
26-
27-
28-
29-
30-
31-
matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]])
32-
L,U = LUDecompose(matrix)
33-
print(L)
34-
print(U)
26+
if __name__ == "__main__":
27+
matrix =numpy.array([[2,-2,1],
28+
[0,1,2],
29+
[5,3,1]])
30+
L,U = LUDecompose(matrix)
31+
print(L)
32+
print(U)

arithmetic_analysis/newton_method.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method
2+
13
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
24
x_n=startingInt
35
while True:
46
x_n1=x_n-function(x_n)/function1(x_n)
5-
if abs(x_n-x_n1)<0.00001:
7+
if abs(x_n-x_n1) < 10**-5:
68
return x_n1
79
x_n=x_n1
810

911
def f(x):
10-
return (x**3)-2*x-5
12+
return (x**3) - (2 * x) -5
1113

1214
def f1(x):
13-
return 3*(x**2)-2
15+
return 3 * (x**2) -2
1416

15-
print(newton(f,f1,3))
17+
if __name__ == "__main__":
18+
print(newton(f,f1,3))

data_structures/graph/dijkstra.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def minDist(mdist, vset, V):
2121
def Dijkstra(graph, V, src):
2222
mdist=[float('inf') for i in range(V)]
2323
vset = [False for i in range(V)]
24-
mdist[src] = 0.0;
24+
mdist[src] = 0.0
2525

2626
for i in range(V-1):
2727
u = minDist(mdist, vset, V)

0 commit comments

Comments
 (0)