Matrix Operations for Image Processing
Paul Haeberli Nov 1993 Introduction Four by four matrices are commonly used to transform geometry for 3D rendering. These matrices may also be used to transform RGB colors, to scale RGB colors, and to control hue, saturation and contrast. The most important advantage of using matrices is that any number of color transformations can be composed using standard matrix multiplication. Please note that for these operations to be correct, we really must operate on linear brightness values. If the input image is in a non-linear brightness space RGB colors must Be transformed Into a linear space Before these Matrix Operations Arend. Color TransformationRGB Colors Arend by A Four by Four Matrix As Shown Here: XFormRGB (MAT, R, G, B, Tr, TG, TB)
Float Mat [4] [4];
Float r, g, b;
FLOAT * TR, * TG, * TB;
{
* Tr = r * Mat [0] [0] g * MAT [1] [0]
B * MAT [2] [0] MAT [3] [0];
* TG = r * Mat [0] [1] g * Mat [1] [1]
B * MAT [2] [1] MAT [3] [1];
* TB = r * Mat [0] [2] g * MAT [1] [2]
B * MAT [2] [2] MAT [3] [2];
}
The IdentityThis Is The Identity Matrix: Float Mat [4] [4] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
}
Transforming colors by the Identity Matrix Will Leave The Unchanged. Changing Brightnessto Scale RGB Colors A Matrix Like this is buy: float mat [4] [4] = {
Rscale, 0.0, 0.0, 0.0,
0.0, gscale, 0.0, 0.0,
0.0, 0.0, bscale, 0.0,
0.0, 0.0, 0.0, 1.0,
}
WHERE RSCALE, GSCALE, AND BSCALE SPECIFY How much to scale the r, g, and balances of colors. This can be used to alter the color, this Calculates: Tr = r * rscale; tg = g * gscale;
TB = b * bscale;
Modifying Saturation Converting to LuminanceTo Convert a Color Image IMAGINA BLACK AND White Image, this Matrix is buy: float mat [4] [4] = {
RWGT, RWGT, RWGT, 0.0,
GWGT, GWGT, GWGT, 0.0,
BWGT, BWGT, BWGT, 0.0,
0.0, 0.0, 0.0, 1.0,
}
Where rwgt is 0.3086, gwgt is 0.6094, and bwgt is 0.0820. This is the luminance vector. Notice here that we do not use the standard NTSC weights of 0.299, 0.587, and 0.114. The NTSC weights are only applicable to RGB colors in a Gamma 2.2 Color Space. for linear RGB Colors The VALUES ABOVE ARE BETTER. IN Effect, This Calculates: Tr = r * rwgt g * GWGT B * BWGT;
TG = r * rwgt g * GWGT B * BWGT;
Tb = r * rwgt g * GWGT B * BWGT;
Modifying Saturationto Saturate RGB Colors, this Matrix is use: float mat [4] [4] = {
A, B, C, 0.0,
D, E, F, 0.0,
G, H, I, 0.0,
0.0, 0.0, 0.0, 1.0,
}
WHERE The Constants Are Derived from The Saturation Value S AS Shown Below: A = (1.0-S) * RWGT S;
B = (1.0-s) * rwgt;
C = (1.0-s) * rwgt;
D = (1.0-s) * GWGT;
E = (1.0-s) * GWGT S;
F = (1.0-s) * GWGT;
g = (1.0-s) * bwgt;
H = (1.0-s) * bwgt;
i = (1.0-s) * bwgt s;
One nice property of this saturation matrix is that the luminance of input RGB colors is maintained. This matrix can also be used to complement the colors in an image by specifying a saturation value of -1.0. Notice that when s is set to 0.0, the matrix is exactly the "convert to luminance" matrix described above. When s is set to 1.0 the matrix becomes the identity. All saturation matrices can be derived by interpolating between or extrapolating beyond these two matrices. This is discussed in more detail in the note on Image Processing By Interpolation and Extrapolation Applying Offsets to Color ComponentsTo offset the r, g, and b components of colors in an image this matrix is used:. float mat [4] [4] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
Roffset, Goffset, Boffset, 1.0,
}
.. This can be used along with color scaling to alter the contrast of RGB images Simple Hue RotationTo rotate the hue, we perform a 3D rotation of RGB colors about the diagonal vector [1.0 1.0 1.0] The transformation matrix is derived as shown here: IF We Have Functions:
Identmat (MAT)
That Creates An Identity Matrix.
XRotatemat (Mat, RSIN, RCOS)
That Multiplies a Matrix That Rotates About The x (red) axis.
YrotateMat (Mat, RSIN, RCOS)
AXIS, That Multiplies A Matrix That Rotates.
ZrotateMat (MAT, RSIN, RCOS)
That Multiplies a Matrix That Rotates About The z (blue) axis.
THEN A Matrix That Rotates About The 1.0,1.0,1.0 Diagonal Can Be Constructed Like this: First We make an identity matrix ideMat (mat);
Rotate The Grey Vector INTO POSITIVE Z MAG = SQRT (2.0);
XRS = 1.0 / mAg;
XRC = 1.0 / mAg;
XROTATEMAT (MAT, XRS, XRC);
MAG = SQRT (3.0);
YRS = -1.0 / mAg; YRC = SQRT (2.0) / MAG;
YROTATEMAT (MAT, YRS, YRC);
Rotate the Hue ZRS = SIN (Rot * Pi / 180.0);
Zrc = COS (ROT * PI / 180.0);
ZrotateMat (MAT, ZRS, ZRC);
Rotate The Grey Vector Back Into Place YrotateMat (MAT, -YRS, YRC);
XROTATEMAT (MAT, -XRS, XRC);
The resulting matrix will rotate the hue of the input RGB colors. A rotation of 120.0 degrees will exactly map Red into Green, Green into Blue and Blue into Red. This transformation has one problem, however, the luminance of the input colors is not preserved ............................ ..
Rotate The Grey Vector INTO POSITIVE Z MAG = SQRT (2.0);
XRS = 1.0 / mAg;
XRC = 1.0 / mAg;
XROTATEMAT (MMAT, XRS, XRC);
MAG = SQRT (3.0);
YRS = -1.0 / mg;
YRC = SQRT (2.0) / MAG;
YROTATEMAT (MMAT, YRS, YRC);
Matrixmult (MMAT, MAT, MAT);
Shear The Space To Make The Luminance Plane Horizontal XFormRGB (MMAT, RWGT, GWGT, BWGT, & LX, & LY, & LZ);
ZSX = lx / lz;
Zsy = LY / LZ;
ZSHEARMAT (MAT, ZSX, ZSY);
Rotate the Hue ZRS = SIN (Rot * Pi / 180.0);
Zrc = COS (ROT * PI / 180.0);
ZrotateMat (MAT, ZRS, ZRC);
Unshear the space to put the luminance plane back zhearmat (MAT, -ZSX, -ZSY);
Rotate The Grey Vector Back Into Place YrotateMat (MAT, -YRS, YRC);
XROTATEMAT (MAT, -XRS, XRC);