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

embedded/2024/9/24 16:13:47/

参考:
使用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)

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


http://www.ppmy.cn/embedded/112497.html

相关文章

字符函数与字符串函数

目录 一、字符分类函数 二.字符转换函数 三.字符串函数 一、字符分类函数 C语言中有一系列的函数是专门做字符分类的&#xff0c;也就是一个字符是属于什么类型的字符的&#xff0c;这些函数的使用都需要包括一个头文件是 ctype.h isalnum 检查字符是否为字母数字&#xf…

SpringBoot用kafka.listener监听接受Kafka消息

1.创建kafka监听配置并进行注册 import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.common.serialization.StringDeserializer; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation…

【鸿蒙OH-v5.0源码分析之 Linux Kernel 部分】005 - Kernel 入口 C 函数 start_kernel() 源码分析

【鸿蒙OH-v5.0源码分析之 Linux Kernel 部分】005 - Kernel 入口 C 函数 start_kernel 源码分析 系列文章汇总:《鸿蒙OH-v5.0源码分析之 Uboot+Kernel 部分】000 - 文章链接汇总》 本文链接:《【鸿蒙OH-v5.0源码分析之 Linux Kernel 部分】005 - Kernel 入口 C 函数 start_ke…

【Go】Go语言基本语法--注释、变量、常量

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…

python爬虫基础:了解html

编辑器vscode <!DOCTYPE html> <html><head><title>第一个网页</title></head><body><h1>字体</h1><h2>字体</h2><h3>字体</h3><p>Lorem, ipsum dolor sit amet consectetur adipisicing…

【系统架构设计师-2015年真题】案例分析-答案及详解

更多内容请见: 备考系统架构设计师-核心总结索引 文章目录 【材料1】问题1问题2【材料2】问题1问题2问题3【材料3】问题1问题2问题3【材料4】问题1问题2问题3【材料5】问题1问题2问题3【材料1】 阅读以下关于软件架构评估的说明,在答题纸上回答问题1和问题2。 【说明】某软件…

Redisson 分布式锁的使用详解

一、分布式锁的概述 1.1 分布式锁的背景 在单机系统中&#xff0c;Java 提供了 synchronized 和 Lock 等锁机制来确保并发情况下的线程安全。然而&#xff0c;在分布式系统中&#xff0c;多个服务实例运行在不同的物理或虚拟机上&#xff0c;无法直接使用这些本地的锁机制来控…

深兰科技董事长陈海波应出席“香港大学国际科创大赛”

近日&#xff0c;以“人工智能与智能制造”为主题的“香港大学国际科创大赛——知识转化论坛(沪港场)”在上海市普陀区隆重举行&#xff0c;众多来自人工智能与智能制造领域的专家学者与企业高管齐聚一堂&#xff0c;共话人工智能、智能制造与新质生产力的深度融合、最新进展以…