Modified by SunJackson

How to perform Blur Detection using OpenCV in Python

[MEGA DISCOUNT] Get Certified in Business Analytics with 12+ Projects, 1:1 Mentorship | Limited Time Offer


Blog Archive

Learn everything about Analytics




Blog Archive

SAS Business Analyst

LeaRn Data Science on R

Data Science in Python


Data Visualization with Tableau

Data Visualization with QlikView

Interactive Data Stories with D3.js

Applied Machine Learning Beginner to Professional


Natural Language Processing (NLP) Using Python

Computer Vision using Deep Learning 2.0

More Courses

How to perform Blur Detection using OpenCV in Python


In the corona days (still not over) I would walk near my house for an hour a day and come back home. One day, when I went out for a walk again, I thought of unnecessary photos on my phone. Thousands of photos from university Whatshap groups. It would take me a long time to find unnecessary photos among them. But I had an idea to lighten this workload.

I probably have nearly ten thousand photos in my gallery. Of these, I thought at least 300–400 photos were blurry (there are 378 blurry photos in my gallery). “How can I handle this?” When I thought, it came to my mind that I was a software engineer. I immediately finished my walk and went back home and sat down at my computer and made a small project called “Blur Detection” that I will tell you below.

Part 1: Logic After about 3 hours, I had written 117 lines of code and the algorithm was not working as I wanted. Realizing that he couldn’t handle it like that, I started to think a little. I thought of a topic that had been covered in the lesson about 3 years ago: “filter of the Laplacian”.

The Laplace filter is mainly used to define the edge lines in a picture. What is meant here by the edge are the sharp color separations that usually separate objects from the background. The Laplace filter, also known as the Sharpening Filter, uses a window while operating. You can understand the event better with the short video below.

For example, below is a 3 × 3 (3 * 3) window and a coefficient value in each cell that can be used during this process:

In this window above, the pixel value in each cell is simply multiplied by the coefficient in that cell, and the results are added to the new value of the pixel in the middle. For example, consider a matrix whose current values ​​are given below:

The new value in the middle of this matrix is ​​calculated by the formula: 1 x 4 + 1 x 5 + 1 x 6 + 1 x 3 + -8 x 3 + 1 x 8 + 1 x 2 + 1 x 1 + 1 x 7 = 12

Filtered matrix:

found as. Below is a normal version of a picture with a Laplace filter applied:

Part 2: Application

First, let’s import the necessary libraries.

Let’s open a folder called “images” that contains enough photos. Photos:

Let’s go back to coding now. Let’s make the necessary adjustments with the “argparse” package.

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

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

  • 1.line: We start a loop to rotate the photos in the folder one by one.

  • 2.line:We turn the photo into grayscale.

  • 3.line: We use the Laplacian method. As a result, a float type number will be returned. (For example 4.312563 or 764.471094)

  • 6.line: We compare the result inline 3 with the threshold value we have determined. If the result is below the threshold value, we perceive it as “blurry”. In general, the threshold value gives very good results at 100.

Type the following on the console screen for it to run

And it finished. We did a little project for blur detection using OpenCV so easily.

Project Github: WebApp Github:

About the Author

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.

Related Articles

Tags : blur detection, laplace, opencv

** **Previous ArticleHow to Build a Sales Forecast using Microsoft Excel in Just 10 Minutes!

How to Build a Sales Forecast using Microsoft Excel in Just 10 Minutes!

Guest Blog

45 Questions to test a data scientist on basics of Deep Learning (along with solution)

40 Questions to test a data scientist on Machine Learning [Solution: SkillPower – Machine Learning, DataFest 2017]

Commonly used Machine Learning Algorithms (with Python and R Codes)

6 Top Tools for Analytics and Business Intelligence in 2020

40 Questions to test a Data Scientist on Clustering Techniques (Skill test Solution)

Web Scraping using Selenium with Python!

Basic Concepts of Object-Oriented Programming in Python

30 Questions to test a data scientist on Linear Regression [Solution: Skilltest – Linear Regression]

Recent Posts

How to perform Blur Detection using OpenCV in Python

How to Build a Sales Forecast using Microsoft Excel in Just 10 Minutes!

Create your first Video Face Recognition app + Bonus (Happiness Recognition)

Start Using The timeit Library in Python!

Download App

Analytics Vidhya About Us Our Team Careers Contact us

Data Science Blog Hackathon Discussions Apply Jobs

Companies Post Jobs Trainings Hiring Hackathons Advertising

Visit us





© Copyright 2013-2020 Analytics Vidhya

Privacy Policy Terms of Use Refund Policy


Data Science