评估方法实现

所有函数的具体说明都在参考文献[1]里,这里不做过多的赘述,只讨论实现
github:图像清晰度评估算法包(有示例)

1 Brenner 梯度函数

  1. def brenner(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. shape = np.shape(img)
  7. out = 0
  8. for x in range(0, shape[0]-2):
  9. for y in range(0, shape[1]):
  10. out+=(int(img[x+2,y])-int(img[x,y]))**2
  11. return out

2 Laplacian梯度函数

  1. def Laplacian(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. return cv2.Laplacian(img,cv2.CV_64F).var()

3 SMD(灰度方差)

  1. def SMD(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. shape = np.shape(img)
  7. out = 0
  8. for x in range(0, shape[0]-1):
  9. for y in range(1, shape[1]):
  10. out+=math.fabs(int(img[x,y])-int(img[x,y-1]))
  11. out+=math.fabs(int(img[x,y]-int(img[x+1,y])))
  12. return out

4 SMD2(灰度方差乘积)

  1. def SMD2(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. shape = np.shape(img)
  7. out = 0
  8. for x in range(0, shape[0]-1):
  9. for y in range(0, shape[1]-1):
  10. out+=math.fabs(int(img[x,y])-int(img[x+1,y]))*math.fabs(int(img[x,y]-int(img[x,y+1])))
  11. return out

5 方差函数

  1. def variance(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. out = 0
  7. u = np.mean(img)
  8. shape = np.shape(img)
  9. for x in range(0,shape[0]):
  10. for y in range(0,shape[1]):
  11. out+=(img[x,y]-u)**2
  12. return out

6 能量梯度函数

  1. def energy(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. shape = np.shape(img)
  7. out = 0
  8. for x in range(0, shape[0]-1):
  9. for y in range(0, shape[1]-1):
  10. out+=((int(img[x+1,y])-int(img[x,y]))**2)+((int(img[x,y+1]-int(img[x,y])))**2)
  11. return out

7 Vollath函数

  1. def Vollath(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. shape = np.shape(img)
  7. u = np.mean(img)
  8. out = -shape[0]*shape[1]*(u**2)
  9. for x in range(0, shape[0]-1):
  10. for y in range(0, shape[1]):
  11. out+=int(img[x,y])*int(img[x+1,y])
  12. return out

8 熵函数

  1. def entropy(img):
  2. '''
  3. :param img:narray 二维灰度图像
  4. :return: float 图像约清晰越大
  5. '''
  6. out = 0
  7. count = np.shape(img)[0]*np.shape(img)[1]
  8. p = np.bincount(np.array(img).flatten())
  9. for i in range(0, len(p)):
  10. if p[i]!=0:
  11. out-=p[i]*math.log(p[i]/count)/count
  12. return out

参考文献

[1] 图像清晰度的评价指标

image.png

https://blog.csdn.net/Greepex/article/details/90183018