from typing import Tuple import numpy as np import cv2 from config import BlurrinessConfig class BlurrinessEstimator: def __init__(self, cfg: BlurrinessConfig): """ Checks if an image is blurry or not, in respect to a user-defined threshold. The metric used here is the variance of the laplacian. Args: cfg: config for blurriness """ self.threshold = cfg.threshold def __call__(self, img: np.ndarray) -> Tuple[bool, float]: """ Compute the Laplacian of the image and then return the variance of the Laplacian, which is a decent indicator of image blur Args: img (np.ndarray): input image """ laplacian = cv2.Laplacian(img, cv2.CV_64F) variance_of_laplacian = laplacian.var() is_blurry = variance_of_laplacian < self.threshold return is_blurry, variance_of_laplacian if __name__ == "__main__": blur = BlurrinessEstimator(BlurrinessConfig()) img = cv2.imread('data/104_IPHONE-SE_F.jpg') print(blur(img))