Matrix Operations for Image ProcessingPaul HaeberliNov 1993IntroductionFour 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 are used. Color TransformationRGB colors are transformed 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 them unchanged.
Changing BrightnessTo scale RGB colors a matrix like this is used:
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 b
components of colors. This can be used to alter the color balance of an image.
In effect, this calculates: tr = r*rscale; tg = g*gscale; tb = b*bscale; Modifying SaturationConverting to LuminanceTo convert a color image into a black and white image, this matrix is used:
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 used:
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 This is discussed in more detail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the tail in the t |