python - Python检测高斯模糊
问题描述
我有以下代码来检测输入图像是否模糊。
from imutils import paths
import argparse
import cv2
import os
def variance_of_laplacian(image):
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
return cv2.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
for imagePath in paths.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"
if fm < args["threshold"]:
text = "Blurry"
要使用该脚本,我运行以下命令:
python detect_blur.py --images images
images
包含一系列照片的文件夹名称在哪里。
然而,结果相当不准确(假设拉普拉斯值 < 100 被归类为模糊):
它检测到这张照片是模糊的(正确,拉普拉斯值 = 1.26):
但它也检测到这张照片是模糊的(不正确,拉普拉斯值 = 62.9):
如何让代码更准确?我想专门检测高斯模糊,如何更改代码?
解决方案
第二张图片,嗯,客观地说,除了最底部之外,它是模糊的。
因此,如果将每个输入图像细分为 25 块(即 5 x 5 网格)并计算每个图像的焦点值,则可能会获得更好的结果。然后,您可以使用例如焦点的最大值(或其他度量,例如平均值或中值,如果这对您更有效?)作为完整图像的焦点值。
推荐阅读
- jsp - 如何从 AEM 的 Java 文件中获取给定再现的资产路径?
- uitableview - UITableView 没有出现在 iPhone 中
- c# - c# Naaudio音频电平捕获和显示,仅在打开录音属性时有效
- javascript - 多语言验证处理逻辑
- javascript - 如何动态更改 logoURL?
- python - 在python中对不同的列进行分组
- jquery - 如何通过下拉列表的变化根据数据属性对div进行排序
- qt - Qt QTreeView indexBelow 不起作用
- ios - 无法通过whatsapp分享pdf
- javascript - 为什么无论代码中的顺序如何,alert() 对话框都会首先显示?