Skip to content
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

Fix psnr and corresponding test cases and rename variables (BugFix) #1487

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions checkbox-support/checkbox_support/scripts/psnr.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,28 @@ def psnr_args() -> argparse.ArgumentParser:
return parser.parse_args()


def _get_psnr(I1: np.ndarray, I2: np.ndarray) -> float:
def _get_psnr(ref_img: np.ndarray, target_img: np.ndarray) -> float:
"""
Calculate the Peak Signal-to-Noise Ratio (PSNR) between two frames.

Args:
I1 (np.ndarray): Reference frame.
I2 (np.ndarray): Frame to be compared with the reference.
ref_img (np.ndarray): Reference frame.
target_img (np.ndarray): Frame to be compared with the reference.

Returns:
float: PSNR value indicating the quality of I2 compared to I1.
float: PSNR value indicating the quality of target_img compared to ref_img.
"""
# Calculate the absolute difference
s1 = cv2.absdiff(I1, I2)
s1 = cv2.absdiff(ref_img, target_img)
# cannot make a square on 8 bits
s1 = np.float32(s1)
# Calculate squared differences
s1 = s1 * s1
# Sum of squared differences per channel
# Sum of squared differences
sse = s1.sum()
# sum channels
if sse <= 1e-10:
# for small values return zero
return 0.0
else:
shape = I1.shape
mse = 1.0 * sse / (shape[0] * shape[1] * shape[2])
psnr = 10.0 * np.log10((255 * 255) / mse)
shape = ref_img.shape
mse = 1.0 * sse / (shape[0] * shape[1] * shape[2])
psnr = 10.0 * np.log10((255 * 255) / mse)
return psnr


Expand Down
6 changes: 3 additions & 3 deletions checkbox-support/checkbox_support/tests/test_psnr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import patch, MagicMock
from argparse import Namespace
from io import StringIO
from math import inf

from checkbox_support.scripts.psnr import (
main,
Expand Down Expand Up @@ -60,13 +61,12 @@ def create_image(self, width, height, color):
def test_identical_images(self):
img1 = self.create_image(100, 100, 255)
img2 = self.create_image(100, 100, 255)
self.assertEqual(_get_psnr(img1, img2), 0.0)
self.assertEqual(_get_psnr(img1, img2), np.float32(inf))

def test_different_images(self):
img1 = self.create_image(100, 100, 255)
img2 = self.create_image(100, 100, 0)
self.assertNotEqual(_get_psnr(img1, img2), 0.0)
self.assertLessEqual(_get_psnr(img1, img2), 50.0)
self.assertEqual(_get_psnr(img1, img2), 0.0)

def test_similar_images(self):
img1 = self.create_image(100, 100, 125)
Expand Down
Loading