Skip to content

How to calculate RP-177 3×3 matrices in Matlab and Python. Part 2. TRA. Rec709 to Rec2020.

Matlab code made by Tinna Lif Gunnarsdottir.

Part 1 is here

SMPTE RP-177:1993  Rec709 to Rec2020 NPM/TRA. P3 to Rec2020 TRA has a problem with the color red.

COLOR REC 2020 NPM & NPM INV (Matlab)

%COLOR REC 2020 NPM & NPM INV        
R = [0.708 ; 0.292 ; 0.000];
G = [0.170 ; 0.797 ; 0.0330];
B = [0.131 ; 0.046 ; 0.8230];
P = [R G B] ;

%White Point D65        
X = 0.3127;
Y = 0.3290;
Z = 0.3583;        
W = [X/Y ; 1 ; Z/Y];
CC = P \ W;
C = [CC(1,1) 0 0 ; 0 CC(2,1) 0 ; 0 0 CC(3,1)];
format long 
NPM = P * C
NPM_inv = inv(NPM) 
NPM =
0.636958048301291   0.144616903586208   0.168880975164172
0.262700212011267   0.677998071518871   0.059301716469862
0                   0.028072693049088   1.060985057710791

NPM_inv =
 1.716651187971268  -0.355670783776393  -0.253366281373660
-0.666684351832489   1.616481236634939   0.015768545813911
 0.017639857445311  -0.042770613257809   0.942103121235474

From REC709 D65 to Rec-2020 (Matlab)

%From REC 709 D65 to REC-2020        
R_7 = [0.640 ;  0.330 ; 0.030];
G_7 = [0.300 ;  0.600 ; 0.100];
B_7 = [0.150 ;  0.060 ; 0.790];
P_7 = [R_7 G_7 B_7];
% White Point  D65
X_7 = 0.3127;
Y_7 = 0.3290;
Z_7 = 0.3583;
W_7 = [X_7/Y_7 ; 1 ; Z_7/Y_7];
CC_7 = P_7 \ W_7;             
% P inv
C_7 = [ CC_7(1,1) 0 0 ; 0 CC_7(2,1) 0 ; 0 0 CC_7(3,1)];
NPM_7 = P_7 * C_7
TRA_7 =  NPM \ NPM_7 
% Rec709 source, NPM/2020 dest, and inv
NPM_7 =
0.412390799265959   0.357584339383878   0.180480788401834
0.212639005871510   0.715168678767756   0.072192315360734
0.019330818715592   0.119194779794626   0.950532152249661

TRA_7 =
0.627403895934699   0.329283038377884   0.043313065687417
0.069097289358232   0.919540395075459   0.011362315566309
0.016391438875150   0.088013307877226   0.895595253247624

Python REC709 D65 to Rec-2020 (Python)

import numpy as np

# COLOR REC 2020 NPM & NPM INV
R = np.array([0.708, 0.292, 0.000])
G = np.array([0.170, 0.797, 0.033])
B = np.array([0.131, 0.046, 0.823])
P = np.column_stack((R, G, B))

# White Point D65
X = 0.3127
Y = 0.3290
Z = 0.3583
W = np.array([X/Y, 1, Z/Y])

# Compute CC
CC = np.linalg.solve(P, W)

# Create C matrix
C = np.diag(CC)

# Compute NPM and NPM_inv
NPM = np.dot(P, C)
NPM_inv = np.linalg.inv(NPM)

# From REC 709 D65 to REC-2020
R_7 = np.array([0.640, 0.330, 0.030])
G_7 = np.array([0.300, 0.600, 0.100])
B_7 = np.array([0.150, 0.060, 0.790])
P_7 = np.column_stack((R_7, G_7, B_7))

# White Point D65
X_7 = 0.3127
Y_7 = 0.3290
Z_7 = 0.3583
W_7 = np.array([X_7/Y_7, 1, Z_7/Y_7])

# Compute CC_7
CC_7 = np.linalg.solve(P_7, W_7)

# Create C_7 matrix
C_7 = np.diag(CC_7)

# Compute NPM_7
NPM_7 = np.dot(P_7, C_7)

# Compute TRA_7
TRA_7 = np.linalg.solve(NPM, NPM_7)

# Print results with high precision
np.set_printoptions(precision=15)
print("NPM:")
print(NPM)
print("\nNPM_inv:")
print(NPM_inv)
print("\nNPM_7:")
print(NPM_7)
print("\nTRA_7:")
print(TRA_7)

Download and install Python

Run cmd on windows or terminal on mac
Install NumPy

pip3 install numpy


Copy and paste code into a text editor like nano, notepad++(win), bbedit(mac) and save it as rec709torec2020.py

Run the code

python3 rec709torec2020.py

The result is the same as in Matlab

NPM:
[[0.636958048301291 0.144616903586208 0.168880975164172]
[0.262700212011267  0.677998071518871 0.059301716469862]
[0.                 0.028072693049088 1.060985057710791]]
NPM_inv:
[[ 1.716651187971268 -0.355670783776392 -0.25336628137366 ]
[-0.666684351832489   1.616481236634939  0.015768545813911]
[ 0.017639857445311  -0.042770613257809  0.942103121235474]]
NPM_7:
[[0.412390799265959 0.357584339383878 0.180480788401834]
[0.21263900587151   0.715168678767756 0.072192315360734]
[0.019330818715592  0.119194779794626  0.95053215224966 ]]
TRA_7:
[[0.627403895934699 0.329283038377884 0.043313065687417]
[0.069097289358232  0.919540395075459 0.011362315566309]
[0.01639143887515   0.088013307877226 0.895595253247624]]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.