mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-07 10:00:55 +00:00
Re-design psnr.py code and change image names (#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
This commit is contained in:
parent
39912aed57
commit
beafe3656f
BIN
analysis/compression_analysis/PSNR-example-base.png
Normal file
BIN
analysis/compression_analysis/PSNR-example-base.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.3 MiB |
BIN
analysis/compression_analysis/PSNR-example-comp-10.jpg
Normal file
BIN
analysis/compression_analysis/PSNR-example-comp-10.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 104 KiB |
BIN
analysis/compression_analysis/example_wikipedia_image.jpg
Normal file
BIN
analysis/compression_analysis/example_wikipedia_image.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 476 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 82 KiB |
|
@ -1,38 +1,39 @@
|
||||||
import numpy as np
|
"""
|
||||||
|
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
|
||||||
|
Soruce: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
|
||||||
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
def Representational(r,g,b):
|
def psnr(original, contrast):
|
||||||
return (0.299*r+0.287*g+0.114*b)
|
mse = np.mean((original - contrast) ** 2)
|
||||||
|
if mse == 0:
|
||||||
|
return 100
|
||||||
|
PIXEL_MAX = 255.0
|
||||||
|
PSNR = 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
|
||||||
|
return PSNR
|
||||||
|
|
||||||
def calculate(img):
|
|
||||||
b,g,r = cv2.split(img)
|
|
||||||
pixelAt = Representational(r,g,b)
|
|
||||||
return pixelAt
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
#Loading images (orignal image and compressed image)
|
# Loading images (original image and compressed image)
|
||||||
orignal_image = cv2.imread('orignal_image.png',1)
|
original = cv2.imread('original_image.png')
|
||||||
compressed_image = cv2.imread('compressed_image.png',1)
|
contrast = cv2.imread('compressed_image.png', 1)
|
||||||
|
|
||||||
#Getting image height and width
|
original2 = cv2.imread('PSNR-example-base.png')
|
||||||
height,width = orignal_image.shape[:2]
|
contrast2 = cv2.imread('PSNR-example-comp-10.jpg', 1)
|
||||||
|
|
||||||
orignalPixelAt = calculate(orignal_image)
|
# Value expected: 29.73dB
|
||||||
compressedPixelAt = calculate(compressed_image)
|
print("-- First Test --")
|
||||||
|
print(f"PSNR value is {psnr(original, contrast)} dB")
|
||||||
|
|
||||||
diff = orignalPixelAt - compressedPixelAt
|
# # Value expected: 31.53dB (Wikipedia Example)
|
||||||
error = np.sum(np.abs(diff) ** 2)
|
print("\n-- Second Test --")
|
||||||
|
print(f"PSNR value is {psnr(original2, contrast2)} dB")
|
||||||
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
mid = (start + end) / 2
|
mid = (start + end) / 2
|
||||||
while abs(start - mid) > 0.0000001: # until we achieve precise equals to 10^-7
|
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
|
||||||
if function(mid) == 0:
|
if function(mid) == 0:
|
||||||
return mid
|
return mid
|
||||||
elif function(mid) * function(start) < 0:
|
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
|
||||||
def f(x):
|
def f(x):
|
||||||
return math.pow(x, 3) - 2*x - 5
|
return math.pow(x, 3) - 2*x - 5
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
print(bisection(f, 1, 1000))
|
print(bisection(f, 1, 1000))
|
||||||
|
|
|
@ -5,12 +5,13 @@ def intersection(function,x0,x1): #function is the f we want to find its root an
|
||||||
x_n1 = x1
|
x_n1 = x1
|
||||||
while True:
|
while True:
|
||||||
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
|
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
|
||||||
if abs(x_n2 - x_n1)<0.00001 :
|
if abs(x_n2 - x_n1) < 10**-5:
|
||||||
return x_n2
|
return x_n2
|
||||||
x_n=x_n1
|
x_n=x_n1
|
||||||
x_n1=x_n2
|
x_n1=x_n2
|
||||||
|
|
||||||
def f(x):
|
def f(x):
|
||||||
return math.pow(x,3)-2*x-5
|
return math.pow(x , 3) - (2 * x) -5
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
print(intersection(f,3,3.5))
|
print(intersection(f,3,3.5))
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
|
# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
def LUDecompose (table):
|
def LUDecompose (table):
|
||||||
#table that contains our data
|
# Table that contains our data
|
||||||
#table has to be a square array so we need to check first
|
# Table has to be a square array so we need to check first
|
||||||
rows,columns=numpy.shape(table)
|
rows,columns=numpy.shape(table)
|
||||||
L=numpy.zeros((rows,columns))
|
L=numpy.zeros((rows,columns))
|
||||||
U=numpy.zeros((rows,columns))
|
U=numpy.zeros((rows,columns))
|
||||||
if rows!=columns:
|
if rows!=columns:
|
||||||
return
|
return []
|
||||||
for i in range (columns):
|
for i in range (columns):
|
||||||
for j in range(i-1):
|
for j in range(i-1):
|
||||||
sum=0
|
sum=0
|
||||||
|
@ -22,13 +23,10 @@ def LUDecompose (table):
|
||||||
U[i][j]=table[i][j]-sum1
|
U[i][j]=table[i][j]-sum1
|
||||||
return L,U
|
return L,U
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
matrix =numpy.array([[2,-2,1],
|
||||||
|
[0,1,2],
|
||||||
|
[5,3,1]])
|
||||||
|
|
||||||
|
|
||||||
matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]])
|
|
||||||
L,U = LUDecompose(matrix)
|
L,U = LUDecompose(matrix)
|
||||||
print(L)
|
print(L)
|
||||||
print(U)
|
print(U)
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
|
# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method
|
||||||
|
|
||||||
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
|
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
|
||||||
x_n=startingInt
|
x_n=startingInt
|
||||||
while True:
|
while True:
|
||||||
x_n1=x_n-function(x_n)/function1(x_n)
|
x_n1=x_n-function(x_n)/function1(x_n)
|
||||||
if abs(x_n-x_n1)<0.00001:
|
if abs(x_n-x_n1) < 10**-5:
|
||||||
return x_n1
|
return x_n1
|
||||||
x_n=x_n1
|
x_n=x_n1
|
||||||
|
|
||||||
def f(x):
|
def f(x):
|
||||||
return (x**3)-2*x-5
|
return (x**3) - (2 * x) -5
|
||||||
|
|
||||||
def f1(x):
|
def f1(x):
|
||||||
return 3 * (x**2) -2
|
return 3 * (x**2) -2
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
print(newton(f,f1,3))
|
print(newton(f,f1,3))
|
||||||
|
|
|
@ -21,7 +21,7 @@ def minDist(mdist, vset, V):
|
||||||
def Dijkstra(graph, V, src):
|
def Dijkstra(graph, V, src):
|
||||||
mdist=[float('inf') for i in range(V)]
|
mdist=[float('inf') for i in range(V)]
|
||||||
vset = [False for i in range(V)]
|
vset = [False for i in range(V)]
|
||||||
mdist[src] = 0.0;
|
mdist[src] = 0.0
|
||||||
|
|
||||||
for i in range(V-1):
|
for i in range(V-1):
|
||||||
u = minDist(mdist, vset, V)
|
u = minDist(mdist, vset, V)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user