如何使用OpenCV Python計算影像中物體的縱橫比?


物體的縱橫比計算方法是物體邊界矩形的寬高比。因此,要計算縱橫比,首先必須找到物體的邊界矩形。可以使用cv2.boundingRect()函式找到物體的邊界矩形。

它接收物體的輪廓點,並返回邊界矩形的左上角座標 (x,y) 和 (寬度, 高度)。我們使用寬度高度來計算縱橫比。

語法

x, y, w, h = cv2.boundingRect(cnt)
aspect_ratio = float(w)/h

這裡,“cnt”是影像中物體輪廓點的NumPy陣列。

步驟

您可以使用以下步驟計算影像中物體的縱橫比:

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

import cv2

使用cv2.imread()讀取輸入影像並將其轉換為灰度影像。

img = cv2.imread('fourpoint-star.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

對灰度影像應用閾值處理以建立二值影像。調整第二個引數以更好地檢測輪廓。

ret,thresh = cv2.threshold(gray,150,255,0)

使用cv2.findContours()函式查詢影像中的輪廓。

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

選擇一個輪廓cnt或迴圈遍歷所有輪廓。使用物體的邊界矩形的寬度和高度計算縱橫比。

cnt = contours[0]
x, y, w, h = cv2.boundingRect(cnt)
ar = float(w)/h

您可以選擇在輸入影像上繪製輪廓和邊界矩形。也可以將縱橫比作為文字新增到影像上。

cv2.drawContours(img, [cnt], -1, (0,255,255), 3)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

列印縱橫比並顯示帶有繪製輪廓和邊界矩形的影像。

print("Aspect Ratio of object: ", ar)
cv2.imshow("Aspect Ratio", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

讓我們來看一些例子以便更好地理解。

示例1

在這個Python程式中,我們計算影像中物體的縱橫比。我們在影像上繪製物體的輪廓和邊界矩形。我們還將縱橫比作為文字新增到物體上。

# import required libraries import cv2 # load the input image img = cv2.imread('fourpoint-star.png') # convert the 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 objects detected:", len(contours)) # define function to compute aspect ratio def aspect_ratio(cnt): x, y, w, h = cv2.boundingRect(cnt) ratio = float(w)/h return ratio # select first contour cnt = contours[0] # find the aspect ratio ar = aspect_ratio(cnt) # round it to two decimal points ar = round(ar, 2) # draw contours cv2.drawContours(img,[cnt],0,(0,255,0),2) # draw bounding rectangle x,y,w,h = cv2.boundingRect(cnt) cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # put text on the image cv2.putText(img, f'Aspect Ratio={ar}', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2) print(f"Aspect Ratio of object 1 =", ar) cv2.imshow("Aspect Ratio", img) cv2.waitKey(0) cv2.destroyAllWindows()

我們將使用以下影像作為此程式中的輸入檔案

輸出

執行上述程式碼後,將產生以下輸出

Number of objects detected: 1 
Aspect Ratio of object 1 = 1.71

並且,我們將得到以下輸出視窗:

輪廓以綠色繪製,邊界矩形以藍色繪製。檢測到的物體的縱橫比以白色顯示。

示例2

在這個Python程式中,我們計算影像中所有物體的縱橫比。我們在影像上繪製所有輪廓和邊界矩形。我們還將所有物體的縱橫比作為文字新增到影像上。

import cv2 import numpy as np img = cv2.imread('shapes2.png') img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(img1,40,255,0) contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of objects detected:", len(contours)) def aspect_ratio(cnt): x, y, w, h = cv2.boundingRect(cnt) ratio = float(w)/h return ratio # loop over all the contours for i, cnt in enumerate(contours): ar = aspect_ratio(cnt) ar = round(ar, 3) x,y,w,h = cv2.boundingRect(cnt) cv2.drawContours(img,[cnt],0,(0,255,0),2) cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) cv2.putText(img, f'{ar}', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) print(f"Aspect Ratio of object {i+1} =", ar) cv2.imshow("Aspect Ratio", img) cv2.waitKey(0) cv2.destroyAllWindows()

我們將使用以下影像作為此程式中的輸入檔案

輸出

執行上述程式碼後,將產生以下輸出

Number of objects detected: 7 
Aspect Ratio of object 1 = 1.662 
Aspect Ratio of object 2 = 0.657 
Aspect Ratio of object 3 = 0.705 
Aspect Ratio of object 4 = 0.912 
Aspect Ratio of object 5 = 0.94 
Aspect Ratio of object 6 = 1.054 
Aspect Ratio of object 7 = 1.845

並且我們將得到以下輸出視窗:

輪廓以綠色繪製,邊界矩形以藍色繪製。檢測到的物體的縱橫比以紅色顯示。

更新於:2022年9月28日

4K+ 瀏覽量

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.