I am currently implementing a feature for scoring the clarity of images, which requires grading the sharpness of pictures. I found some information online and discovered two methods to achieve this: one is using the Laplacian
algorithm, and the other is using the Sobel
algorithm. I would like to ask what are the differences between these two algorithms? Which one is more suitable for my scenario?
Most articles recommend using the Laplacian
algorithm, why is that?
Moreover, I noticed that when using the Laplacian
algorithm, the code is very simple, which seems somewhat unbelievable to me. Can this algorithm really score image clarity?
There’s another question; some answers mentioned that if variance falls below ‘100’, it can be considered as blurry. How did they come up with this ‘100’? Is it accurate?
Here’s my code (openCV-python4.9.0):
import cv2
def laplacian_score(image_path):
# Read the image
image = cv2.imread(image_path)
# Convert to grayscale image.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform edge detection using the Laplace operator.
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
# Calculate the variance of the Laplace operator.
variance = laplacian.var()
cv2.imshow("Demo", gray)
cv2.waitKey(0)
# Set the threshold
threshold = 100
# Determine whether the image is blurry.
if variance > threshold:
print("The image is not blurry, Laplacian operator variance:", variance)
else:
print("The image is blurry, Laplacian operator variance:", variance)