This article will talk about the sharpening Effect of image special effect processing. As before, pixels are processed, and algorithms are universal.
Algorithm principle:
I. Simple Algorithm: Obtain the RGB values of the current pixel and the eight surrounding pixels respectively. First, obtain the RGB values of the current pixel and the sum of the RGB values of the eight pixels, multiply by the corresponding coefficient and then sum it with the current pixel.
Example:
ABC
Def
Ghi
Sharpen the E point:
[Java]
View plaincopyprint?
- Float Delta = 0.3;
- E. R = (E. r-(. R + B. R + C. R + D. R + F. R + G. R + H. R + I. r )/
8) * Delta + E. R;
Float Delta = 0.3; <br/> E. R = (E. r-(. R + B. R + C. R + D. R + F. R + G. R + H. R + I. r)/8) * Delta + E. r;
E. g, E. B is similar. We recommend that you set Delta to 0.3. If you have a try, you will find it. However, according to the above principle, the expected results are not met, and the Delta value cannot be changed, so the code will not be posted later. If you are interested, you can study it.
Ii. Laplace transformation: the product of the item in the Laplace matrix and the RGB value of the corresponding vertex is multiplied by the sum of the corresponding coefficients as the RGB value of the current vertex.
For example, use the above example to sharpen the E point.
[Java]
View plaincopyprint?
- // Laplace Matrix
- Int [] Laplacian = new
Int [] {-1,-1,-1,-1,
9,-1,-1,-1,-1 };
- Float Delta = 0.3;
- E. R =. R * Laplacian [0] * Delta + B. R * Laplacian [1] * Delta + C. R * Laplacian [2] * Delta + D. R * Laplacian [3]
* Delta + E. R * Laplacian [4] * Delta + F. R * Laplacian [5] * Delta + G. R * Laplacian [6] * Delta + H. R * Laplacian [7]
* Delta + I. R * Laplacian [8] * delta;
- // The values of e. g and E. B are similar.
// Laplace matrix <br/> int [] Laplacian = new int [] {-1,-1,-1,-1, 9,-1,-1, -1,-1 }; <br/> float Delta = 0.3; <br/> E. R =. R * Laplacian [0] * Delta + B. R * Laplacian [1] * Delta + C. R * Laplacian [2] * Delta + D. R * Laplacian [3] * Delta + E. R * Laplacian [4] * Delta + F. R * Laplacian [5] * Delta + G. R * Laplacian [6] * Delta + H. R * Laplacian [7] * Delta + I. R * Laplacian [8] * delta; <br/> // E. G and E. B value is similar
See the following:
Source image:
After processing:
It seems that there are some problems to solve. There are a lot of vertical lines in the middle. Obviously, it may not be well optimized because the getpiexels () and setpixels () methods are used, therefore, the width and height of the image corresponding to the one-dimensional array are a little troublesome.
The following code is provided only for parameters. Pay attention to the image size. The array size cannot exceed the Virtual Machine value.
/*** Image sharpening (Laplace transformation) * @ Param BMP * @ return */private bitmap sharpenimageameliorate (bitmap BMP) {long start = system. currenttimemillis (); // Laplace matrix int [] Laplacian = new int [] {-1,-1,-1,-1, 9,-1,-1, -1,-1}; int width = BMP. getwidth (); int Height = BMP. getheight (); Bitmap bitmap = bitmap. createbitmap (width, height, bitmap. config. rgb_565); int pixr = 0; int pixg = 0; int pixb = 0; int pixcolor = 0; int newr = 0; int newg = 0; int newb = 0; int idx = 0; float alpha = 0.3f; int [] pixels = new int [width * Height]; BMP. getpixels (pixels, 0, width, 0, 0, width, height); For (INT I = 1, length = height-1; I <length; I ++) {for (int K = 1, Len = width-1; k <Len; k ++) {idx = 0; For (int m =-1; m <= 1; M ++) {for (INT n =-1; n <= 1; n ++) {pixcolor = pixels [(I + n) * width + K + M]; pixr = color. red (pixcolor); pixg = color. green (pixcolor); pixb = color. blue (pixcolor); newr = newr + (INT) (pixr * Laplacian [idx] * alpha); newg = newg + (INT) (pixg * Laplacian [idx] * alpha); newb = newb + (INT) (pixb * Laplacian [idx] * alpha); idx ++;} newr = math. min (255, math. max (0, newr); newg = math. min (255, math. max (0, newg); newb = math. min (255, math. max (0, newb); pixels [I * width + k] = color. argb (255, newr, newg, newb); newr = 0; newg = 0; newb = 0 ;}} bitmap. setpixels (pixels, 0, width, 0, 0, width, height); long end = system. currenttimemillis (); log. D ("may", "used time =" + (end-Start); Return bitmap ;}