如何使用 OpenCV Python 计算图像中对象的纵横比?
对象的纵横比计算为对象边界矩形的宽度和高度之比。因此,要计算纵横比,我们首先必须找到对象的边界矩形。可以使用 cv2.boundingRect() 函数找到对象的边界矩形。
它接受对象的轮廓点并返回边界矩形的左上角坐标 (x,y) 和 (width, height)。我们使用宽度和高度来计算纵横比。
语法
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("对象的纵横比: ", ar) cv2.imshow("纵横比", 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
我们得到以下输出窗口 -
轮廓用绿色绘制,边界矩形用蓝色绘制。检测到的物体的纵横比用红色表示。