colour-science是专门用来进行颜色空间计算的python模块,绘制CIE色度图用法十分简便。近来尝试了CIE 1976色度图的绘制,记录如下:
安装python colour-science模块,用于绘制色域图
git clone git://github.com/colour-science/colour.git
cd colour
conda activate back-matting #用虚拟环境安装为好
pip install --user
- 1
- 2
- 3
- 4
安装plotting option
pip install --user ‘colour-science[plotting]’
- 1
安装完成。
绘制:
1 colour-science函数画出CIE1976UCS色度图;
2 将CIE1931 xy点坐标转换成CIE1976 u’v’坐标;
3 matplotlib绘制ITU-R BT.709,ITU-R BT.2020,DCI-P3色度空间多边形,以及pointer 色度空间;
4 附加曲线标注,修改坐标范围;
5 显示
import colour
from colour.plotting import *
colour_style()
import numpy as np
plot_chromaticity_diagram_CIE1976UCS(standalone=False)
import matplotlib.pyplot as plt
import matplotlib.patches as
ax = plt.gca() # 获取CIE1976UCS的坐标系
# 坐标转换函数
# colour.xy_to_Luv_uv convert CIE 1931 xy to CIE 1976UCS u’v’
# colour.xy_to_UCS_uv convert CIE 1931 xy to CIE 1960UCS uv
# xy: CIE 1931 xy; UCS_uv: CIE 1960UCS uv; Luv_uv: CIE 1976 u’v’
ITUR_709=([[.64, .33], [.3, .6], [.15, .06]])
ITUR_709_uv=colour.xy_to_Luv_uv(ITUR_709)
ITUR_2020=([[.708, .292], [.170, .797], [.131, .046]])
ITUR_2020_uv=colour.xy_to_Luv_uv(ITUR_2020)
DCI_P3=([[.68, .32], [.265, .69], [.15, .06]])
DCI_P3_uv=colour.xy_to_Luv_uv(DCI_P3)
pointer_bound= ([[ 0.508, 0.226], [ 0.538, 0.258], [ 0.588, 0.280], [ 0.637, 0.298], [ 0.659, 0.316],
[ 0.634, 0.351], [ 0.594, 0.391], [ 0.557, 0.427], [ 0.523, 0.462], [ 0.482, 0.491],
[ 0.444, 0.515], [ 0.409, 0.546], [ 0.371, 0.558], [ 0.332, 0.573], [ 0.288, 0.584],
[ 0.242, 0.576], [ 0.202, 0.530 ], [ 0.177, 0.454], [ 0.151, 0.389],[ 0.151, 0.330 ],
[ 0.162, 0.295], [ 0.157, 0.266], [ 0.159, 0.245], [ 0.142, 0.214], [ 0.141, 0.195],
[ 0.129, 0.168], [ 0.138, 0.141], [ 0.145, 0.129], [ 0.145, 0.106], [ 0.161, 0.094],
[ 0.188, 0.084], [ 0.252, 0.104], [ 0.324, 0.127], [ 0.393, 0.165], [ 0.451, 0.199], [ 0.508, 0.226]])
pointer_bound_uv=colour.xy_to_Luv_uv(pointer_bound)
# matplotlib绘制四个多边形,对应四种颜色空间
gamut_709=patches.Polygon(ITUR_709_uv, linewidth=2, color=‘green’, fill=False)
gamut_2020=patches.Polygon(ITUR_2020_uv, linewidth=2, color=‘yellow’, fill=False)
gamut_DCI_P3=patches.Polygon(DCI_P3_uv, linewidth=1, color=‘blue’, fill=False)
gamut_pointer=patches.Polygon(pointer_bound_uv, linewidth=2, color=‘white’, fill=False)
ax.add_patch(gamut_709)
ax.add_patch(gamut_2020)
ax.add_patch(gamut_DCI_P3)
ax.add_patch(gamut_pointer)
plt.legend([gamut_709,gamut_2020, gamut_DCI_P3, gamut_pointer],
[‘ITU-R BT.709’,‘ITU-R BT.2020’, ‘DCI-P3’, ‘pointer gamut’],
loc=‘upper right’) # 对曲线的标注
plt.axis([-0.1, 0.7, -0.1, 0.7]) #改变坐标轴范围
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
也可以直接用以下函数绘制。
RGB = np.random.random((3, 3, 3)) #(32,32,3) 定义点数少,可减少RGB对图形的干扰。
plot_RGB_chromaticities_in_chromaticity_diagram_CIE1976UCS(RGB,colourspaces=['ITU-R BT.2020','ITU-R BT.709','dci-p3'],show_pointer_gamut=True)
- 1
- 2
注:
plot_RGB_chromaticities_in_chromaticity_diagram_CIE1976UCS()
- 1
函数和
plot_chromaticity_diagram_CIE1976UCS(standalone=False)
- 1
不同,前者直接显示了所绘制的图像,于是不能在其上再用matplotlib绘制任何东西;后者没有显示图像,因此可以添加matplotlib的对象,最后
plt.show()
- 1
显示合成的图像。
两图比较,由plot_RGB_chromaticities_in_chromaticity_diagram_CIE1976UCS()绘制的pointer色域图与第一个(matplotlib绘制)略有不同,在见右上角处。原因是从colour-science提供的colour.POINTER_GAMUT_BOUNDARIES数据少了三个点。而matplotlib从colour-science github网站提供的PointerData.xls则共有35个点,此xls文件由pointer博士提供,具有权威性。
根据色度学,pointer色域是自然界常见的颜色范围,目前所用的几种颜色空间都是围绕pointer色域来定义,使之尽可能多的包含pointer色域。
BT.709色域是HDTV采用的标准,对CIE1976色域的覆盖率为33.2%,对pointer色域的覆盖率为70.2%。
DCI-P3是美国数字影院系统采用的色域,对CIE1976和pointer色域的覆盖率分别是41.7%和85.5%。
BT.2020是超高清UHDTV和高动态范围HDR电视规定的宽色域,对CIE1976和pointer色域的覆盖率分别是57.3%和99.7%。
目前的液晶显示器,基本能够覆盖BT.709色域;性能优异的液晶显示器,对DCI-P3色域的覆盖可达到98%;而采用纯净激光RGB光源的 DLP投影机,则可覆盖BT.2020色域达到97%。