Python+OpenCV檢測燈光亮點的實現(xiàn)方法
本篇博文分享一篇尋找圖像中燈光亮點(圖像中最亮點)的教程,例如,檢測圖像中五個燈光的亮點并標記,項目效果如下所示:
第1步:導入并打開原圖像,實現(xiàn)代碼如下所示:
# 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步:開始檢測圖像中最亮的區(qū)域,首先需要從磁盤加載圖像,然后將其轉換為灰度圖并進行平滑濾波,以減少高頻噪聲,實現(xiàn)代碼如下所示:
#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步:閾值化處理,為了顯示模糊圖像中最亮的區(qū)域,將像素值p >= 200,設置為255(白色),像素值< 200,設置為0(黑色),實現(xiàn)代碼如下所示:
# threshold the image to reveal light regions in the # blurred image thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]
效果如下所示:
第4步:此時可看到圖像中存在噪聲(小斑點),所以需要通過腐蝕和膨脹操作來清除,實現(xiàn)代碼如下所示:
# 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步:本項目的關鍵步驟是對上圖中的每個區(qū)域進行標記,即使在應用了腐蝕和膨脹后,仍然想要過濾掉剩余的小塊兒區(qū)域。一個很好的方法是執(zhí)行連接組件分析,實現(xiàn)代碼如下所示:
# 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庫執(zhí)行實際的連接組件分析。measure.lable返回的label和閾值圖像有相同的大小,唯一的區(qū)別就是label存儲的為閾值圖像每一斑點對應的正整數(shù)。
然后在第5行初始化一個掩膜來存儲大的斑點。
第7行開始循環(huán)遍歷每個label中的正整數(shù)標簽,如果標簽為零,則表示正在檢測背景并可以安全的忽略它(9,10行)。否則,為當前區(qū)域構建一個掩碼。
下面提供了一個GIF動畫,它可視化地構建了每個標簽的labelMask。使用這個動畫來幫助你了解如何訪問和顯示每個單獨的組件:
第15行對labelMask中的非零像素進行計數(shù)。如果numPixels超過了一個預先定義的閾值(在本例中,總數(shù)為300像素),那么認為這個斑點“足夠大”,并將其添加到掩膜中。輸出掩模如下圖所示:
第6步:此時圖像中所有小的斑點都被過濾掉了,只有大的斑點被保留了下來。最后一步是在的圖像上繪制標記的斑點,實現(xiàn)代碼如下所示:
# 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)
最后運行程序,可實現(xiàn)燈光亮點的檢測和標記,每個燈泡都被獨特地標上了圓圈,圓圈圍繞著每個單獨的明亮區(qū)域,效果如下所示:
本文來源于:Detecting multiple bright spots in an image with Python and OpenCV
到此這篇關于Python+OpenCV檢測燈光亮點的實現(xiàn)方法的文章就介紹到這了,更多相關OpenCV 檢測燈光亮點內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)多條件篩選目標數(shù)據功能【測試可用】
這篇文章主要介紹了Python實現(xiàn)多條件篩選目標數(shù)據功能,結合實例形式總結分析了Python3使用內建函數(shù)filter、pandas包以及for循環(huán)三種方法對比分析了列表進行條件篩選操作相關實現(xiàn)技巧與運行效率,需要的朋友可以參考下2018-06-06詳解Python 定時框架 Apscheduler原理及安裝過程
Apscheduler是一個非常強大且易用的類庫,可以方便我們快速的搭建一些強大的定時任務或者定時監(jiān)控類的調度系統(tǒng),這篇文章主要介紹了Python 定時框架 Apscheduler ,需要的朋友可以參考下2019-06-06Python Pandas實現(xiàn)DataFrame合并的圖文教程
我們在使用pandas處理數(shù)據的時候,往往會需要合并兩個或者多個DataFrame的操作,下面這篇文章主要給大家介紹了關于Pandas實現(xiàn)DataFrame合并的相關資料,需要的朋友可以參考下2022-07-07python實現(xiàn)給微信公眾號發(fā)送消息的方法
這篇文章主要介紹了python實現(xiàn)給微信公眾號發(fā)送消息的方法,結合實例形式分析了Python針對微信公眾號接口操作的相關技巧,需要的朋友可以參考下2017-06-06Pycharm虛擬環(huán)境創(chuàng)建并使用命令行指定庫的版本進行安裝
Pycharm創(chuàng)建的項目,使用了虛擬環(huán)境,對庫的版本進行管理,有些項目的對第三方庫的版本要求不同,可使用虛擬環(huán)境進行管理,直接想通過pip命令安裝可以參考下本文的操作步驟2022-07-07