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,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 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。使用這個動畫來幫助你瞭解如何訪問和顯示每個單獨的元件:
第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,255),3) cv2.putText(image,"#{}".format(i + 1),(x,y - 15),cv2.FONT_HERSHEY_SIMPLEX,0.45,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檢測燈光亮點的實現方法的文章就介紹到這了,更多相關OpenCV 檢測燈光亮點內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!