图像处理实例-拉普拉斯算子图像模糊度检测
from imutils import paths
import argparse
import cv2
import sys
def variance_of_laplacian(image):
# 计算图像的拉普拉斯算子方差
return cv2.Laplacian(image, cv2.CV_64F).var()
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default='resources/cv4/images/blur_8.jpg')
ap.add_argument("-t", "--threshold", type=float, default=100.0)
args = vars(ap.parse_args())
image = cv2.imread(args['image'])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = variance_of_laplacian(gray)
if fm > args["threshold"]:
text = args['image']+" - Not Blurry: "+str(fm)
print(args['image']+" - Not Blurry: "+str(fm))
#如果聚焦度量值小于提供的阈值,
#那么图像应被视为“模糊”
if fm < args["threshold"]:
text = args['image']+" - Blurry: "+str(fm)
print(args['image']+" - Blurry: "+str(fm))