问题描述
我正在尝试创建给定宽度和高度的色轮图案图像。像这样:-
最好使用opencv 和numpy 以创造性的pythonic 方式如何完成?
我发现了一些资源(例如 here),其中使用了 matloblib
的内置函数。
解决方法
利用 Mark Setchell 的回答中的提示,我能够生成给定宽度和高度的基于色轮的图像。
色调:-
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
以下是相同的工作代码:-
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,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
,
首先,您需要考虑在 HSV 色彩空间中需要哪些值,并生成这三个单通道层:
色调:
在 OpenCV 中使用 Hue 时要非常小心。如果您的 Numpy dtype
是 np.float
,请使用 0..360 的范围。如果您的 Numpy dtype
是 np.uint8
,请使用 0..180 的范围。
饱和度:
值:
然后使用:
HSL = np.dstack((Hue,Saturation,Value))
并将结果从 HSV 转换为 BGR 色彩空间:
wheel = cv2.cvtColor(... cv2.COLOR_HSV2BGR)