在python中创建彩色滚轮图案图像

2024-10-03 06:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个给定宽度和高度的彩色车轮图案图像。类似这样的:

enter image description here

如何更好地使用opencvnumpy以创造性的pythonic方式完成? 我发现一些资源(例如here)正在使用matloblib的内置函数


Tags: 图像numpy宽度高度here方式资源pythonic
2条回答

利用马克·塞切尔答案中的线索,我能够生成给定宽度和高度的基于色轮的图像

色调:-

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 = np.ones((img_height, img_width)) * 255

enter image description here

价值:-

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

enter image description here

以下是相同的工作代码:-

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

结果图像: enter image description here

首先,您需要考虑在HSV颜色空间中需要哪些值,并生成这三个单通道层:

色调:

enter image description here

OpenCV中使用色调时要非常小心。如果Numpydtypenp.float,请使用0..360的范围。如果Numpydtypenp.uint8,请使用0..180的范围

饱和度:

enter image description here

价值:

enter image description here

然后使用以下方法组合它们:

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

并将结果从HSV转换为BGR颜色空间:

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

enter image description here

相关问题 更多 >