OpenCV-Python报错:Overload resolution failed: Scalar value for argument ‘color‘ is not numeric

OpenCV-Python报错:Overload resolution failed: Scalar value for argument ‘color’ is not numeric

在OpenCV中,许多绘图函数中的颜色参数接受的类型是cv::Scalar,这个类型表示一个多元素(最多4个)的标量。而在Matplotlib中,颜色的表达方式有更多种:字符、RGB等,初学者常常将Matplotlib种的颜色表示法混用到OpenCV种,导致一系列错误。

下面就三类经典的犯错方式进行示例和纠正:

错误原因1:传入的颜色参数是字符串

import cv2

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, ))
    # 直接使用'r'表示红色,错误
    cv2.circle(canvas, tuple(center), r, 'r', thickness=-1)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)

正确的方式是,已知红色的BGR值是(0,255,0),代码应改为:

import cv2

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, ))
    # 使用[0,255,0]表示红色,正确
    cv2.circle(canvas, tuple(center), r, [0,255,0], thickness=-1)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)

错误原因2:传入的颜色参数是numpy数组

在较新的opencv版本中,color参数不能传入numpy数组,只能传入list或tuple。错误示例:

import cv2

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, ))
    # 直接使用numpy数组,即便数据类型是int也会报错
    cv2.circle(canvas, tuple(center), r, color=np.array((int(centroid_color[0]),int(centroid_color[1]),int(centroid_color[2]))), thickness=-1)
cv2.imshow('Canvas', canvas)
cv2.waitKey(0)

总结

报错Scalar value for argument ‘color‘ is not numeric的根本原因是用户传入的color参数类型不符合要求,应尽量使传入的参数为包含三个元素的list或tuple。在OpenCV4中,实测不需要设置numpy数组的int64或int32类型,转换为list即可,不需要考虑数据类型问题。

The error message "Failed building wheel for opencv-python Failed to build opencv-python ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects" usually means that there is a problem with the installation of the opencv-python package. One solution is to try installing the package using conda instead of pip. You can create a new conda environment and install opencv-python in that environment using the following commands: ``` conda create --name myenv conda activate myenv conda install -c conda-forge opencv ``` This will create a new environment called "myenv" and activate it, and then install opencv-python in that environment. If you still want to use pip to install opencv-python, you can try upgrading pip to the latest version using the following command: ``` python -m pip install --upgrade pip ``` Then, you can try installing opencv-python again using pip: ``` pip install opencv-python ``` If this still doesn't work, you can try installing the dependencies for opencv-python manually before installing opencv-python itself. The dependencies are numpy and setuptools. You can install them using the following commands: ``` pip install numpy pip install setuptools ``` Once you have installed these dependencies, you can try installing opencv-python again using pip: ``` pip install opencv-python ``` If none of these solutions work, you can try installing a pre-built binary package of opencv-python. You can download the package from the official opencv-python website and install it using pip: ``` pip install opencv_python‑4.2.0.32‑cp37‑cp37m‑win_amd64.whl ``` Replace the package name with the appropriate version and platform for your system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值