Reputation: 300
I am trying to create a color wheel pattern image of given width and height. Something like this:-
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
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)
Saturation:-
saturation = np.ones((img_height, img_width)) * 255
Value:-
value = np.ones((img_height, img_width)) * 255
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
Upvotes: 3
Reputation: 207630
First you need to think what values you will need in HSV colourspace, and generate those three single-channel layers:
Hue:
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:
Value:
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)
Upvotes: 2