1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        使用Python和OpenCV檢測(cè)圖像中的多個(gè)亮點(diǎn)

        共 5009字,需瀏覽 11分鐘

         ·

        2020-10-20 09:07

        點(diǎn)擊上方AI算法與圖像處理”,選擇加"星標(biāo)"或“置頂”

        重磅干貨,第一時(shí)間送達(dá)

        來(lái)源:Opencv視覺(jué)實(shí)踐






        本文來(lái)自光頭哥哥的博客【Detecting multiple bright spots in an image with Python and OpenCV】,僅做學(xué)習(xí)分享。

        原文鏈接:https://www.pyimagesearch.com/2016/10/31/detecting-multiple-bright-spots-in-an-image-with-python-and-opencv/

        今天的博客文章是我?guī)啄昵白龅囊粋€(gè)關(guān)于尋找圖像中最亮點(diǎn)的教程的后續(xù)。
        我之前的教程假設(shè)在圖像中只有一個(gè)亮點(diǎn)你想要檢測(cè)...

        但如果有多個(gè)亮點(diǎn)呢?
        如果您想在圖像中檢測(cè)多個(gè)亮點(diǎn),代碼會(huì)稍微復(fù)雜一點(diǎn),但不會(huì)太復(fù)雜。不過(guò)不用擔(dān)心:我將詳細(xì)解釋每一個(gè)步驟。

        看看下面的圖片:

        在這幅圖中,我們有五個(gè)燈泡。
        我們的目標(biāo)是檢測(cè)圖像中的這五個(gè)燈泡,并對(duì)它們進(jìn)行唯一的標(biāo)記。
        首先,打開(kāi)一個(gè)新文件并將其命名為detect_bright_spot .py。然后,插入以下代碼:

        # import the necessary packagesfrom imutils import contoursfrom skimage import measureimport numpy as npimport argparseimport imutilsimport cv2# construct the argument parse and parse the argumentsap = argparse.ArgumentParser()ap.add_argument("-i", "--image", required=True,  help="path to the image file")args = vars(ap.parse_args())

        導(dǎo)入一些必要的包和命令行參數(shù)。

        要開(kāi)始檢測(cè)圖像中最亮的區(qū)域,我們首先需要從磁盤(pán)加載我們的圖像,然后將其轉(zhuǎn)換為灰度圖并進(jìn)行平滑濾波,以減少高頻噪聲:

        # load the image, convert it to grayscale, and blur itimage = cv2.imread(args["image"])gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)blurred = cv2.GaussianBlur(gray, (11, 11), 0)

        這些操作的輸出如下:

        為了顯示模糊圖像中最亮的區(qū)域,我們需要應(yīng)用閾值化:

        # threshold the image to reveal light regions in the# blurred imagethresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]

        操作取任意像素值p >= 200,并將其設(shè)置為255(白色)。像素值< 200被設(shè)置為0(黑色)。
        閾值化后,我們得到如下圖像:

        注意圖像的明亮區(qū)域現(xiàn)在都是白色的,而其余的圖像被設(shè)置為黑色。
        然而,在這幅圖像中有一點(diǎn)噪聲(即,小斑點(diǎn)),所以讓我們通過(guò)執(zhí)行一系列的腐蝕和膨脹操作來(lái)清除它:

        # perform a series of erosions and dilations to remove# any small blobs of noise from the thresholded imagethresh = cv2.erode(thresh, None, iterations=2)thresh = cv2.dilate(thresh, None, iterations=4)

        在應(yīng)用這些操作之后,你可以看到我們的thresh圖像變得更加“干凈”,但是仍然有一些我們想要移除的斑點(diǎn)。

        本項(xiàng)目的關(guān)鍵步驟是對(duì)上圖中的每個(gè)區(qū)域進(jìn)行標(biāo)記,然而,即使在應(yīng)用了腐蝕和膨脹后,我們?nèi)匀幌胍^(guò)濾掉剩余的小塊兒區(qū)域。

        一個(gè)很好的方法是執(zhí)行連接組件分析:

        # perform a connected component analysis on the thresholded# image, then initialize a mask to store only the "large"# componentslabels = measure.label(thresh, neighbors=8, background=0)mask = np.zeros(thresh.shape, dtype="uint8")# loop over the unique componentsfor 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ù)。

        然后我們?cè)诘?行初始化一個(gè)掩膜來(lái)存儲(chǔ)大的斑點(diǎn)。

        第7行我們開(kāi)始循環(huán)遍歷每個(gè)label中的正整數(shù)標(biāo)簽,如果標(biāo)簽為零,則表示我們正在檢測(cè)背景并可以安全的忽略它(9,10行)。

        否則,我們?yōu)楫?dāng)前區(qū)域構(gòu)建一個(gè)掩碼。

        下面我提供了一個(gè)GIF動(dòng)畫(huà),它可視化地構(gòu)建了每個(gè)標(biāo)簽的labelMask。使用這個(gè)動(dòng)畫(huà)來(lái)幫助你了解如何訪問(wèn)和顯示每個(gè)單獨(dú)的組件:

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

        輸出掩模如下圖:

        注意,所有小的斑點(diǎn)都被過(guò)濾掉了,只有大的斑點(diǎn)被保留了下來(lái)。
        最后一步是在我們的圖像上繪制標(biāo)記的斑點(diǎn):

        # find the contours in the mask, then sort them from left to# rightcnts = 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 contoursfor (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 imagecv2.imshow("Image", image)cv2.waitKey(0)

        首先,我們需要檢測(cè)掩模圖像中的輪廓,然后按從左到右排序(3-7行)。

        一旦我們的輪廓已經(jīng)排序,我們可以對(duì)它們進(jìn)行單獨(dú)的循環(huán)處理(第8行)。

        對(duì)于這些輪廓線,我們將計(jì)算出代表明亮區(qū)域的最小包圍圓(第12行)。

        然后,我們唯一地標(biāo)記該區(qū)域并在圖像上繪制它(第12-15行)。

        最后,第17行和第18行顯示了輸出結(jié)果。

        運(yùn)行程序,你應(yīng)該會(huì)看到以下輸出圖像:

        請(qǐng)注意,每個(gè)燈泡都被獨(dú)特地標(biāo)上了圓圈,圓圈圍繞著每個(gè)單獨(dú)的明亮區(qū)域。

        THE  END


        下載1:OpenCV黑魔法


        AI算法與圖像處理」公眾號(hào)后臺(tái)回復(fù):OpenCV黑魔法,即可下載小編精心編寫(xiě)整理的計(jì)算機(jī)視覺(jué)趣味實(shí)戰(zhàn)教程



        下載2 CVPR2020

        AI算法與圖像處公眾號(hào)后臺(tái)回復(fù):CVPR2020,即可下載1467篇CVPR 2020論文
            
        個(gè)人微信(如果沒(méi)有備注不拉群!
        請(qǐng)注明:地區(qū)+學(xué)校/企業(yè)+研究方向+昵稱(chēng)


        覺(jué)得有趣就點(diǎn)亮在看吧




        瀏覽 95
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            91秒拍福利视频 | 成人AV无码网站 | 精品少妇一区二区三区视频免付费 | 教室吻着她湿漉漉的私密之处视频 | 国产又粗又硬又大 | 亚洲精品视频免费观看 | 国产91精品看黄网站在线观看 | 精品成人无码乱码无人区 | 国产黄片操逼视频 | 欧美特大黄 |