Hoodythree
Hoodythree

Reputation: 317

'TypeError: Scalar value for argument 'color' is not numeric' when using opencv-python drawing a circle

I'm new to OpenCV. Some weird things happened when I drew a circle. It didn't work when I tried to pass c2 to the circle function, but it worked well when I pass c1 to the color argument. But c1 == c2.

Here is my code:

import cv2
import numpy as np 

canvas = np.zeros((300, 300, 3), dtype='uint8')
for _ in range(1):
    r = np.random.randint(0, 200)
    center = np.random.randint(0, 300, size=(2, )) 
    color = np.random.randint(0, 255, size=(3, ))
    c1 = tuple(color.tolist())
    c2 = tuple(color)
    print('c1 == c2 : {} '.format(c1 == c2))
    cv2.circle(canvas, tuple(center), r, c2, thickness=-1)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)

When I use c2, the console printed:

TypeError: Scalar value for argument 'color' is not numeric

but why it happened when c1 == c2? Thanks.

Upvotes: 17

Views: 27599

Answers (1)

bharatk
bharatk

Reputation: 4315

  • Convert data type int64 to int.
  • ndarray.tolist() : data items are converted to the nearest compatible builtin Python type, via the item function.

Ex.

import cv2
import numpy as np 

canvas = np.zeros((300, 300, 3), dtype='uint8')
for _ in range(1):
    r = np.random.randint(0, 200)
    center = np.random.randint(0, 300, size=(2, )) 
    color = np.random.randint(0, 255, size=(3, ))

    #convert data types int64 to int
    color = ( int (color [ 0 ]), int (color [ 1 ]), int (color [ 2 ])) 
    cv2.circle(canvas, tuple(center), r, tuple (color), thickness=-1)


cv2.imshow('Canvas', canvas)
cv2.waitKey(0)

Upvotes: 21

Related Questions