HSV色锥建模

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from math import sin, cos, pi

def rgb2hsv(r, g, b):
    r_, g_, b_ = r / 255, g / 255, b / 255
    c_max = max(r_, g_, b_)
    c_min = min(r_, g_, b_)
    dela = c_max - c_min

    if dela == 0:
        h = 0
    elif c_max == r_ and g_ >= b_:
        h = 60 * ((g_ - b_) / dela + 0)
    elif c_max == r_ and g_ < b_:
        h = 60 * ((g_ - b_) / dela + 2)
    elif c_max == g_:
        h = 60 * ((b_ - r_) / dela + 2)
    else:
        h = 60 * ((r_ - g_) / dela + 4)

    s = 0 if c_max == 0 else dela / c_max
    v = c_max
    return h, s * 100, v * 100

def hsv2cartesian(h, s, v):
    x = s * cos(h * pi / 180)
    y = s * sin(h * pi / 180)
    z = v
    return x, y, z

def hsv2rgb(h, s, v):
    s /= 100
    v /= 100
    c = v * s
    x = c * (1 - abs((h / 60) % 2 - 1))
    m = v - c
    if h < 60:
        r_, g_, b_ = c, x, 0
    elif h < 120:
        r_, g_, b_ = x, c, 0
    elif h < 180:
        r_, g_, b_ = 0, c, x
    elif h < 240:
        r_, g_, b_ = 0, x, c
    elif h < 300:
        r_, g_, b_ = x, 0, c
    else:
        r_, g_, b_ = c, 0, x
    r, g, b = (r_ + m) * 255, (g_ + m) * 255, (b_ + m) * 255
    return r / 255, g / 255, b / 255



fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
size = 15  # 减小点的数量
points = np.linspace(0, 255, size).astype(np.int32)

for h in np.linspace(0, 360, size):
    for s in np.linspace(0, 100, size):
        for v in np.linspace(0, 100, size):
            if v < s:
                continue
            x, y, z = hsv2cartesian(h, s / 100, v / 100)
            ax.scatter([x], [y], [z], color=hsv2rgb(h, s, v), marker='o')  # 使用scatter绘制点
            
           



ax.set_zlabel('r')
ax.set_ylabel('g')
ax.set_xlabel('b')
plt.show()




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值