美文网首页
RGB转换到XYZ是什么样子的? (附python代码)

RGB转换到XYZ是什么样子的? (附python代码)

作者: 罗引杰 | 来源:发表于2021-07-21 13:03 被阅读0次
    image.png

    实际上XYZ色彩空间是通过RGB(线性信号)放射变换产生的, 即乘一个3X3 的矩阵
    代码如下, 以NTSC色彩空间下的RGB举例

    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
    import matplotlib.pyplot as plt
    
    points = np.array([[0, 0, 0],
                      [1, 0, 0 ],
                      [1, 1, 0],
                      [0, 1, 0],
                      [0, 0, 1],
                      [1, 0, 1 ],
                      [1, 1, 1],
                      [0, 1, 1]])
    
    P = [[0.6068909,  0.1735011,  0.2003480],
     [0.2989164,  0.5865990,  0.1144845],
     [0.0000000,  0.0660957,  1.1162243]]
    
    Z = np.zeros((8,3))
    for i in range(8): Z[i,:] = np.dot(points[i,:],P)
    Z = 1.0*Z
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    r = [-1,1]
    
    X, Y = np.meshgrid(r, r)
    # plot vertices
    ax.scatter3D(Z[:, 0], Z[:, 1], Z[:, 2])
    
    # list of sides' polygons of figure
    verts = [[Z[0],Z[1],Z[2],Z[3]],
     [Z[4],Z[5],Z[6],Z[7]], 
     [Z[0],Z[1],Z[5],Z[4]], 
     [Z[2],Z[3],Z[7],Z[6]], 
     [Z[1],Z[2],Z[6],Z[5]],
     [Z[4],Z[7],Z[3],Z[0]]]
    
    # plot sides
    ax.add_collection3d(Poly3DCollection(verts, 
     facecolors='cyan', linewidths=1, edgecolors='black', alpha=.9))
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    plt.show()
    

    相关文章

      网友评论

          本文标题:RGB转换到XYZ是什么样子的? (附python代码)

          本文链接:https://www.haomeiwen.com/subject/xqespltx.html