-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Re-design psnr.py code and change image names #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 cv2 | ||
import numpy as np | ||
|
||
def Representational(r,g,b): | ||
return (0.299*r+0.287*g+0.114*b) | ||
def psnr(original, contrast): | ||
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(): | ||
|
||
#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] | ||
# Loading images (original image and compressed image) | ||
original = cv2.imread('original_image.png') | ||
contrast = cv2.imread('compressed_image.png', 1) | ||
|
||
orignalPixelAt = calculate(orignal_image) | ||
compressedPixelAt = calculate(compressed_image) | ||
original2 = cv2.imread('PSNR-example-base.png') | ||
contrast2 = cv2.imread('PSNR-example-comp-10.jpg', 1) | ||
|
||
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)) | ||
# Value expected: 29.73dB | ||
print("-- First Test --") | ||
print(f"PSNR value is {psnr(original, contrast)} dB") | ||
|
||
# # Value expected: 31.53dB (Wikipedia Example) | ||
print("\n-- Second Test --") | ||
print(f"PSNR value is {psnr(original2, contrast2)} dB") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
|
||
main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
x_n=startingInt | ||
while True: | ||
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 | ||
x_n=x_n1 | ||
|
||
def f(x): | ||
return (x**3)-2*x-5 | ||
return (x**3) - (2 * x) -5 | ||
|
||
def f1(x): | ||
return 3*(x**2)-2 | ||
return 3 * (x**2) -2 | ||
|
||
print(newton(f,f1,3)) | ||
if __name__ == "__main__": | ||
print(newton(f,f1,3)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @rafagarciac,
I would be happy to merge this PR, but I want you to remove one of the PSNR methods and keep the only one which works better! Also, Do not forget to modify your code with respect to changes that will happen after removing the method!
Thank you.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved ! Also I reformat some code from arithmetic_analysis 👍 💯
057291f