Rapid mean Blur for image processing (box blur)
The essence of Image Blur: From the Perspective of digital signal processing, image blur requires suppression of high-frequency signals to retain low-frequency signals,
Convolution filtering is an alternative method to suppress high-frequency signals. Select a low-frequency filter
Each pixel is used for low-frequency filtering, so that the overall effect is that a digital image is more blurred and shows less details.
Traditional convolutional fuzzy algorithms have a huge amount of computing and low program efficiency. Box Blur Based on sliding windows is a fast fuzzy method,
The result is similar to the result of convolution. I have never proved it!
I. Mathematical Principles of box blur
Calculate the average value of the first 2 * r + 1 pixel based on the input radius R as the result of the first output pixel,
The formula can be expressed
Pixel X0 = k indicates the set of input pixels. The value range of I is iε [-R, R].
Then calculate the output pixel value of each row according to xi = x0 + (K [index + R + 1]-K [index-R])
Ii. Features of box blur
Box blur is a fast image blur technology. Compared with traditional convolution fuzzy, box blur can be more efficient.
Blur the image. The degree of blur depends on the three input parameters,
1. X-direction upper radius H radius
2. V radius in the Y direction
3. Number of iterations iteration number
When the radius is the same, the more iterations, the blurrier the output image.
When the number of iterations is the same, the larger the pixel radius, the blurrier the output image.
The difference between the two is the image stretching effect, the larger the radius, the more significant the image stretching Effect
Box blur uses the Sliding Window Algorithm to simplify the overhead of each average calculation.
From the perspective of digital image and signal processing, box blur is an uncompromising low-pass filter, but it does not
It is a real Gaussian low-pass filter, which is faster than convolution.
When the horizontal and vertical radius are respectively 1, it is a typical 3*3 matrix convolution.
1, 1, 1
1, 1, 1
1, 1, 1
Compared with traditional convolutional computing, the box computing process has completed pixel average,
Normalization is not required.
3. box blur effect based on Sliding Window Algorithm
Horizontal and vertical
Vertical direction:
Horizontal Direction:
4. key code parsing
The comment has been written in detail in the Code. The most important step is to create an index in advance and locate the average value based on the index.
public static void blur( int[] in, int[] out, int width, int height, int radius ) { int widthMinus1 = width-1; int tableSize = 2*radius+1; int divide[] = new int[256*tableSize]; // the value scope will be 0 to 255, and number of 0 is table size // will get means from index not calculate result again since // color value must be between 0 and 255. for ( int i = 0; i < 256*tableSize; i++ ) divide[i] = i/tableSize; int inIndex = 0; // for ( int y = 0; y < height; y++ ) { int outIndex = y; int ta = 0, tr = 0, tg = 0, tb = 0; // ARGB -> prepare for the alpha, red, green, blue color value. for ( int i = -radius; i <= radius; i++ ) { int rgb = in[inIndex + ImageMath.clamp(i, 0, width-1)]; // read input pixel data here. table size data. ta += (rgb >> 24) & 0xff; tr += (rgb >> 16) & 0xff; tg += (rgb >> 8) & 0xff; tb += rgb & 0xff; } for ( int x = 0; x < width; x++ ) { // get output pixel data. out[ outIndex ] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8) | divide[tb]; // calculate the output data. int i1 = x+radius+1; if ( i1 > widthMinus1 ) i1 = widthMinus1; int i2 = x-radius; if ( i2 < 0 ) i2 = 0; int rgb1 = in[inIndex+i1]; int rgb2 = in[inIndex+i2]; ta += ((rgb1 >> 24) & 0xff)-((rgb2 >> 24) & 0xff); tr += ((rgb1 & 0xff0000)-(rgb2 & 0xff0000)) >> 16; tg += ((rgb1 & 0xff00)-(rgb2 & 0xff00)) >> 8; tb += (rgb1 & 0xff)-(rgb2 & 0xff); outIndex += height; // per column or per row as cycle... } inIndex += width; // next (i+ column number * n, n=1....n-1) } }