天天看點

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

點選上方“AI算法與圖像處理”,選擇加"星标"或“置頂”

重磅幹貨,第一時間送達

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

來源:Opencv視覺實踐

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

本文來自光頭哥哥的部落格【Detecting multiple bright spots in an image with Python and OpenCV】,僅做學習分享。

原文連結:https://www.pyimagesearch.com/2016/10/31/detecting-multiple-bright-spots-in-an-image-with-python-and-opencv/
           
opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

今天的部落格文章是我幾年前做的一個關于尋找圖像中最亮點的教程的後續。我之前的教程假設在圖像中隻有一個亮點你想要檢測...

但如果有多個亮點呢?如果您想在圖像中檢測多個亮點,代碼會稍微複雜一點,但不會太複雜。不過不用擔心:我将詳細解釋每一個步驟。

看看下面的圖檔:

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

在這幅圖中,我們有五個燈泡。我們的目标是檢測圖像中的這五個燈泡,并對它們進行唯一的标記。首先,打開一個新檔案并将其命名為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())
           

導入一些必要的包和指令行參數。

要開始檢測圖像中最亮的區域,我們首先需要從磁盤加載我們的圖像,然後将其轉換為灰階圖并進行平滑濾波,以減少高頻噪聲:

# 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)
           

這些操作的輸出如下:

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

為了顯示模糊圖像中最亮的區域,我們需要應用門檻值化:

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

操作取任意像素值p >= 200,并将其設定為255(白色)。像素值< 200被設定為0(黑色)。門檻值化後,我們得到如下圖像:

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

注意圖像的明亮區域現在都是白色的,而其餘的圖像被設定為黑色。然而,在這幅圖像中有一點噪聲(即,小斑點),是以讓我們通過執行一系列的腐蝕和膨脹操作來清除它:

# 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)
           

在應用這些操作之後,你可以看到我們的thresh圖像變得更加“幹淨”,但是仍然有一些我們想要移除的斑點。

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

本項目的關鍵步驟是對上圖中的每個區域進行标記,然而,即使在應用了腐蝕和膨脹後,我們仍然想要過濾掉剩餘的小塊兒區域。

一個很好的方法是執行連接配接元件分析:

# 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庫執行實際的連接配接元件分析。measure.lable傳回的label和我們的門檻值圖像有相同的大小,唯一的差別就是label存儲的為門檻值圖像每一斑點對應的正整數。

然後我們在第5行初始化一個掩膜來存儲大的斑點。

第7行我們開始循環周遊每個label中的正整數标簽,如果标簽為零,則表示我們正在檢測背景并可以安全的忽略它(9,10行)。

否則,我們為目前區域建構一個掩碼。

下面我提供了一個GIF動畫,它可視化地建構了每個标簽的labelMask。使用這個動畫來幫助你了解如何通路和顯示每個單獨的元件:

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

然後第15行對labelMask中的非零像素進行計數。如果numPixels超過了一個預先定義的門檻值(在本例中,總數為300像素),那麼我們認為這個斑點“足夠大”,并将其添加到掩膜中。

輸出掩模如下圖:

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

注意,所有小的斑點都被過濾掉了,隻有大的斑點被保留了下來。最後一步是在我們的圖像上繪制标記的斑點:

# 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)
           

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

一旦我們的輪廓已經排序,我們可以對它們進行單獨的循環處理(第8行)。

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

然後,我們唯一地标記該區域并在圖像上繪制它(第12-15行)。

最後,第17行和第18行顯示了輸出結果。

運作程式,你應該會看到以下輸出圖像:

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

請注意,每個燈泡都被獨特地标上了圓圈,圓圈圍繞着每個單獨的明亮區域。

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

THE  END

下載下傳1:OpenCV黑魔法

在「AI算法與圖像處理」公衆号背景回複:OpenCV黑魔法,即可下載下傳小編精心編寫整理的計算機視覺趣味實戰教程

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

下載下傳2 CVPR2020在「AI算法與圖像處理」公衆号背景回複:CVPR2020,即可下載下傳1467篇CVPR 2020論文

opencv 拟合圓 c++_使用Python和OpenCV檢測圖像中的多個亮點

覺得有趣就點亮在看吧

繼續閱讀