What exactly is a "color matrix"? - RAW Shooting and Processing - CHDK Forum

# What exactly is a "color matrix"?

• 16 Replies
• 18751 Views

#### Howdy

• 21
• [S5 IS]
##### What exactly is a "color matrix"?
« on: 22 / April / 2008, 04:27:46 »
Advertisements
Hello!

I tried to write my own program for finding the color matrix (something like the ArtDen's one) but I failed.....
I do not seem to grasp the magic behind DCRaw.

I thought that the color matrix is some kind of transformation from XYZ into RGB (something like
http://www.brucelindbloom.com/Eqn_XYZ_to_RGB.html). So the algorithm I used was:

1) get the RAW
2) use dcraw -D -T -4 raw.dng
3) debayer raw.tiff to obtain debayered.tiff
4) I assumed that "debayered.tiff" contains pixels in XYZ format with (X being stored in R, Y in G and Z in B of the tiff). Am I right?
4) find the matrix M which would cause "debayered.tiff" to match raw.jpg (the JPEG generated by camera) as closely as possible. The formula to use was:

for each pixel (X,Y,Z) in debayered.tiff, to obtain a pixel in RGB format use
[R G B] = [X Y Z] *M,
where M is a 3x3 matrix of
m11, m12 m13
m21, m22, m23
m31, m32, m33
of unknown values that need to be found. I performed the search by minimizing the sum of squared errors ([R_jpg, G_jpg, B_jpg] - [X Y Z]*M

To test if the matrix is correct I used:

dngsetmatrix raw.dng M m11 m12 m13 m21 m22 m23 m31 m32 m33
dcraw -T -r 1 1 1 1 raw.dng
and then visually comparing the result with raw.jpg.

The results varied from "barely acceptable" to "hopeless".

I also tried to find the white balance (the -r r1 g1 b1 g1 argument to DCRaw) by hand so that the whites on ArtDen's test file were uniform (i.e. R=G=B on the white spot in the left-down corner) as closely as possible. I then redid my algorithm - the results were equally hopeless....

I come to a conclusion that DCRaw does some additional tricks, as:
1a) if you multiply each row of matrix M by any non-zero number such that the matrix is still displayed properly in dngsetmatrix, the result does not change
2a) if you multiply each column of M, the result changes
3a) If M really is the transformation from XYZ to RGB, then (all conslusions UNVERIFIED and COULD BE COMPLETELY WRONG!):
3.1a) by setting it to identity matrix you should obtain the TIFF with XYZ instead of RGB. Am I right?
3.2a) If you set M to
1 0 0
0 0 0
0 0 0
you should get TIFF with all reds, and G=B=0 - instead the DCRaw crashes.
3.3a) Maybe you need to have M invertible? If so, set:
1 0 0
0 0.001 0
0 0 0.001
and G and B should be very small - NOT TRUE - After testing it, I think that I got every possible value of RGB....

3.4a) Maybe the formula for transformation is wrong? Maybe it is the INVERSE of M that is needed?
I tried the inverse of 3.3a) and the result was different but still no reds...

Could someone (ArtDen? ) provide some comments on my approach? I KNOW I am doing SOMETHING very wrong, the problem is to find that SOMETHING

#### vit40

• 276
##### Re: What exactly is a "color matrix"?
« Reply #1 on: 22 / April / 2008, 05:43:03 »
About dcraw, you have to use -o 5 switch to get tiff in XYZ

About dcraw crashing with matrices mostly filled with zeros, probably some divide by zero error

As I wrote several times, you can't match the jpg from camera, because after conversion to sRGB, some additional tweaking is performed in camera, like boosting blue and yellow color (to get nice blue sky and more acceptable skin colors), and also some changes in hue

I didn't bother with a program like ArtDen, I tuned the matrix for A620 by hand (matrix for my S3 is still waiting to be tuned), which can be done that way only by specifying (supposed) primary values in xyY format, so DngSetMatrix also accepts this kind of input

#### Howdy

• 21
• [S5 IS]
##### Re: What exactly is a "color matrix"?
« Reply #2 on: 22 / April / 2008, 06:42:49 »
First of all, thank you for the response, vit 40!

Quote
About dcraw, you have to use -o 5 switch to get tiff in XYZ

If I understand vit40, the following should produce a TIFF with max. red values and very small G and B (G and B should be about 10000 times smaller). It DOES NOT - I still get TIFF with R=178 G=255 B=197 (approximately, of course; with -4 the ratio is more or less the same):

dngsetmatrix raw.dng M 1 0 0 0 0.0001 0 0 0 0.0001
dcraw +M -4 -T -o 5 -r 1 1 1 1 raw.dng

(I also tried
dcraw +M -T -o 5 -r 1 1 1 1 raw.dng
The result is the same.)

Could anyone confirm this result?

#### arkanoid

• 7
##### Re: What exactly is a "color matrix"?
« Reply #3 on: 22 / April / 2008, 10:10:10 »
I'm interested, too

#### Howdy

• 21
• [S5 IS]
##### Re: What exactly is a "color matrix"?
« Reply #4 on: 22 / April / 2008, 17:00:59 »
Arkanoid, could you perform the same test to confirm that one cannot get G and B channels close to zero using only the color matrix that I wrote?

If only ArtDen was here to teach me some of his color matrix' magic

#### ArtDen

• 175
##### Re: What exactly is a "color matrix"?
« Reply #5 on: 22 / April / 2008, 23:36:04 »
Howdy
Color matrix used in DNG file (and DCRaw program too) transforms colors from XYZ color space into camera sensor values. So for converting from camera sensor to XYZ, you have to calculate inverse matrix for color one. In order to get sRGB values you have:
1. Find inverse matrix
2. Calculate XYZ values (XYZ = inversed color matrix * cam.sensors)
3. Calculate RGB values (RGB = xyz_rgb_conv. matrix * XYZ)
4. Calculate sRGB values (sRGB = RGB with gamma)

#### Howdy

• 21
• [S5 IS]
##### Re: What exactly is a "color matrix"?
« Reply #6 on: 23 / April / 2008, 02:23:00 »
Thank you for an answer, ArtDen!

Hmmmmmm.... I think I can see the light in the tunnel....... Nope, still it is only the train But I think (=hope) that the solution is nearby....

If I understand you correctly, I have to calculate the mapping from [Sx Sy Sz] (Sx, Sy, Sz being the values from the sensor) into the XYZ space. Then I need to find the conversion from XYZ to RGB and then from RGB to sRGB. So to sum it up:

[X Y Z] = inv(M) * [Sx Sy Sz]^T   (T is the the transpose operator, inv(X) is the inverse of matrix X)
[R G B] = C * [X Y Z]^T      (C is the XYZ to RGB conversion matrix)

The last step is to add the gamma to [R G B].

Should [Sx Sy Sz] be calculated before or after debayering?

Is the value of C matrix known? Is it the one shown on http://www.brucelindbloom.com/Eqn_RGB_XYZ_Matrix.html under [sRGB, XYZ to RGB [M]-1]:

C= 3.24071    -0.969258    0.0556352
-1.53726     1.87599    -0.203996
-0.498571    0.0415557   1.05707

#### ArtDen

• 175
##### Re: What exactly is a "color matrix"?
« Reply #7 on: 23 / April / 2008, 02:31:49 »
Should [Sx Sy Sz] be calculated before or after debayering?
I don't know Just look at DCRaw sources.

Is the value of C matrix known?
Yes, matrix C is one of matrices on RGB/XYZ Matrices page. I use sRGB matrix.

PS: may be I was wrong about sRGB and RGB color spaces. I already forget differences between sRGB and RGB

#### Howdy

• 21
• [S5 IS]
##### Re: What exactly is a "color matrix"?
« Reply #8 on: 23 / April / 2008, 02:53:14 »
Quote
Quote
Should [Sx Sy Sz] be calculated before or after debayering?
I don't know Just look at DCRaw sources.

How do you do it in your program?

#### vit40

• 276
##### Re: What exactly is a "color matrix"?
« Reply #9 on: 23 / April / 2008, 03:49:44 »
I presume there's no conversion in DNG4PS(2), data are left in bayerized format in DNG, just in compressed form (DNG4PS2), and color matrix is written to appropriate tags of dng. My DngSetMatrix also changes only 2 tags, without changing other data

From my remembering, color space conversion in dcraw is done after debayerization (use -v  option (verbose) and check)