mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
"""
|
|
Peak signal-to-noise ratio - PSNR
|
|
https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
|
|
Source:
|
|
https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python
|
|
"""
|
|
|
|
import math
|
|
import os
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
|
|
def psnr(original: float, contrast: float) -> float:
|
|
mse = np.mean((original - contrast) ** 2)
|
|
if mse == 0:
|
|
return 100
|
|
PIXEL_MAX = 255.0 # noqa: N806
|
|
PSNR = 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) # noqa: N806
|
|
return PSNR
|
|
|
|
|
|
def main() -> None:
|
|
dir_path = os.path.dirname(os.path.realpath(__file__))
|
|
# Loading images (original image and compressed image)
|
|
original = cv2.imread(os.path.join(dir_path, "image_data/original_image.png"))
|
|
contrast = cv2.imread(os.path.join(dir_path, "image_data/compressed_image.png"), 1)
|
|
|
|
original2 = cv2.imread(os.path.join(dir_path, "image_data/PSNR-example-base.png"))
|
|
contrast2 = cv2.imread(
|
|
os.path.join(dir_path, "image_data/PSNR-example-comp-10.jpg"), 1
|
|
)
|
|
|
|
# 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()
|