"类型错误:使用 opencv-python 绘制圆时,参数"颜色"的标量值不是数字"



我是opencv的新手。当我画一个圆圈时,发生了一些奇怪的事情。当我试图将c2传递给circle函数时,它不起作用,但当我将c1传递给color参数时,它起作用很好。但是c1==c2

这是我的代码:

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)

当我使用c2时,控制台会打印:"TypeError:参数'color'的标量值不是数字",但为什么在c1==c2时会发生这种情况?谢谢

  • 将数据类型int64转换为int
  • ndarray.tolist():数据项通过item函数转换为最接近的兼容内置Python类型

示例

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)

最新更新