Search code examples
image-processingcolor-space

Matrix multiplication for color space conversion


In raw image processing you usually do two color space conversions using two 3x3 matrices: rgb2xyz and xyz2camera.

You could then get rgb2camera by either of these two matrix multiplications: (1) rgb2camera = (rgb2xyz)(xyz2camera) or: (2) rgb2camera = (xyz2camera)(rgb2xyz)

And then get camera2rgb by inverting the rgb2camera matrix.

However, matrix multiplication is not commutative so (1) and (2) give different results. I have seen both methods in various on-line articles, but which is correct? To me, (1) looks correct (mathematically), but (2) seems to give the more correct image (visually) for the sample image I have.


Solution

  • Mathematically if you first multiply by A and then by B the correct combined matrix is BA, not AB. They go in reverse order. You can think of this as putting the vector last always so it looks logical. So the second one should be correct.