Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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.

like image 689
Hoodythree Avatar asked Mar 02 '20 07:03

Hoodythree


1 Answers

  • 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)
like image 120
bharatk Avatar answered Nov 04 '22 14:11

bharatk