Python 在np.array上应用cv2.boundingRect

Python 在np.array上应用cv2.boundingRect,python,opencv,Python,Opencv,如何将cv2.boundingRect应用于点的np.array 下面的代码生成一个错误 points = np.array([[1, 2], [3, 4]], dtype=np.float32) import cv2 cv2.boundingRect(points) 错误: OpenCV Error: Unsupported format or combination of formats (The image/matrix format is not supported by the fu

如何将
cv2.boundingRect
应用于点的
np.array
下面的代码生成一个错误

points = np.array([[1, 2], [3, 4]], dtype=np.float32)
import cv2
cv2.boundingRect(points)
错误:

OpenCV Error: Unsupported format or combination of formats (The image/matrix format is not supported by the function) in cvBoundingRect, file /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/shapedescr.cpp, line 97

File "<ipython-input-23-42e84e11f1a7>", line 1, in <module>
  cv2.boundingRect(points)
error: /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/shapedescr.cpp:970: error: (-210) The image/matrix format is not supported by the function in function cvBoundingRect
OpenCV错误:cvBoundingRect文件/build/buildd/OpenCV-2.4.8+dfsg1/modules/imgproc/src/shapedscr.cpp第97行中不支持格式或格式组合(函数不支持图像/矩阵格式)
文件“”,第1行,在
cv2.boundingRect(点)
错误:/build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/shapedscr.cpp:970:error:(-210)函数cvBoundingRect中的函数不支持图像/矩阵格式

OpenCV 2.x版本的Python绑定对某些数据的表示方式与3.x版本略有不同

从现有的代码示例(例如on SO),我们可以看到,我们可以调用
cv2.boundingRect
,使用
cv2.findContours
返回的等高线列表的en元素。让我们看看它是什么样子:

>>> a = cv2.copyMakeBorder(np.ones((3,3), np.uint8),1,1,1,1,0)
>>> b,c = cv2.findContours(a, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
>>> b[0]
array([[[1, 1]],

       [[1, 3]],

       [[3, 3]],

       [[3, 1]]])
我们可以看到,轮廓中的每个点都表示为
[[x,y]]
,我们有这些点的列表

因此

我们得到了输出

(1, 2, 3, 3)

我打算建议
cv2.boundingRect(cv2.cv.fromarray(points))
,但这似乎也不起作用。返回
TypeError:points不是numpy数组,也不是标量
我尝试了你的代码,它给了我这个边界框,没有错误(1,2,3,3),这似乎是真的。然而,我的简历版本是“3.1.0-dev”。
(1, 2, 3, 3)