# reload library
%load_ext autoreload
%autoreload 2
import numpy as np
import cv2
from matplotlib import pyplot as plt
np.set_printoptions(precision=3)
np.set_printoptions(suppress=True)
def blurry(image, threshold=100.0):
"""
Given an image and threshold, returns if image is blurry and its value
Args:
image: opencv grayscale image
threshold: blur threshold value, below this value an image is considered
to be blurry
Return:
blurry: True/False
value: numeric value of blurriness
Reference: https://pyimagesearch.com/2015/09/07/blur-detection-with-opencv/
"""
val = cv2.Laplacian(image, cv2.CV_64F).var()
val = int(val)
if val < threshold:
return True, val
return False, val
cat = cv2.imread("cat-blurry.webp",0)
plt.imshow(cat, cmap="gray")
plt.axis("off")
plt.title(f"Blurry: {blurry(cat, 300)}");
cat = cv2.imread("cat-crisp.webp",0)
plt.imshow(cat, cmap="gray")
plt.axis("off")
plt.title(f"Blurry: {blurry(cat, 300)}");