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)
導入亮燈圖像,過濾后效果如下所示:
第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]
效果如下所示:
?第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)
此時“干凈”的圖像如下所示:
第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 itif label == 0:continue# otherwise, construct the label mask and count the# number of pixels labelMask = np.zeros(thresh.shape, dtype="uint8")labelMask[labels == label] = 255numPixels = 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。使用這個動畫來幫助你了解如何訪問和顯示每個單獨的組件:
第15行對labelMask中的非零像素進行計數。如果numPixels超過了一個預先定義的閾值(在本例中,總數為300像素),那么認為這個斑點“足夠大”,并將其添加到掩膜中。輸出掩模如下圖所示:
第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)
?最后運行程序,可實現燈光亮點的檢測和標記,每個燈泡都被獨特地標上了圓圈,圓圈圍繞著每個單獨的明亮區域,效果如下所示:
本文來源于:Detecting multiple bright spots in an image with Python and OpenCV
總結
以上是生活随笔為你收集整理的Python+OpenCV检测灯光亮点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LDO和DC-DC有什么不同?如何选型?
- 下一篇: 用栈、回溯算法设计迷宫程序