在cie1931颜色空间Python 2.7中绘制色域

时间:2018-11-08 11:31:27

标签: python python-2.7 color-space

我要在CIE1931空间中绘制的色域:https://www.google.co.uk/search?biw=1337&bih=1257&tbm=isch&sa=1&ei=9x3kW7rqBo3ygQb-8aWYBw&q=viewpixx+gamut&oq=viewpixx+gamut&gs_l=img.3...2319.2828.0.3036.5.5.0.0.0.0.76.270.5.5.0....0...1c.1.64.img..0.0.0....0.KT8w80tcZik#imgrc=77Ufw31S6UVlYM

我想在这些坐标内创建ciexyY颜色的三角图:(.119,.113),(。162,.723),(。695,.304),如图所示-具有一组亮度Y为30.0。

我创建了一个0-1之间的xy值的3d数组。 然后,我创建了一个矩阵,三角形内部为1,三角形外部为0。 我将三角矩阵乘以xyY ndarray。 然后,我遍历xyY ndarray并将xyY值转换为rgb,并显示它们。

结果有点接近但不正确。我认为错误是在我转换为rgb的最后一节中,但是我不确定为什么。这是当前图像:https://imgur.com/a/7cWY0FI。任何建议将不胜感激。

from __future__ import division
import numpy as np
from colormath.color_objects import sRGBColor, xyYColor
from colormath.color_conversions import convert_color
import matplotlib.pyplot as plt

def frange(x,y,jump):
    while x < y:
        yield x
        x += jump

def onSameSide(p1,p2, A,B):
    cp1 = np.cross(B-A, p1-A)
    cp2 = np.cross(B-A, p2-A)
    if(np.dot(cp1, cp2) >= 0):
        return True
    else:
        return False

def isPointInTriangle(p,A,B,C):
    if(onSameSide(p,A,B,C) and onSameSide(p,B,A,C) and onSameSide(p,C,A,B)):
        return True
    else:
        return False

xlen = 400
ylen = 400

#CIExyY colour space
#Make an array (1,1,3) with each plane representing how x,y,Y vary in the coordinate space
ciexyY = np.zeros((3,xlen,ylen))
ciexyY[2,:,:]=30.0
for x in frange(0,1,1/xlen):
    ciexyY[0,:,int(xlen*x)]=x
    for y in frange(0,1,1/xlen):
        ciexyY[1,int(ylen*y),:]=y

#coordinates from Viewpixx gamut, scaled up to 100
blue=np.array((.119,.113,30.0))
green=np.array((.162,.723,30.0))
red=np.array((.695,.304,30.0))
#scale up to size of image
blue = np.multiply(blue,xlen)
green = np.multiply(green,xlen)
red = np.multiply(red,xlen)

#make an array of zeros and ones to plot the shape of Viewpixx triangle
triangleZeros = np.zeros((xlen,ylen))
for x in frange(0,xlen,1):
    for y in frange(0,ylen,1):
        if(isPointInTriangle((x,y,0),blue,green,red)):
            triangleZeros[x,y]=1
        else:
            triangleZeros[x,y]=0

#cieTriangle
cieTriangle = np.multiply(ciexyY,triangleZeros)

#convert cieTriangle xyY to rgb
rgbTriangle = np.zeros((3,xlen,ylen))
for x in frange(0,xlen,1):
    for y in range(0,ylen,1):
        xyYcolour = xyYColor(cieTriangle[0,x,y],cieTriangle[1,x,y],cieTriangle[2,x,y])
        rgbColour = convert_color(xyYcolour,sRGBColor)
        rgbTriangle[0,x,y] = rgbColour.rgb_r
        rgbTriangle[1,x,y] = rgbColour.rgb_g
        rgbTriangle[2,x,y] = rgbColour.rgb_b

rgbTriangle = np.transpose(rgbTriangle)
plt.imshow(rgbTriangle)
plt.show()

2 个答案:

答案 0 :(得分:0)

我们在Colour中拥有所有常见的色度图,我建议在python-colormath上使用它,因为Color是矢量化的,因此速度更快。

您是否可以共享当前图像的渲染?

答案 1 :(得分:0)

from colour.plotting import plot_chromaticity_diagram_CIE1931
plot_chromaticity_diagram_CIE1931()