UI development process, we often need to deal with the image, common such as maps, some more complex, such as location transformation, rotation, filter effects, and so on, the following is a brief introduction to the image processing of some basic knowledge and principles.
1 Basic Concepts
For the image processing, the most commonly used data structure is bitmap, it contains a picture of all the data, which data including those content? In short, it is composed of lattice and color values, so-called lattice is a concept is a width * height of the matrix, each element corresponds to a pixel of the picture, that is, the dot matrix holds the picture's spatial location information, and the color value is ARGB, respectively, corresponding to the transparency, red, green, Blue These four channel components, each channel is defined with 8 bits, so a color value is an int integer that can represent 256*256*256 color values.
android we used to such a few constants: argb_8888, argb_4444, rgb_565. These constants are actually telling the system how to deal with the color values of the picture, for example, argb_8888 is to tell the system transparency, R, G, b in the color value of 8bit respectively, when the color value of 32bit, so that the definition can represent the most color values, picture quality is also the best; argb_ 4444 is each channel with 4bit, so that the color value is only 16bit, save space, but can only represent 16*16*16 color, that is, the picture is lost a lot of color information; The color value of the rgb_565 type is also 16bit, but it discards the transparency information. can represent 32*64*32 color values.
2 Color matrix
The color matrix is a 5*4 matrix that is used to manipulate image color values. Define the color matrix and color values as follows:
Perform the following matrix operations:
The result R is a 4*1 matrix, which is the new color value, and the values for each channel in R are as follows:
R ' = a*r + b*g + c*b + d*a + E;
G ' = f*r + g*g + h*b + i*a + j;
B ' = k*r + l*g + m*b + n*a + o;
A ' = p*r + q*g + r*b + s*a + t;
This may seem abstract, it is difficult to understand the relationship between the color matrix and the result R directly, we assume that the color matrix values are as follows:
So the result is:
R ' = r;
G ' = g;
B ' = b;
a ' = A;
In other words, the new color value is the same as the original! In another example, the color matrix is evaluated as:
Results:
R ' = r +;
G ' = g + 100;
B ' = b;
a ' = A; In the
new color value, the red channel value and the green channel value are increased by 100 respectively, and the picture is yellowing (because r + G = Yellow).
From the above examples we can easily understand the meaning of each component in the color matrix (each column):
The first line determines the red,
The second row determines the green,
The third line determines the blue,
Row four determines the transparency,
The fifth column is the offset of the color.
At this point we should be able to understand how to change the color values of each component by the color matrix.
Here is a piece of code for Android that handles the picture as a yellowing effect:
public static Bitmap Testbitmap (Bitmap Bitmap) {Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheig HT (), config.rgb_565); Canvas canvas = new canvas (output); Paint paint = new paint (); ColorMatrix cm = new ColorMatrix (); Float[] Array = {1, 0, 0, 0, 100, 0, 1, 0, 0, 100, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0}; Cm.set (array); Paint.setcolorfilter (new Colormatrixcolorfilter (cm)); Canvas.drawbitmap (bitmap, 0, 0, paint); return output; }
Android image processing: color matrix and coordinate transformation matrix