2024-10-03 06:29:46 发布
网友
我试图创建一个给定宽度和高度的彩色车轮图案图像。类似这样的:
如何更好地使用opencv和numpy以创造性的pythonic方式完成? 我发现一些资源(例如here)正在使用matloblib的内置函数
matloblib
利用马克·塞切尔答案中的线索,我能够生成给定宽度和高度的基于色轮的图像
色调:-
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
结果图像:
首先,您需要考虑在HSV颜色空间中需要哪些值,并生成这三个单通道层:
色调:
在OpenCV中使用色调时要非常小心。如果Numpydtype是np.float,请使用0..360的范围。如果Numpydtype是np.uint8,请使用0..180的范围
dtype
np.float
np.uint8
饱和度:
价值:
然后使用以下方法组合它们:
HSL = np.dstack((Hue, Saturation, Value))
并将结果从HSV转换为BGR颜色空间:
wheel = cv2.cvtColor(... cv2.COLOR_HSV2BGR)
利用马克·塞切尔答案中的线索,我能够生成给定宽度和高度的基于色轮的图像
色调:-
饱和度:-
价值:-
以下是相同的工作代码:-
结果图像:
首先,您需要考虑在HSV颜色空间中需要哪些值,并生成这三个单通道层:
色调:
在OpenCV中使用色调时要非常小心。如果Numpy
dtype
是np.float
,请使用0..360的范围。如果Numpydtype
是np.uint8
,请使用0..180的范围饱和度:
价值:
然后使用以下方法组合它们:
并将结果从HSV转换为BGR颜色空间:
相关问题 更多 >
编程相关推荐