opencv学习—亮点检测(python)

opencv学习—亮点检测(python)

目录

opencv学习—亮点检测(python)

 1、输入原图像

2、原图像灰度化

3、平滑滤波

4、阈值分割

5、形态学运算移除斑点

6、标记斑点-轮廓检测-输出图像

完整Code

鸣谢


为啥叫亮点检测,因为该方法是用来检测一张图像或者视频中的亮点区域,并进行统计,举个简单的例子,一张图像中有100个发光的灯泡,因为故障原因,其中若干个灯泡发生故障,那么该如何统计有效发光的灯泡数量呢?该方法就是用来解决类似问题的。

方法流程如下:

 1、输入原图像

2、原图像灰度化

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

3、平滑滤波

blurred = cv2.GaussianBlur(gray, (11, 11), 0)

4、阈值分割

thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]

5、形态学运算移除斑点

  • 膨胀腐蚀操作
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=4)
  • 连接组件分析
labels = measure.label(thresh, neighbors=8, background=0)
mask = np.zeros(thresh.shape, dtype="uint8")
for label in np.unique(labels):
  # if this is the background label, ignore it
  if label == 0:
    continue
  labelMask = np.zeros(thresh.shape, dtype="uint8")
  labelMask[labels == label] = 255
  numPixels = cv2.countNonZero(labelMask)
  if numPixels > 300:
    mask = cv2.add(mask, labelMask)
cv2.imshow('mask',mask)
cv2.waitKey(0)

结果图:

6、标记斑点-轮廓检测-输出图像

cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
  cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = contours.sort_contours(cnts)[0]
for (i, c) in enumerate(cnts):
  (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 - 2),
    cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)

从结果图像来看检测出图像中亮点有5个。

完整Code

from imutils import contours
from skimage import measure
import numpy as np
import cv2
image = cv2.imread('E:\\car\\6.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
cv2.imshow('blurred',blurred)
cv2.waitKey(0)
thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]
cv2.imshow('thresh',thresh)
cv2.waitKey(0)
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=4)
labels = measure.label(thresh, neighbors=8, background=0)
mask = np.zeros(thresh.shape, dtype="uint8")
for label in np.unique(labels):
  if label == 0:
    continue
  labelMask = np.zeros(thresh.shape, dtype="uint8")
  labelMask[labels == label] = 255
  numPixels = cv2.countNonZero(labelMask)
  if numPixels > 300:
    mask = cv2.add(mask, labelMask)
cv2.imshow('mask',mask)
cv2.waitKey(0)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
  cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = contours.sort_contours(cnts)[0]
for (i, c) in enumerate(cnts):
  (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 - 2),
    cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)

鸣谢

参考博客

  • 11
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岁月蹉跎的一杯酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值