cv2.rectangle: TypeError: Argument given by name ('thickness') and position (4)

Sam Shleifer picture Sam Shleifer · May 6, 2019 · Viewed 8.6k times · Source

I am trying to visualize bounding boxes on top of an image.

My Code:

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)

and I get TypeError: Argument given by name ('thickness') and position (4) Even if I pass thickness positionally, I get a different traceback:

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

raises TypeError: expected a tuple.

Answer

Sam Shleifer picture Sam Shleifer · May 6, 2019

You need to make sure your bounding coordinates are integers.

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

Either invocation of cv2.rectangle will work.