From c81ca9f3119e1c97c4e975aa0ab16c4fce87edd2 Mon Sep 17 00:00:00 2001 From: narugo1992 Date: Thu, 2 May 2024 22:21:35 +0800 Subject: [PATCH] dev(narugo): add laplacian score --- imgutils/metrics/__init__.py | 1 + imgutils/metrics/laplacian.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 imgutils/metrics/laplacian.py diff --git a/imgutils/metrics/__init__.py b/imgutils/metrics/__init__.py index 487e7679c60..5bdf3b5b4b7 100644 --- a/imgutils/metrics/__init__.py +++ b/imgutils/metrics/__init__.py @@ -5,5 +5,6 @@ from .aesthetic import * from .ccip import * from .dbaesthetic import * +from .laplacian import * from .lpips import * from .psnr_ import * diff --git a/imgutils/metrics/laplacian.py b/imgutils/metrics/laplacian.py new file mode 100644 index 00000000000..bf14f712c30 --- /dev/null +++ b/imgutils/metrics/laplacian.py @@ -0,0 +1,35 @@ +import cv2 +import numpy as np + +from ..data import load_image, ImageTyping + +__all__ = [ + 'laplacian_score' +] + + +def _variance_of_laplacian(d_image: np.ndarray): + """ + Calculate the variance of Laplacian for a given image. + + :param d_image: The input image as a numpy array. + :type d_image: np.ndarray + :return: The variance of Laplacian. + :rtype: float + """ + return cv2.Laplacian(d_image, cv2.CV_64F).var() + + +def laplacian_score(image: ImageTyping) -> float: + """ + Calculate the Laplacian score for the given image. + + The Laplacian score is a measure of image bluriness. + + :param image: The input image. + :type image: ImageTyping + :return: The Laplacian score. + :rtype: float + """ + v = np.array(load_image(image, force_background='white', mode='L')) + return _variance_of_laplacian(v).item()