Image Blur Detection in Python using OpenCV and imutils Package

  1158 views   5 months ago Python

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.

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


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 --image test.jpg

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

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]