Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带点列表的python boundingRect_Python_Opencv - Fatal编程技术网

带点列表的python boundingRect

带点列表的python boundingRect,python,opencv,Python,Opencv,我试图在opencv中使用boundingRect()函数时出错。给出的是一个点列表 lists = [] for match in enumerate(matches): lists.append(kp2[match.trainIdx].pt) x,y,w,h = cv2.boundingRect(lists) TypeError:点不是numpy数组,也不是标量 p/s:我想在图像中检测到的对象周围画一个矩形 任何帮助都是非常感谢的 编辑 更改为np数组之前的列表 [(328.02

我试图在opencv中使用
boundingRect()
函数时出错。给出的是一个点列表

lists = []
for match in enumerate(matches):
    lists.append(kp2[match.trainIdx].pt)
x,y,w,h = cv2.boundingRect(lists)
TypeError:点不是numpy数组,也不是标量

p/s:我想在图像中检测到的对象周围画一个矩形

任何帮助都是非常感谢的

编辑

更改为np数组之前的列表

[(328.0227.0),(372.0241.0),(366.0229.0)]

之后

[[328.227]
[ 372.  241.]

[366.229.]

我刚刚遇到了完全相同的问题。未解决此问题,但以下是解决此问题的代码:

points = ([1,1], [3,1], [3,3], [1,4])

xx, yy = zip(*points)
min_x = min(xx); min_y = min(yy); max_x = max(xx); max_y = max(yy)
bbox = [(min_x, min_y), (max_x, min_y), (max_x, max_y), (min_x, max_y)]

点列表需要作为numpy数组传递。 因此,首先转换它,例如使用

lists = np.array(lists, dtype=np.float32)
失败的断言

'error: (-215) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function cv::pointSetBoundingRect'
如果创建的numpy数组没有数据类型
float32
int32
,则会触发。另一个原因是传递的数组不包含任何点

数组创建命令中的参数
dtype=np.float32
确保创建的数组具有数据类型
float32
。在64位环境中,省略此参数将导致使用datatype
float64
创建数组,从而导致断言失败

转换为数据类型为
float32
的numpy数组后,将结果传递到
cv.boundingRectangle
将按预期工作


在我被搜索引擎引导到这里之后,故意回答一个老问题。

使用numpy数组或标量。您所要做的就是查看函数CV::pointSetBoundingRect中的错误消息
error:(-215)npoints>=0&&(depth==CV|32F||depth==cv32s)从opencv获得此错误,该函数似乎只读取数组中的一个对象。