Image blur detection in Python using OpenCV and imutils package

imutils is Python basic image processing functional package to do image translation, rotation, resizing, skeletonization, or blur amount detection. imutils also check to find functions if you already have NumPy, SciPy, Matplotlib, and OpenCV installed.

In this article, we will check the image blur amount. This is used to verify image quality.

We will use PIP to install the package. If you haven't already installed PIP, then follow this article to install PIP.

The next step is to install OpenCV package that will process the image. Run the below command to install it.

pip3 install opencv-python

We will also need Numpy package to work with array data. Install Numpy with following command.

pip3 install numpy

Now install imutils with below command:

pip3 install imutils

Now we have installed all packages that we required. Create file blur_detection.py and add the below code.

from imutils import paths
import argparse
import cv2
import sys

def variance_of_laplacian(image):
    return cv2.Laplacian(image, cv2.CV_64F).var()

# image path
imagePath = sys.argv['image']    
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = variance_of_laplacian(gray)
text = "Not Blur"

# check image quality
if fm < 100:
    text = "Blur image"    

print text

Explanation

variance_of_laplacian() function takes one argument of image and returns 3X3 laplacian variance. We will pass imagePath with argument. The default blur threshold value we set is 100. You can set it according to image you want to test. If the value will be lower than 100, we will print image as Blur.

Now run Python file in Terminal using python command

python blur_detection.py --image test.jpg

This will print variable text whether the image is blur or not. Thank you giving time to read the article.

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment