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

Hoo*_*ree 16 python opencv numpy

我是 opencv 的新手。当我画一个圆时,发生了一些奇怪的事情。当我尝试将 c2 传递给圆函数时,它不起作用,但当我将 c1 传递给颜色参数时,它工作得很好。但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)
Run Code Online (Sandbox Code Playgroud)

当我使用 c2 时,控制台打印:“类型错误:参数“颜色”的标量值不是数字”,但为什么当c1 == c2时会发生这种情况?谢谢。

bha*_*atk 19

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

前任。

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)
Run Code Online (Sandbox Code Playgroud)