Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 472
Sam Shleifer Avatar asked May 06 '19 18:05

Sam Shleifer


2 Answers

I got this error when passing the coordinate points as lists like:

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

Passing them as tuples solved the problem:

cv2.rectangle(image, tuple(start_point), tuple(end_point), color, thickness=1)
like image 110
Pablo Guerrero Avatar answered Sep 30 '22 12:09

Pablo Guerrero


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.

like image 43
Sam Shleifer Avatar answered Sep 30 '22 14:09

Sam Shleifer