How to draw a HSV color wheel using matplotlib

Viewed 2574

is it possible to draw a HSV color wheel using matplotlib in python? I want to get an HSV color wheel like the following image as a reference image of the optical flow. In this image, every HSV color can be expressed as a vector where the direction is encoded in hue and length is encoded in saturation so that I can easily compare it with optical flow. I have searched online but I could not find a satisfied solution. Any help will be grateful!

enter image description here

1 Answers

The following code uses Colour for the HSV to RGB conversion:

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])

Colour Wheel

We use it in this interactive Jupyter Notebook Matplotlib Widget:

HSV Control Widget

The repository is available here: https://github.com/colour-science/gamut-mapping-ramblings

Related