欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > 使用OpenCV进行模糊检测(拉普拉斯算子)

使用OpenCV进行模糊检测(拉普拉斯算子)

2024/10/25 6:28:33 来源:https://blog.csdn.net/WhiffeYF/article/details/142178420  浏览:    关键词:使用OpenCV进行模糊检测(拉普拉斯算子)

参考:
使用OpenCV进行模糊检测(拉普拉斯算子)

代码:

# import the necessary packages
from imutils import paths
import argparse
import cv2
import osdef variance_of_laplacian(image):# compute the Laplacian of the image and then return the focus# measure, which is simply the variance of the Laplacianreturn 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("-o", "--output", required=True, help="path to output directory for saving results")
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())# create output directories if they don't exist
blurry_dir = os.path.join(args["output"], "blurry")
not_blurry_dir = os.path.join(args["output"], "not_blurry")
os.makedirs(blurry_dir, exist_ok=True)
os.makedirs(not_blurry_dir, exist_ok=True)# 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# methodimage = 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"outputPath = os.path.join(blurry_dir, os.path.basename(imagePath))else:outputPath = os.path.join(not_blurry_dir, os.path.basename(imagePath))# annotate and save the imagecv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)cv2.imwrite(outputPath, image)print("Processing complete. Blurry images saved to", blurry_dir)
print("Not blurry images saved to", not_blurry_dir)

结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com