Python+OpenCV检测灯光亮点的实现方法


Posted in Python onNovember 02, 2020

本篇博文分享一篇寻找图像中灯光亮点(图像中最亮点)的教程,例如,检测图像中五个灯光的亮点并标记,项目效果如下所示:

Python+OpenCV检测灯光亮点的实现方法

Python+OpenCV检测灯光亮点的实现方法

第1步:导入并打开原图像,实现代码如下所示:

# import the necessary packages
from imutils import contours
from skimage import measure
import numpy as np
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
 help="path to the image file")
args = vars(ap.parse_args())

第2步:开始检测图像中最亮的区域,首先需要从磁盘加载图像,然后将其转换为灰度图并进行平滑滤波,以减少高频噪声,实现代码如下所示:

#load the image, convert it to grayscale, and blur it
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)

导入亮灯图像,过滤后效果如下所示:

Python+OpenCV检测灯光亮点的实现方法

第3步:阈值化处理,为了显示模糊图像中最亮的区域,将像素值p >= 200,设置为255(白色),像素值< 200,设置为0(黑色),实现代码如下所示:

# threshold the image to reveal light regions in the
# blurred image
thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]

效果如下所示:

Python+OpenCV检测灯光亮点的实现方法

 第4步:此时可看到图像中存在噪声(小斑点),所以需要通过腐蚀和膨胀操作来清除,实现代码如下所示:

# perform a series of erosions and dilations to remove
# any small blobs of noise from the thresholded image
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=4)

此时“干净”的图像如下所示:

Python+OpenCV检测灯光亮点的实现方法

第5步:本项目的关键步骤是对上图中的每个区域进行标记,即使在应用了腐蚀和膨胀后,仍然想要过滤掉剩余的小块儿区域。一个很好的方法是执行连接组件分析,实现代码如下所示:

# perform a connected component analysis on the thresholded
# image, then initialize a mask to store only the "large"
# components
labels = measure.label(thresh, neighbors=8, background=0)
mask = np.zeros(thresh.shape, dtype="uint8")
# loop over the unique components
for label in np.unique(labels):
 # if this is the background label, ignore it
 if label == 0:
  continue
 # otherwise, construct the label mask and count the
 # number of pixels 
 labelMask = np.zeros(thresh.shape, dtype="uint8")
 labelMask[labels == label] = 255
 numPixels = cv2.countNonZero(labelMask)
 # if the number of pixels in the component is sufficiently
 # large, then add it to our mask of "large blobs"
 if numPixels > 300:
  mask = cv2.add(mask, labelMask)

上述代码中,第4行使用scikit-image库执行实际的连接组件分析。measure.lable返回的label和阈值图像有相同的大小,唯一的区别就是label存储的为阈值图像每一斑点对应的正整数。

然后在第5行初始化一个掩膜来存储大的斑点。

第7行开始循环遍历每个label中的正整数标签,如果标签为零,则表示正在检测背景并可以安全的忽略它(9,10行)。否则,为当前区域构建一个掩码。

下面提供了一个GIF动画,它可视化地构建了每个标签的labelMask。使用这个动画来帮助你了解如何访问和显示每个单独的组件:

Python+OpenCV检测灯光亮点的实现方法

第15行对labelMask中的非零像素进行计数。如果numPixels超过了一个预先定义的阈值(在本例中,总数为300像素),那么认为这个斑点“足够大”,并将其添加到掩膜中。输出掩模如下图所示:

Python+OpenCV检测灯光亮点的实现方法

第6步:此时图像中所有小的斑点都被过滤掉了,只有大的斑点被保留了下来。最后一步是在的图像上绘制标记的斑点,实现代码如下所示:

# find the contours in the mask, then sort them from left to
# right
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
 cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = contours.sort_contours(cnts)[0]
# loop over the contours
for (i, c) in enumerate(cnts):
 # draw the bright spot on the image
 (x, y, w, h) = cv2.boundingRect(c)
 ((cX, cY), radius) = cv2.minEnclosingCircle(c)
 cv2.circle(image, (int(cX), int(cY)), int(radius),
  (0, 0, 255), 3)
 cv2.putText(image, "#{}".format(i + 1), (x, y - 15),
  cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)

 最后运行程序,可实现灯光亮点的检测和标记,每个灯泡都被独特地标上了圆圈,圆圈围绕着每个单独的明亮区域,效果如下所示:

Python+OpenCV检测灯光亮点的实现方法

Python+OpenCV检测灯光亮点的实现方法

本文来源于:Detecting multiple bright spots in an image with Python and OpenCV

到此这篇关于Python+OpenCV检测灯光亮点的实现方法的文章就介绍到这了,更多相关OpenCV 检测灯光亮点内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python使用win32com库播放mp3文件的方法
May 30 Python
使用Python的Flask框架来搭建第一个Web应用程序
Jun 04 Python
mac安装pytorch及系统的numpy更新方法
Jul 26 Python
Django JWT Token RestfulAPI用户认证详解
Jan 23 Python
pandas进行时间数据的转换和计算时间差并提取年月日
Jul 06 Python
Python传递参数的多种方式(小结)
Sep 18 Python
windows中安装Python3.8.0的实现方法
Nov 19 Python
python计算波峰波谷值的方法(极值点)
Feb 18 Python
python可迭代对象去重实例
May 15 Python
在Python3.74+PyCharm2020.1 x64中安装使用Kivy的详细教程
Aug 07 Python
Python filter()及reduce()函数使用方法解析
Sep 05 Python
Python 操作pdf pdfplumber读取PDF写入Exce
Aug 14 Python
python获取命令行参数实例方法讲解
Nov 02 #Python
Windows环境下Python3.6.8 importError: DLLload failed:找不到指定的模块
Nov 01 #Python
详解tensorflow之过拟合问题实战
Nov 01 #Python
python cookie反爬处理的实现
Nov 01 #Python
10个python爬虫入门实例(小结)
Nov 01 #Python
利用pipenv和pyenv管理多个相互独立的Python虚拟开发环境
Nov 01 #Python
Python经纬度坐标转换为距离及角度的实现
Nov 01 #Python
You might like
怎么使 Mysql 数据同步
2006/10/09 PHP
php self,$this,const,static,-&amp;gt;的使用
2009/10/22 PHP
apache+php完美解决301重定向的两种方法
2011/06/08 PHP
php守护进程 加linux命令nohup实现任务每秒执行一次
2011/07/04 PHP
php中json_encode处理gbk与gb2312中文乱码问题的解决方法
2014/07/10 PHP
thinkPHP5实现数据库添加内容的方法
2017/10/25 PHP
PHP 实现人民币小写转换成大写的方法及大小写转换函数
2017/11/17 PHP
(JS实现)MapBar中坐标的加密和解密的脚本
2007/05/16 Javascript
基于JavaScript实现继承机制之调用call()与apply()的方法详解
2013/05/07 Javascript
setInterval与clearInterval的使用示例代码
2014/01/28 Javascript
浅谈javascript事件取消和阻止冒泡
2015/05/26 Javascript
javascript事件绑定学习要点
2016/03/09 Javascript
仿iframe效果Aajx文件上传实例
2016/11/18 Javascript
微信小程序开发之数据存储 参数传递 数据缓存
2017/04/13 Javascript
JavaScript中offsetWidth的bug及解决方法
2017/05/17 Javascript
详解基于vue的移动web app页面缓存解决方案
2017/08/03 Javascript
Vue + better-scroll 实现移动端字母索引导航功能
2018/05/07 Javascript
js图数据结构处理 迪杰斯特拉算法代码实例
2019/09/11 Javascript
微信小程序实现星级评价
2019/11/20 Javascript
基于js实现数组相邻元素上移下移
2020/05/19 Javascript
Element Breadcrumb 面包屑的使用方法
2020/07/26 Javascript
解决vue打包报错Unexpected token: punc的问题
2020/10/24 Javascript
python获取各操作系统硬件信息的方法
2015/06/03 Python
python pandas 对series和dataframe的重置索引reindex方法
2018/06/07 Python
Python小进度条显示代码
2019/03/05 Python
Python_查看sqlite3表结构,查询语句的示例代码
2019/07/17 Python
tensorflow实现测试时读取任意指定的check point的网络参数
2020/01/21 Python
PyQt5实现仿QQ贴边隐藏功能的实例代码
2020/05/24 Python
python实现控制台输出颜色
2021/03/02 Python
个性化皮包、小袋、生活配件:Mon Purse
2019/03/26 全球购物
英国家电购物网站:Sonic Direct
2019/03/26 全球购物
预备党员2014全国两会学习心得体会
2014/03/10 职场文书
2015企业年终工作总结范文
2015/05/27 职场文书
公司致全体员工的感谢信
2019/06/24 职场文书
python内置进制转换函数的操作
2021/06/02 Python
win11高清晰音频管理器在哪里?win11找不到高清晰音频管理器解决办法
2022/04/08 数码科技