OpenCV Python – 如何查詢影像中點到輪廓的最短距離?


我們可以使用 **cv2.pointPolygonTest()** 計算影像中點到輪廓的最短距離,將輪廓點座標和點座標作為引數傳遞。在應用 **cv2.pointPolygonTest()** 之前,我們需要計算影像中的輪廓。我們可以按照以下步驟查詢影像中給定點到物件輪廓的最短距離:

  • 匯入所需的庫。在以下所有 Python 示例中,所需的 Python 庫是 **OpenCV**。確保您已安裝它。

  • 使用 **cv2.imread()** 讀取輸入影像。使用此方法讀取的 RGB 影像為 BGR 格式。可以選擇將讀取的 BGR 影像分配給 img。

  • 現在,使用 **cv2.cvtColor()** 函式將此 BGR 影像轉換為灰度影像,如下所示。可以選擇將轉換後的灰度影像分配給 **gray**。

  • 對灰度影像應用閾值處理,將其轉換為二值影像。調整第二個引數 (**threshValue**) 以獲得更好的二值影像。

  • 查詢二值影像中的輪廓。

  • 選擇第一個輪廓作為 **cnt** 或迴圈遍歷所有輪廓。

  • 使用 **cv2.pointPolygonTest()** 函式計算點與所選輪廓之間的最短距離。將所需的引數傳遞給此函式。要計算點 (**250,250**) 和輪廓 **cnt** 之間的最短距離,我們使用以下程式碼片段

dist = cv2.pointPolygonTest(cnt,(250,250),True)
  • 列印計算出的點與輸入影像中物件輪廓之間的最短距離。

  • 在影像中繪製點和檢測到的輪廓,以獲得更好的視覺化效果。

讓我們瞭解如何藉助一些 Python 示例查詢影像中給定點到物件輪廓的最短距離。

示例

在這個 Python 程式中,我們在輸入影像上取了兩個點 (250,250) 和 (350,250)。我們計算點與輸入影像中輪廓之間的最短距離。輸入影像只有一個物件輪廓。我們還在影像上繪製輪廓和點以方便理解。

# import required libraries import cv2 # load the input image img = cv2.imread('four-point-star.png') # convert the input image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding to convert grayscale to binary image ret,thresh = cv2.threshold(gray,150,255,0) # find the contours contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of contours detected:", len(contours)) # select the first contour cnt = contours[0] # find the shortest distance from point[250,250] dist1 = cv2.pointPolygonTest(cnt,(250,250),True) # print the shortest distance between the point 1 and contour detected. print('Shortest distance of Point 1 from contour:', dist1) dist2 = cv2.pointPolygonTest(cnt,(350,250),True) # print the shortest distance between the point 2 and contour detected. print('Shortest distance of Point 2 from contour:', dist2) # draw the point [250,250] on the image cv2.circle(img, (250,250), 4, (0, 0, 255), -1) cv2.putText(img, "Point 1", (255,255), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) cv2.circle(img, (350,250), 4, (0, 0, 255), -1) cv2.putText(img, "Point 2", (355,255), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) # draw contour on the input image cv2.drawContours(img, [cnt], -1, (0,255,255), 3) # display the image with drawn extreme points while True: cv2.imshow("Extreme Points", img) if cv2.waitKey(1) & 0xFF == 27: break cv2.destroyAllWindows()

我們將此影像用作此程式的 **輸入檔案**:


輸出

執行上述程式後,將生成以下輸出:

Number of contours detected: 1
Shortest distance of Point 1 from contour: -17.72004514666935
Shortest distance of Point 2 from contour: 31.622776601683793

並且我們得到以下視窗,顯示了輪廓和兩個點:


在上面的輸出影像中,輪廓以黃色繪製,兩個點以紅色繪製。點 1 與輪廓之間的最短距離為負數,表明該點位於輪廓外部。點 2 與輪廓之間的最短距離為正數,表明該點位於輪廓內部。如果最短距離為零,則表示該點位於輪廓邊界上

示例

在這個 Python 程式中,我們在輸入影像上取了一個點 (350,250)。我們檢測輸入影像中的所有物件輪廓。影像有三個輪廓。我們計算點與輸入影像中輪廓之間的最短距離。我們還在影像上繪製輪廓和點以方便理解。

# import required libraries import cv2 # load the input image img = cv2.imread('convexhull.png') # convert the input image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding to convert grayscale to binary image ret,thresh = cv2.threshold(gray,100,255,0) # find the contours contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of contours detected:", len(contours)) # draw contour and shape number for i, cnt in enumerate(contours): M = cv2.moments(cnt) x1, y1 = cnt[0,0] img1 = cv2.drawContours(img, [cnt], -1, (0,255,255), 3) cv2.putText(img, f'Contour:{i+1}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) # compute shortest distance of point (350,250) from the contour dist = cv2.pointPolygonTest(cnt,(350,250),True) print(f'Shortest distance of Point (350,250) from contour {i+1}:', dist) cv2.circle(img, (350,250), 4, (0, 0, 255), -1) cv2.imshow("Shapes", img) cv2.waitKey(0) cv2.destroyAllWindows()

我們將此影像用作此程式的 **輸入檔案**:


輸出

執行上述程式後,將生成以下輸出:

Number of contours detected: 3
Shortest distance of Point (350,250) from contour 1: -83.73768566183328
Shortest distance of Point (350,250) from contour 2: -62.81719509815764
Shortest distance of Point (350,250) from contour 3: -57.27564927611035

並且我們得到以下視窗,顯示了輪廓和點:


在上面的輸出影像中,輪廓以黃色繪製,點以紅色繪製。點與輪廓之間的每個最短距離都為負數,表明該點位於每個輪廓的外部。與其他距離相比,點與輪廓 3 之間的絕對距離最小。因此,該點最靠近輪廓 3。

更新於: 2022-12-02

4K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告