[英]matplotlib colors rgb_to_hsv not working right . Maybe need to report it?
我知道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]
...,
那些不是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)
請注意,源注釋狀態輸入/輸出應為維度M,N,3,並且RGBA(M,N,4)圖像的函數失敗,例如導入的png文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.