How to perform Blur Detection using OpenCV in Python

Guest Blog Last Updated : 14 Sep, 2020
4 min read

Introduction

Blur Detection

Blur detection

1  1 11 -8 11  1 1
4 5 63 3 82 1 7
4 5 63 12 82 1 7

Blur detection

Image for post

import cv2
import argparse
import glob

Image for post

ap = argparse.ArgumentParser()
ap.add_argument('-i', '--images', required=True,)
ap.add_argument('-t', '--threshold', type=float)
args = vars(ap.parse_args())

To keep the images in the folder in a single array, we write the following code:

images = [cv2.imread(file) for file in glob.glob("{}/*.jpeg".format(args['images']))]

Now it is time to take the pictures in the folder one by one and apply the Laplacian method to find blur.

for image in images:
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	fm = cv2.Laplacian(gray, cv2.CV_64F).var()
	text = "Not Blurry"

	if fm < args["threshold"]:
		text = "Blurry"

	cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
		cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
	cv2.imshow("Image", image)
	cv2.waitKey(0)
python blur_detection.py -i images -t 100

Author - Blur detection

Muhammed Furkan Gülşen

I have been a web developer for over 5 years. First, I continued as a Frontend developer. Later, I learned Backend and continued to improve myself as a FullStack developer. During this period, I managed projects as a Project Manager in the Facebook Developer Circle community. After doing this job for about 1 year, I received an offer from a company and switched to that company. I have been working in this company for more than 10 months, both as a full-stack developer and as a Data Scientist.While this process was going on (for the last 2 years) I started data science. Later the process continued as machine learning, deep learning, image processing, and finally Computer Vision.

Responses From Readers

Clear

ganesh
ganesh

hI Model and final line "python blur_detection.py -i images -t 100" , everything is running perfect in console but no result is coming ? output link is not opening ? how you open the output link ?

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details