gpt4 book ai didi

python - 使用 Python 将 OpenCV BoundingRect 转换为 NumPy 数组

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:15 25 4
gpt4 key购买 nike

在 OpenCV 中,调用 cv2.findContours 后,我得到了一个等高线数组。

contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

我想使用 cv2.boundingRect 给我一个定义轮廓的矩形,因为轮廓可能很复杂。

for contour in contours:
boundRect = cv2.boundingRect(contour)

但是,这给了我一个 BoundingRect 对象,它的形式是 (x, y, width, height)。是否有一种标准方法可以将其转换为具有已提供的辅助函数的标准 NumPy 数组,或者我是否需要手动构建它?

最佳答案

是的,您必须手动构建这样一个数组。

也许,你可以这样做:

>>> a = np.empty((0,4))
>>> for con in cont:
rect = np.array(cv2.boundingRect(con)).reshape(1,4)
a = np.append(a,rect,0)

在我的例子中,最终 a 的形状为 (166,4)

或者您可以使用任何 Numpy 方法来执行此操作。

关于python - 使用 Python 将 OpenCV BoundingRect 转换为 NumPy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11396830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com