Under normal circumstances, whether the picture is clear or not is a perceptual understanding. With the same picture, you may think it is OK, while others may think it is not clear and lacks a unified standard. However, there are algorithms that can be used to quantify the sharpness of an image.

The principle of

If you’ve seen signal processing before, you know that the most straightforward way to do it is to compute the fast Fourier transform of the image, and then look at the distribution of high and low frequencies. An image can be considered blurry if it has a small amount of high frequency. However, it is very difficult to distinguish the specific threshold value of the high frequency quantity, and improper threshold value will lead to very poor results.

Beginners have what do not understand can private message me – I just organized a set of 2021 latest 0 basic introductory tutorial, selfless share, access methods: pay attention to the small series CSDN, private message: [learning materials] can get, attached: development tools and installation package, as well as the system learning roadmap.

The expectation is that a single floating point number will represent the sharpness of the image. In the 2000 International Conference on Pattern Recognition, Pech-Pacheco proposed to convolutional operation of a certain channel (usually gray value) in an image through the Laplace mask, and then calculate the standard deviation, and the value obtained can represent the image clarity.

The reason this works is because of the Laplace definition itself. It is used to measure the second derivative of an image, highlighting areas in the image where the intensity changes rapidly, much like Sobel and Scharr operators. And, like the above operator, Laplace operator is often used for edge detection. In addition, the algorithm is based on the assumption that if the image has a high variance, then it will have a wide frequency response range, representing a normal, well-focused image. However, if the image has a small variance, then it has a narrow frequency response range, which means that the number of edges in the image is small. As we all know, the blurry the picture, the fewer edges it has.

Armed with a value for sharpness, all that remains is to set the appropriate threshold. If the variance of an image is below a predefined threshold, the image is considered blurry; if the variance is above the threshold, the image is not.

In field

The principle looks more complex, involving a lot of signal ah picture processing related knowledge, let’s do the actual operation, intuitive feeling.

Since my life is short and I am the number one Python fan in my circle of friends, I choose to do it in Python, and the core code is astonishingly simple:

import cv2

def getImageVar(imgPath):

image = cv2.imread(imgPath);

img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()

return imageVar

Life is really short ah, the core code is three lines, a brief explanation.

Import cv2 uses a famous image processing library called OpenCV. I won’t go into details about the installation of OpenCV here, but it depends on NumPy.

Image = cv2.imread(imgPath) reads the image using the method provided by OpenCV. Img2gray = cv2.cvtcolor (image, cv2.color_bgr2gray) converted to grayscale image. The diagram below:

The original image looks like this:

CV2.Laplacian (Img2Gray, CV2.CV_64f) convolves the image with 3×3 Laplace operator, where CV2.CV_64f is the Laplace operator.

As we said in the principle section, the Laplace operator is often used for edge detection, so all that’s left after Laplace is the detected edge. The image above looks like this after this process:

You can see that the figure is pretty clear here.

Cv2.laplacian (img2gray, cv2.cv_64f).var() calculates the variance and returns it.

The image above was calculated at 3,170, which is the final value we used to judge clarity. So if you want to learn Python, it is necessary to listen to the teacher’s class and get Python benefits. If you want to learn Python, you can go to the teacher’s Weixin: the first group is Mengy, the second group is 7762. Put the above two groups of letters together in order, and she will arrange the learning.

You can find another one:

The original:

After doing the grayscale and the Laplace operator, you can see that the character part is not very clear.

The variance is only 530

All that remains is to determine the threshold based on the overall image quality.


Through the above practical operation, we know that the skill of this algorithm is to set an appropriate threshold. If the threshold is too low, the normal picture will be misjudged as a fuzzy picture, while the threshold is too high, the fuzzy picture will be misjudged as a normal picture. The threshold depends on the business scenario you are actually using and needs to be customized for different scenarios.

There is no real silver bullet. In addition to the need to set a threshold, some images may deliberately have a blurred or blurry background, which can easily be killed by mistake.

Such as:

The calculation looks something like this. The back is all black.

The foreground looks fine, but the background is blurry and blurry, which makes it easy to be killed by mistake.

So it is better to understand the principle, according to the actual situation to use.