cv2.rectangle: TypeError: 由名称("厚度")和位置给出的参数 (4)



我正在尝试可视化图像顶部的边界框。

我的代码:

color = (255, 255, 0)
thickness = 4
x_min, y_min, x_max, y_max = bbox
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, thickness=thickness)

我得到了 TypeError: Argument given by name ('thickness') and position (4)即使我位于厚度,我也会得到不同的追溯:

cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, thickness)

提高TypeError: expected a tuple.

您需要确保您的边界坐标是整数。

x_min, y_min, x_max, y_max = map(int, bbox)
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, thickness)

cv2.rectangle的调用都可以工作。

我在将坐标点传递为以下列表时遇到了此错误:

start_point = [0, 0]
end_point = [10, 10]
cv2.rectangle(image, start_point, end_point, color, thickness=1)

将它们传递到元组时解决了问题:

cv2.rectangle(image, tuple(start_point), tuple(end_point), color, thickness=1)

有时与OpenCV相关的错误原因是您的图像(numpy数组(在内存中不连续。请重试您的图像明确连续:

img = np.ascontiguousarray(img)

当您对切片,更改RGB顺序等图像进行一些操纵时,图像往往不连续。

无需声明厚度,您可以直接给出数字,例如

cv2.rectangle(img, (0, 0), (250, 250), 3)

这里3代表厚度,也不需要img名称的结肠。

尝试使用变量在图像上绘制界限时,我遇到了相同的错误

bbox_color = (id, id, id)
cv2.rectangle(img, (x1, y1), (x2, y2), bbox_color, thickness=1)

我想错误是由于颜色参数中的类型不匹配。它应该是类型< class'int>但就我而言,它是类型< numpy.int64>。

可以通过将每个元素转换为正确的类型来解决:

bbox_color = (id, id, id)
bbox_color = [int(c) for c in bbox_color]
cv2.rectangle(img, (x1, y1), (x2, y2), bbox_color, thickness=1)

最新更新