chi*_*gry 7 python numpy matplotlib scipy
我知道RGB到HSV的转换应该取RGB值0-255并转换为HSV值[0-360,0-1,0-1].例如,在java中看到这个转换器:
当我在图像上运行matplotlib.colors.rbg_to_hsv时,它似乎输出值[0-1,0-1,0-360].但是,我在这样的图像上使用了这个函数,它似乎按正确的顺序[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]
...,
Run Code Online (Sandbox Code Playgroud)
那些不是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
Run Code Online (Sandbox Code Playgroud)
我会使用其他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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2509 次 |
| 最近记录: |