一. 模糊图像评价基础知识
图像质量评价(IQA),根据参考图片(reference image),即原始图片的存在与否,可分为:
- 全参考(full-reference)方法, 有原始图片的全部信息
- 半参考(reduced-reference)方法, 只有原始图片的部分信息
- 无参考(no-reference)方法,没有原始图片
模糊图片分类
- defocus blur image 散焦模糊图像
- motion blur image 运动模糊图像
图像模糊原因:
- 确定性因素:包括成像系统调焦不当、目标物体相对运动等
- 随机性因素:主要是图像在记录、传输过程中的污染,比如电子系统的高频性能不好造成图像高频分量的损失。
在频域,当一幅图像的高频部分被削弱时图像看起来会显得模糊。
在空域,图像的边界和细节部分不清晰时,图像看起来也会显得模糊。
二. 无参考图片算法实现(Python)
import cv2
import os
import time
class Laplacian :
def __init__(self, image_dir, recursion = False) :
self.__image_dir = image_dir
self.__image_paths = []
for root_dir, sub_dirs, images in os.walk(image_dir, recursion) :
for image in images :
self.__image_paths.append(os.path.join(root_dir, image))
def __read_image(self, image_path) :
# In the case of color images, the decoded images will have the channels stored in **B G R** order.
# When using IMREAD_GRAYSCALE, the codec's internal grayscale conversion will be used, if available.
# Results may differ to the output of cvtColor()
return cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
def __pre_process_image(self, image_path, **kwargs) :
image = self.__read_image(image_path)
if kwargs.get('width') and kwargs.get('height') :
image = cv2.resize(image, (kwargs.get('width'), kwargs.get('height')))
return image
def get_image_paths(self) :
return self.__image_paths
def blur_detect(self, image_path) :
cv_image = self.__pre_process_image(image_path, width = 200, height = 200)
return cv2.Laplacian(cv_image, cv2.CV_64F).var()
def test_laplacian() :
laplacian = Laplacian('dir of images that you want to detect with laplacian', False)
image_paths = laplacian.get_image_paths()
for path in image_paths :
begin = time.time() * 1000
score = laplacian.blur_detect(path)
end = time.time() * 1000
print("Laplacian detection {} value is {}, cost time {} ms".format(path, int(score), end - begin))
if __name__ == "__main__" :
test_laplacian()
代码核心是OpenCV提供的接口cv2.Laplacian,
def Laplacian(src, ddepth, dst=None, ksize=None, scale=None, delta=None, borderType=None):
“”"
Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]]) -> dst
. @brief Calculates the Laplacian of an image.
.
. The function calculates the Laplacian of the source image by adding up the second x and y
. derivatives calculated using the Sobel operator:
.
. \f[\texttt{dst} = \Delta \texttt{src} = \frac{\partial^2 \texttt{src}}{\partial x^2} + \frac{\partial^2 \texttt{src}}{\partial y^2}\f]
.
. This is done whenksize > 1
. Whenksize == 1
, the Laplacian is computed by filtering the image
. with the following \fKaTeX parse error: Undefined control sequence: \f at position 11: 3 \times 3\̲f̲ aperture:
.
. \f[\vecthreethree {0}{1}{0}{1}{-4}{1}{0}{1}{0}\f]
.
. @param src Source image.
. @param dst Destination image of the same size and the same number of channels as src .
. @param ddepth Desired depth of the destination image.
. @param ksize Aperture size used to compute the second-derivative filters. See #getDerivKernels for
. details. The size must be positive and odd.
. @param scale Optional scale factor for the computed Laplacian values. By default, no scaling is
. applied. See #getDerivKernels for details.
. @param delta Optional delta value that is added to the results prior to storing them in dst .
. @param borderType Pixel extrapolation method, see #BorderTypes
. @sa Sobel, Scharr
“”"
pass
可以在__pre_process_image中进行图像的预处理,统一图像尺寸,类似于归一化的作用。
三. 算法评价
尝试用这个算法用于人脸识别的预处理步骤,判断人脸图片是否模糊。实际测试结果来看,单用拉普拉斯算子进行卷积运算,无法很好的评价人脸是否模糊。
目前从我测试的情况看,下面两篇参考文章提到的基于二次模糊的清晰度算法(ReBlur) 比较推荐使用。