KaranKakwani
KaranKakwani

Reputation: 300

Create color wheel pattern image in python

I am trying to create a color wheel pattern image of given width and height. Something like this:-

enter image description here

How can it be done in a creative pythonic way preferably using opencv and numpy? I found some resources (e.g. here) where inbuilt functions of matloblib are being used.

Upvotes: 0

Views: 859

Answers (2)

KaranKakwani
KaranKakwani

Reputation: 300

Utilising the cues in Mark Setchell's answer, I am able to generate a color wheel based image of given width and height.

Hue:-

hue = np.fromfunction(lambda i, j: (np.arctan2(i-img_height/2, img_width/2-j) + np.pi)*(180/np.pi)/2,
                              (img_height, img_width), dtype=np.float)

enter image description here

Saturation:-

saturation = np.ones((img_height, img_width)) * 255

enter image description here

Value:-

value = np.ones((img_height, img_width)) * 255

enter image description here

Below is a working code of the same:-

def make_color_wheel_image(img_width, img_height):
    """
    Creates a color wheel based image of given width and height
    Args:
        img_width (int):
        img_height (int):

    Returns:
        opencv image (numpy array): color wheel based image
    """
    hue = np.fromfunction(lambda i, j: (np.arctan2(i-img_height/2, img_width/2-j) + np.pi)*(180/np.pi)/2,
                          (img_height, img_width), dtype=np.float)
    saturation = np.ones((img_height, img_width)) * 255
    value = np.ones((img_height, img_width)) * 255
    hsl = np.dstack((hue, saturation, value))
    color_map = cv2.cvtColor(np.array(hsl, dtype=np.uint8), cv2.COLOR_HSV2BGR)
    return color_map

Resultant Image: enter image description here

Upvotes: 3

Mark Setchell
Mark Setchell

Reputation: 207630

First you need to think what values you will need in HSV colourspace, and generate those three single-channel layers:

Hue:

enter image description here

Be very careful with the Hue in OpenCV. If your Numpy dtype is np.float, use a range of 0..360. If your Numpy dtype is np.uint8, use a range of 0..180.

Saturation:

enter image description here

Value:

enter image description here

Then combine them using:

HSL = np.dstack((Hue, Saturation, Value))

And convert the result from HSV to BGR colourspace:

wheel = cv2.cvtColor(... cv2.COLOR_HSV2BGR)

enter image description here

Upvotes: 2

Related Questions