Detect when an image is Blurry – Opencv with Python
We’re going to learn in this video how to detect when an Image is blurry using Opencv with Python.
Let’s take two images a not blurry one and a blurry one:
What is a blurry image?
Taking a look at the two images above we can easily affirm that the second image is blurry while the first is not.
If we had to explain the “Blur” from a visual point of view, a good explanation would be that a blurry image doesn’t have clear edges.
Let’s compare the edges of the two images. I’m going to cut a portion and draw in green the edges:
As you can clearly see, in the left image the edges are really clear and we can easily draw them, while on the right one they’re not.
How to detect “Blur” on an image?
To detect the blur we could use different approaches, in general all of them are related to the sharpness of the edges of an image.
So we will focus in this tutorial on a specific Edge detection filter which is the Laplacian filter.
And the most amazing thing is that the actual blur detection can be done with just a line of code.
laplacian_var = cv2.Laplacian(img, cv2.CV_64F).var()
The line above return as value the average variance of the edges in an image. The higher the number, the sharper the edge is.
That means that we can use a threshold value and when the laplacian_var is less then the threshold we can state that the image is blurry:
if laplacian_var < 5: print("Image blurry")
Be aware that the threshold value of 5 is used is not absolute. That differs from image to image, it depends on the camera you’re using and on the content of the pictures. So you should find a good value for your image set.