如何使用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
并且我们将得到以下输出窗口:

轮廓以绿色绘制,边界矩形以蓝色绘制。检测到的物体的纵横比以红色显示。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP