【发布时间】:2018-09-16 09:57:49
【问题描述】:
我想读取某个图像的 rgb 或 hsv 值。 我查了一下怎么读,找到了答案。
import cv2
image = cv2.imread("sample.jpg")
color = int(image[300, 300])
# if image type is b g r, then b g r value will be displayed.
# if image is gray then color intensity will be displayed.
print color
链接:Get RGB value opencv python
但该代码给了我这样的错误: TypeError: 只有 size-1 的数组可以转换为 Python 标量
我该如何解决这个错误? 谢谢。
【问题讨论】:
-
对于 RGB 图像... 错误信息自行解释。
image[300, 300]是<class 'numpy.ndarray'>。不是整数。您可以尝试将color = int(image[300, 300])更改为color = image[300, 300],然后尝试直接打印color或打印type(color)。您的代码可能适用于灰度图像(我自己没有测试过)。 HTH -
在顶部添加
import numpy as np,然后在底部添加color = np.array(image)。