在 python 中创建色轮图案图像
Create color wheel pattern image in python
我正在尝试创建给定宽度和高度的色轮图案图像。像这样:-
如何最好地使用 opencv 和 numpy 以创造性的 pythonic 方式完成它?
我找到了一些资源(例如 ),其中使用了 matloblib
的内置函数。
首先,您需要考虑在 HSV 色彩空间中需要哪些值,并生成这三个单通道层:
色调:
对 OpenCV 中的色调要非常小心。如果您的 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)
利用 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, 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
结果图像:
我正在尝试创建给定宽度和高度的色轮图案图像。像这样:-
如何最好地使用 opencv 和 numpy 以创造性的 pythonic 方式完成它?
我找到了一些资源(例如 matloblib
的内置函数。
首先,您需要考虑在 HSV 色彩空间中需要哪些值,并生成这三个单通道层:
色调:
对 OpenCV 中的色调要非常小心。如果您的 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)
利用 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, 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
结果图像: