python - matplotlib 颜色 rgb_to_hsv 不能正常工作。也许需要报告?

标签 python numpy matplotlib scipy

我知道 RGB 到 HSV 的转换应该采用 RGB 值 0-255 并转换为 HSV 值 [0-360, 0-1, 0-1]。例如看这个 converter in java :

当我在图像上运行 matplotlib.colors.rbg_to_hsv 时,它似乎输出值 [0-1, 0-1, 0-360]。但是,我已经在 image like this 上使用了这个函数。 , 它似乎以正确的顺序 [H,S,V] 工作,只是 V 太大了。

例子:

In [1]: import matplotlib.pyplot as plt

In [2]: import matplotlib.colors as colors

In [3]: image = plt.imread("/path/to/rgb/jpg/image")

In [4]: print image
[[[126  91 111]
  [123  85 106]
  [123  85 106]
  ..., 

In [5]: print colors.rgb_to_hsv(image)
[[[  0   0 126]
  [  0   0 123]
  [  0   0 123]
  ..., 

那些不是 0,它们是 0 和 1 之间的一些数字。

这是来自 matplotlib.colors.rgb_to_hsv 的定义

def rgb_to_hsv(arr):
    """
    convert rgb values in a numpy array to hsv values
    input and output arrays should have shape (M,N,3)
    """
    out = np.zeros(arr.shape, dtype=np.float)
    arr_max = arr.max(-1)
    ipos = arr_max > 0
    delta = arr.ptp(-1)
    s = np.zeros_like(delta)
    s[ipos] = delta[ipos] / arr_max[ipos]
    ipos = delta > 0
    # red is max
    idx = (arr[:, :, 0] == arr_max) & ipos
    out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx]
    # green is max
    idx = (arr[:, :, 1] == arr_max) & ipos
    out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0]) / delta[idx]
    # blue is max
    idx = (arr[:, :, 2] == arr_max) & ipos
    out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1]) / delta[idx]
    out[:, :, 0] = (out[:, :, 0] / 6.0) % 1.0
    out[:, :, 1] = s
    out[:, :, 2] = arr_max
    return out

我会使用其他 rgb_to_hsv 转换之一,如 colorsys,但这是我发现的唯一矢量化 python 转换。我们能解决这个问题吗?我们需要在github上报告吗?

Matplotlib 1.2.0、numpy 1.6.1、Python 2.7、Mac OS X 10.8

最佳答案

如果不是将 0 到 255 之间的 unsigned int RGB 值提供给它,而是将 0 到 1 之间的浮点 RGB 值提供给它,那么它会很好地工作。如果文档指定了这一点,或者如果该函数试图捕捉似乎很可能是人为错误。但您只需调用即可获得所需内容:

print colors.rgb_to_hsv(image / 255)

关于python - matplotlib 颜色 rgb_to_hsv 不能正常工作。也许需要报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17349928/

相关文章:

python - 将 Column2 值替换为满足 Column1 条件的字典键

python - 在numpy中生成字母数字随机数

python - 如何使用 matplotlib 和 SVM 算法向图形(数据集)添加边界?

python-2.7 - 导入 matplotlib._png 作为 _png 导入 : Error: DLL load failed: The specified module could not be found

python - 添加多个重叠立方体的矢量化方式

matplotlib - 更改 matplotlib quiver 函数的箭头样式

Python PIL 以日期时间作为名称保存文件

python - 如何解码中文文本中的unicode

python - 从常见的 python 脚本设置变量

python - 编写正则表达式以捕获带有可选前瞻的字符串