Moving Average/Image Processing

Moving average applied on images


A weighted average is an average that has multiplying factors to give different weights to data at different positions in the sample window. Mathematically, the moving average is the convolution of the datum points with a fixed weighting function. One application is creating a pixelisation from a digital graphical image. For all the image on the right pixelisation is applied for several squares. All pixels in the square are replaced by the color average of all pixels in the square. Because colors are defined by three integer numbers so that color average must be rounded for that application. In order to understand color encoding with integer numbers see HTML Color Picker with the RGB color encoding. Three value between 0 and 255 (e.g. rgb(255, 153, 102) for light orange) encode a color. Due to the fact that the HTML colors for Red, Green, Blue (RGB) are integer numbers, the real values of the moving average are rounded as a technical constraint.

The image I with m pixels height and n pixels width is s matrix $$I \in Mat(m \times n, RGB)$$ where all components of the matrix are RGB triples of integer values between 0 and 255, i.e. $$RGB:=\{0,1,\ldots, 255\}^3$$.

A single pixel at row r and column c is denoted as $$I_{(r,c)}$$. If we define $$I_{(r,c)}:=(255, 153, 102)$$ then the
 * intensity of red is $$I_{(r,c)}.R =255$$,
 * intensity of green is $$I_{(r,c)}.G =153$$,
 * intensity of red is $$I_{(r,c)}.B =102$$

If we calculate and average of colors, we calculate the average of red, green and blue separately. As an example we calculate an average of $$2 \times 2$$ sub matrix of the image I for the four pixels: The calculated moving average for this square is: The calculate moving average for the $$2 \times 2$$ sub matrix of the image I will replace all original colors of the square. Let $$IMA \in Mat(m \times n, RGB)$$ the image with the moving average applied for all $$2 \times 2$$ sub matrices, then the selected sub matrix above in IMA will look like this: The last step assigns the calculated average color rgb(222, 84, 39) to all pixels of the 2x2-square submatrix.
 * $$I_{(r,c)}:=(250, 103, 21)$$ $$I_{(r,c+1)}:=(230, 153, 102)$$
 * $$I_{(r+1,c)}:=(255, 50, 12)$$ $$I_{(r+1,c+1)}:=(151, 30, 20)$$
 * Red: $$A_{red}:=round\left(\frac{250+230+255+151}{4}\right) = 222$$
 * Green: $$A_{green}:=round\left(\frac{103+153+50+30}{4}\right) = 84$$
 * Blue: $$A_{blue}:=round\left(\frac{21+102+12+20}{4}\right) = 39$$
 * $$IMA_{(r,c)}:=(222, 84, 39)$$ $$IMA_{(r,c+1)}:=(222, 84, 39)$$
 * $$IMA_{(r+1,c)}:=(222, 84, 39)$$ $$IMA_{(r+1,c+1)}:=(222, 84, 39)$$

Looking at the example image on right, the application of the moving average are visible, because they are applied on a large submatrix of the image.

For the image processing $$V:=\Z \times \Z$$ with the neutral element $$0_V:=(0,0)$$ as an additive group with addition:
 * $$(v_1,v_2)+(w_1,w_2):=(v_1+w_1,v_2+w_2)$$ and $$T:=\{1,\ldots ,m\} \times \{1,\ldots ,n\} \subset V$$

T is the set of all row and column indices of the pixels. The images is decomposed the squares or even rectangles $$R_i$$. The moving average is calculated for all pixels in the rectangle $$R_i$$ similar to $$2 \times 2$$ mentioned above. The calculated moving average from the original image I is assigned to all pixels of the square/rectangle $$R_i$$ in IMA. If the width and height of the rectangles $$R_i$$ have in general a default size. Close the borders of the images, the sizes of these rectangles have to be adapted to the remaining pixels at the right and bottom border of the image I.