RGB Primary Corrections, Saturation

After this discussion I was able in the end to implement a function for add or remove saturation modifying the rgb primaries, it was simpler than was I thought.

raw4_func.py (3.8 KB)

This python script accepts a linear rec.2020 .tif (floating point) and it converts the file to srgb at end.
It requires opencv, numpy and math.

def rgb_primaries_saturation (img,sat_r,sat_g,sat_b,rx,ry,gx,gy,bx,by,wx,wy):

The function itself takes in input sat_r, sat_g, sat_b for the saturation of the rgb primaries, 1 is the neutral value, values higher than 1 will desaturate the color primaries and values lower than 1 will increase the saturation

rx,ry,gx,gy,bx,by,wx,wy are the xy coordinates of the working space

Basically the function does this

  1. finds new rgb xy coordinates with linear interpolation

#rx=0.708
#ry=0.292
#gx=0.170
#gy=0.797
#bx=0.131
#by=0.046
#wx=0.3127
#wy=0.3290

#new red xy chromaticity coordinates
rxnew=sat_r*(rx-wx)+wx
rynew=wy+((rxnew-wx)*(ry-wy))/(rx-wx)

#new green xy chromaticity coordinates
gxnew=sat_g*(gx-wx)+wx
gynew=wy+((gxnew-wx)*(gy-wy))/(gx-wx)

#new blu xy chromaticity coordinates
bxnew=sat_b*(bx-wx)+wx
bynew=wy+((bxnew-wx)*(by-wy))/(bx-wx)

  1. calculate the rgb to xyz matrix from the old xy coordinates
  2. calculate the xyz to rgb matrix with the new rgb coordinates
  3. build the correction matrix “joining” these two matrices

For example with this parameters (increase red saturation, reduce blue saturation)

rgb_primaries_saturation(…,sat_r=0.5,sat_g=1,sat_b=2.5,…)

We get this correction matrix

[[ 7.45518286 2.96645120 2.54481714]
[-2.95987568 1.04150585 2.54481714]
[-2.95987568 5.70137613 1.29598757]]

Before

After the matrix

1 Like