Python+OpenCV檢測(cè)燈光亮點(diǎn)的實(shí)現(xiàn)方法
本篇博文分享一篇尋找圖像中燈光亮點(diǎn)(圖像中最亮點(diǎn))的教程,例如,檢測(cè)圖像中五個(gè)燈光的亮點(diǎn)并標(biāo)記,項(xiàng)目效果如下所示:


第1步:導(dǎo)入并打開原圖像,實(shí)現(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步:開始檢測(cè)圖像中最亮的區(qū)域,首先需要從磁盤加載圖像,然后將其轉(zhuǎn)換為灰度圖并進(jìn)行平滑濾波,以減少高頻噪聲,實(shí)現(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)
導(dǎo)入亮燈圖像,過濾后效果如下所示:

第3步:閾值化處理,為了顯示模糊圖像中最亮的區(qū)域,將像素值p >= 200,設(shè)置為255(白色),像素值< 200,設(shè)置為0(黑色),實(shí)現(xiàn)代碼如下所示:
# threshold the image to reveal light regions in the # blurred image thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]
效果如下所示:

第4步:此時(shí)可看到圖像中存在噪聲(小斑點(diǎn)),所以需要通過腐蝕和膨脹操作來清除,實(shí)現(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)
此時(shí)“干凈”的圖像如下所示:

第5步:本項(xiàng)目的關(guān)鍵步驟是對(duì)上圖中的每個(gè)區(qū)域進(jìn)行標(biāo)記,即使在應(yīng)用了腐蝕和膨脹后,仍然想要過濾掉剩余的小塊兒區(qū)域。一個(gè)很好的方法是執(zhí)行連接組件分析,實(shí)現(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庫(kù)執(zhí)行實(shí)際的連接組件分析。measure.lable返回的label和閾值圖像有相同的大小,唯一的區(qū)別就是label存儲(chǔ)的為閾值圖像每一斑點(diǎn)對(duì)應(yīng)的正整數(shù)。
然后在第5行初始化一個(gè)掩膜來存儲(chǔ)大的斑點(diǎn)。
第7行開始循環(huán)遍歷每個(gè)label中的正整數(shù)標(biāo)簽,如果標(biāo)簽為零,則表示正在檢測(cè)背景并可以安全的忽略它(9,10行)。否則,為當(dāng)前區(qū)域構(gòu)建一個(gè)掩碼。
下面提供了一個(gè)GIF動(dòng)畫,它可視化地構(gòu)建了每個(gè)標(biāo)簽的labelMask。使用這個(gè)動(dòng)畫來幫助你了解如何訪問和顯示每個(gè)單獨(dú)的組件:

第15行對(duì)labelMask中的非零像素進(jìn)行計(jì)數(shù)。如果numPixels超過了一個(gè)預(yù)先定義的閾值(在本例中,總數(shù)為300像素),那么認(rèn)為這個(gè)斑點(diǎn)“足夠大”,并將其添加到掩膜中。輸出掩模如下圖所示:

第6步:此時(shí)圖像中所有小的斑點(diǎn)都被過濾掉了,只有大的斑點(diǎn)被保留了下來。最后一步是在的圖像上繪制標(biāo)記的斑點(diǎn),實(shí)現(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)
最后運(yùn)行程序,可實(shí)現(xiàn)燈光亮點(diǎn)的檢測(cè)和標(biāo)記,每個(gè)燈泡都被獨(dú)特地標(biāo)上了圓圈,圓圈圍繞著每個(gè)單獨(dú)的明亮區(qū)域,效果如下所示:


本文來源于:Detecting multiple bright spots in an image with Python and OpenCV
到此這篇關(guān)于Python+OpenCV檢測(cè)燈光亮點(diǎn)的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)OpenCV 檢測(cè)燈光亮點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python+opencv實(shí)現(xiàn)高斯平滑濾波
- Python+Opencv實(shí)現(xiàn)把圖片、視頻互轉(zhuǎn)的示例
- python 基于opencv 繪制圖像輪廓
- python 基于opencv 實(shí)現(xiàn)一個(gè)鼠標(biāo)繪圖小程序
- 基于python的opencv圖像處理實(shí)現(xiàn)對(duì)斑馬線的檢測(cè)示例
- python 用opencv實(shí)現(xiàn)圖像修復(fù)和圖像金字塔
- python利用opencv保存、播放視頻
- python openCV自制繪畫板
- Python+OpenCV圖像處理——圖像二值化的實(shí)現(xiàn)
- python 基于opencv實(shí)現(xiàn)高斯平滑
相關(guān)文章
python3 實(shí)現(xiàn)除法結(jié)果為整數(shù)
這篇文章主要介紹了python3 實(shí)現(xiàn)除法結(jié)果為整數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Django細(xì)致講解多對(duì)多使用through自定義中間表方法
我們?cè)陂_發(fā)網(wǎng)站的時(shí)候,無可避免的需要設(shè)計(jì)實(shí)現(xiàn)網(wǎng)站的用戶系統(tǒng),我們需要實(shí)現(xiàn)包括用戶注冊(cè)、用戶登錄、用戶認(rèn)證、注銷等功能,Django作為完美主義終極框架,它默認(rèn)使用auth_user表來存儲(chǔ)用戶數(shù)據(jù),下面我們來看看Django多對(duì)多使用through自定義中間表2022-06-06
MATLAB如何利用散點(diǎn)進(jìn)行函數(shù)曲線擬合
這篇文章主要介紹了MATLAB如何利用散點(diǎn)進(jìn)行函數(shù)曲線擬合問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
python hough變換檢測(cè)直線的實(shí)現(xiàn)方法
這篇文章主要介紹了python hough變換檢測(cè)直線的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python random模塊(獲取隨機(jī)數(shù))常用方法和使用例子
這篇文章主要介紹了Python random模塊(獲取隨機(jī)數(shù))常用方法和使用例子,需要的朋友可以參考下2014-05-05

