Skip to content

Commit

Permalink
dev(narugo): add laplacian score
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed May 2, 2024
1 parent 3441e93 commit c81ca9f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions imgutils/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
from .aesthetic import *
from .ccip import *
from .dbaesthetic import *
from .laplacian import *
from .lpips import *
from .psnr_ import *
35 changes: 35 additions & 0 deletions imgutils/metrics/laplacian.py
Original file line number Diff line number Diff line change
@@ -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()

Check warning on line 20 in imgutils/metrics/laplacian.py

View check run for this annotation

Codecov / codecov/patch

imgutils/metrics/laplacian.py#L20

Added line #L20 was not covered by tests


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()

Check warning on line 35 in imgutils/metrics/laplacian.py

View check run for this annotation

Codecov / codecov/patch

imgutils/metrics/laplacian.py#L34-L35

Added lines #L34 - L35 were not covered by tests

0 comments on commit c81ca9f

Please sign in to comment.