Table of contents
3. Quantitative indicators of clarity
SMD (grayscale variance) function
SMD2 (grayscale variance product) function
4. Image clarity evaluation implementation
0. Realize the effect
Able to evaluate the clarity of photos taken by the same set of cameras through a standard image.
1 Overview
Image sharpness is an evaluation function used to guide the focusing mechanism to find the correct focus position. The ideal clarity evaluation curve is similar to the Poisson distribution, please see the figure below:
Point p corresponds to the positive focus position, and P1 and P2 are the sharpness evaluation results of images collected before and after focus at the positive focus position.
The edges of a focused image are sharper and clearer than a blurred out-of-focus image. The gray value of the corresponding edge pixel changes greatly, so there will be a larger gradient value. From a mathematical point of view, the image is two-dimensional and discrete. Matrix, the gradient function can be used to obtain the grayscale information of the image to determine the clarity of the image. In the discrete signal, the gradient is expressed in a differential form.
2. Fuzziness classification
1. Motion blur
Motion blur is the brief relative movement of the imaging system and the subject during the opening time of the shutter when capturing an image, causing blur in a certain direction of the image.
2. Compression blur
Compression blur is the loss of part of the image information during lossy compression.
3. Gaussian blur
Gaussian blur is a kind of artificially introduced blur, which is obtained by filtering the original image using a Gaussian low-pass filter.
3. Quantitative indicators of clarity
Brenner
The Brenner gradient function is the simplest gradient evaluation function. It simply calculates the square of the gray level difference between two adjacent pixels. The function is defined as follows:
f(x,y) represents the gray value of the corresponding pixel point (x,y) of image f, and D(f) is the calculation result of image sharpness.
python implementation:
def brenner(img):
'''
:param img:narray 二维灰度图像
:return: int 图像越清晰越大
'''
shapes = np.shape(img)
output = 0
for x in range(0, shapes[0]-2):
for y in range(0, shapes[1]):
output+=(int(img[x+2,y])-int(img[x,y]))**2
return output
Energy of Gradient
The sum of the squares of the difference between the grayscale values of adjacent pixels in the x direction and the y direction is used as the gradient value of each pixel, and the gradient values of all pixels are accumulated as the sharpness evaluation function value. The expression is as follows:
python implementation:
def EOG(img):
'''
:param img:narray 二维灰度图像
:return: int 图像越清晰越大
'''
shapes = np.shape(img)
output = 0
for x in range(0, shapes[0]-1):
for y in range(0, shapes[1]-1):
output+=((int(img[x+1,y])-int(img[x,y]))**2+(int(img[x,y+1])-int(img[x,y]))**2)
return output
Roberts
The Roberts function is similar to the energy gradient function, which uses the difference in gray value of pixels in the diagonal direction. The sum of the squares of the cross-subtraction of the grayscale values of four adjacent pixels is used as the gradient value of each pixel, and the gradient values of all pixels are accumulated as the sharpness evaluation function value. The expression is as follows:
python implementation:
def Roberts(img):
'''
:param img:narray 二维灰度图像
:return: int 图像越清晰越大
'''
shapes = np.shape(img)
output = 0
for x in range(0, shapes[0]-1):
for y in range(0, shapes[1]-1):
output+=((int(img[x+1,y+1])-int(img[x,y]))**2+(int(img[x+1,y])-int(img[x,y+1]))**2)
return output
Laplace
The Laplace operator is used to convolve with the gray value of each pixel in the image to obtain a gradient matrix, which is recorded as: The square sum of the gradients of each pixel is taken as the evaluation function, as shown in the following formula:
python implementation:
def Laplacian(img):
'''
:param img:narray 二维灰度图像
:return: int 图像越清晰越大
'''
return cv2.Laplacian(img,cv2.CV_64F).var()
Since it has been deduced before, you can check this article, which is a blog I wrote before: (2 messages) Image Processing: Principle of Edge Detection_Summer is Iced Tea Blog-CSDN Blog
SMD (grayscale variance) function
When fully focused, the image is the clearest and the high-frequency components in the image are the most, so the grayscale change can be used as the basis for focus evaluation. The formula of the grayscale variance method is as follows:
python implementation:
def SMD(img):
'''
:param img:narray 二维灰度图像
:return: int 图像越清晰越大
'''
shape = np.shape(img)
output = 0
for x in range(1, shape[0]-1):
for y in range(0, shape[1]):
output+=math.fabs(int(img[x,y])-int(img[x,y-1]))
output+=math.fabs(int(img[x,y]-int(img[x+1,y])))
return output
SMD2 (grayscale variance product) function
The SDM function has good computational performance, but its shortcomings are also obvious, that is, the sensitivity is not high near the focus, that is, the function is too flat near the extreme point, making it difficult to improve the focus accuracy. In " A Fast and Highly Sensitive Focusing Evaluation Function ", Li Yufeng and others proposed a new evaluation function in the paper, called the gray-level variance product method, that is, after multiplying the two gray-level differences in each pixel area, Then accumulated pixel by pixel, the function is defined as follows:
python implementation:
def SMD2(img):
'''
:param img:narray 二维灰度图像
:return: int 图像约清晰越大
'''
shape = np.shape(img)
output = 0
for x in range(0, shape[0]-1):
for y in range(0, shape[1]-1):
output+=math.fabs(int(img[x,y])-int(img[x+1,y]))*math.fabs(int(img[x,y]-int(img[x,y+1])))
return output
4. Image clarity evaluation implementation
SWD2:
import cv2
import pyps.pyzjr.definition as din
import pyps.pyzjr.utility as ult
image=ult.read_resize_image("./compare/8881.jpg",space=True)
img = din.SMD2(image)
print(img)
cv2.putText(image, f"definition:{img:.2f}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
cv2.imshow("definition_Image", image)
cv2.waitKey(0)
Brenner:
Laplacian:
5. Summary and evaluation
Unfortunately, this time the definition evaluation failed. It is almost impossible to obtain a boundary value, even through a large amount of image data (blurred images and clear images), because these definition quantitative index functions are all passed To find the gradient of image pixels, it is difficult to compare under different blur conditions in different scenes.
References:
Image sharpness evaluation function - Zhihu (zhihu.com)
Blurry image detection - Clarity evaluation of no reference image - Zhihu (zhihu.com)
(2 messages) Clarity evaluation method for images without reference_Lingfeng Tanmei’s Blog-CSDN Blog
(2 messages) Image processing: Principle of edge detection_Summer is Iced Tea Blog-CSDN Blog
Image processing evaluation indexes of blurriness and clarity (to be updated) - Zhihu (zhihu.com)