# 用 Opencv和Python 对汪星人做模糊检测

Jemma，一只超级甜美，极其活泼可爱的家养米格鲁猎犬，可能是家里有史以来照片最多的狗狗了。从我在她八周大的时候遇到她开始，三年以来，我已经累积了6000 多张她的照片。

Pertuz 等人讨论了很多种方法来计算“模糊度”。其中的一些方法简单明了，仅仅使用了像素灰度值的统计数据，其他的一些相比更加先进并且是基于特征的，使用了局部二值模式。

cv2.Laplacian(image, cv2.CV_64F).var()

# import the necessary packages

fromimutilsimportpaths

importargparse

importcv2

defvariance_of_laplacian(image):

# compute the Laplacian of the image and then return the focus

# measure, which is simply the variance of the Laplacian

returncv2.Laplacian(image,cv2.CV_64F).var()

# construct the argument parse and parse the arguments

ap=argparse.ArgumentParser()

ap.add_argument("-i","--images",required=True,

help="path to input directory of images")

ap.add_argument("-t","--threshold",type=float,default=100.0,

help="focus measures that fall below this value will be considered 'blurry'")

args=vars(ap.parse_args())

\$ pip intall imutils

12-17行处理命令行参数解析。第一个需要的参数是 –image，包含进行模糊度检测的图片数据集路径。

# import the necessary packages

fromimutilsimportpaths

importargparse

importcv2

defvariance_of_laplacian(image):

# compute the Laplacian of the image and then return the focus

# measure, which is simply the variance of the Laplacian

returncv2.Laplacian(image,cv2.CV_64F).var()

# construct the argument parse and parse the arguments

ap=argparse.ArgumentParser()

ap.add_argument("-i","--images",required=True,

help="path to input directory of images")

ap.add_argument("-t","--threshold",type=float,default=100.0,

help="focus measures that fall below this value will be considered 'blurry'")

args=vars(ap.parse_args())

# loop over the input images

forimagePathinpaths.list_images(args["images"]):

# load the image, convert it to grayscale, and compute the

# focus measure of the image using the Variance of Laplacian

# method

image=cv2.imread(imagePath)

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

fm=variance_of_laplacian(gray)

text="Not Blurry"

# if the focus measure is less than the supplied threshold,

# then the image should be considered "blurry"

iffm

text="Blurry"

# show the image

cv2.putText(image,"{}: {:.2f}".format(text,fm),(10,30),

cv2.FONT_HERSHEY_SIMPLEX,0.8,(,,255),3)

cv2.imshow("Image",image)

key=cv2.waitKey()

\$ python detect_blur.py --images images

http://python.jobbole.com/83702/

• 发表于:
• 原文链接http://kuaibao.qq.com/s/20180130B0B3E100?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。

2020-01-21

2020-01-21

2020-01-21

2020-01-21

2020-01-21

2020-01-21

2018-06-26

2018-05-30

2020-01-21

2020-01-21

2020-01-21

2020-01-21