python 如何使用matplotlib绘制HSV色轮

68de4m5k  于 2022-12-21  发布在  Python
关注(0)|答案(2)|浏览(240)

是否可以使用Python中的matplotlib绘制HSV色轮?我想得到一个HSV色轮,如下图所示,作为光流的参考图像。在此图中,每个HSV颜色都可以表示为一个矢量,其中方向用色调编码,长度用饱和度编码,这样我就可以很容易地将其与光流进行比较。2我在网上搜索过,但没有找到满意的解决方案。任何帮助将不胜感激!

8dtrkrch

8dtrkrch1#

以下代码使用Colour进行HSV到RGB的转换:

def colour_wheel(samples=1024, clip_circle=True, method='Colour'):
    xx, yy = np.meshgrid(
        np.linspace(-1, 1, samples), np.linspace(-1, 1, samples))

    S = np.sqrt(xx ** 2 + yy ** 2)    
    H = (np.arctan2(xx, yy) + np.pi) / (np.pi * 2)

    HSV = colour.utilities.tstack([H, S, np.ones(H.shape)])
    RGB = colour.HSV_to_RGB(HSV)

    if clip_circle == True:
        RGB[S > 1] = 0
        A = np.where(S > 1, 0, 1)
    else:
        A = np.ones(S.shape)

    if method.lower()== 'matplotlib':
        RGB = colour.utilities.orient(RGB, '90 CW')
    elif method.lower()== 'nuke':
        RGB = colour.utilities.orient(RGB, 'Flip')
        RGB = colour.utilities.orient(RGB, '90 CW')

    R, G, B = colour.utilities.tsplit(RGB)
    
    return colour.utilities.tstack([R, G, B, A])

我们在这个交互式Jupyter Notebook Matplotlib小部件中使用它:

该存储库可从以下位置获得:https://github.com/colour-science/gamut-mapping-ramblings

iezvtpos

iezvtpos2#

Kel Solaar给出的答案非常好,但它实际上并没有使用matplotlib。
下面是使用matplolibnumpycoloursys得到的类似结果。

fig = plt.figure()
ax = fig.add_subplot(projection='polar')

rho = np.linspace(0,1,100) # Radius of 1, distance from center to outer edge
phi = np.linspace(0, math.pi*2.,1000 ) # in radians, one full circle

RHO, PHI = np.meshgrid(rho,phi) # get every combination of rho and phi

h = (PHI-PHI.min()) / (PHI.max()-PHI.min()) # use angle to determine hue, normalized from 0-1
h = np.flip(h)        
s = RHO               # saturation is set as a function of radias
v = np.ones_like(RHO) # value is constant

# convert the np arrays to lists. This actually speeds up the colorsys call
h,s,v = h.flatten().tolist(), s.flatten().tolist(), v.flatten().tolist()
c = [colorsys.hsv_to_rgb(*x) for x in zip(h,s,v)]
c = np.array(c)

ax.scatter(PHI, RHO, c=c)
_ = ax.axis('off')

请注意,这会导致图中蓝色/紫色指向上方,而红色指向右侧。根据我的经验,这是典型的光流色调图。要实现OP发布的图像,请更改以下内容,以便图开始绘制在左侧而不是右侧:

phi = np.linspace(math.pi, math.pi*3.,1000 ) # in radians, one full circle

相关问题