# 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 ${\displaystyle 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. ${\displaystyle RGB:=\{0,1,\ldots ,255\}^{3}}$ .

A single pixel at row r and column c is denoted as ${\displaystyle I_{(r,c)}}$ . If we define ${\displaystyle I_{(r,c)}:=(255,153,102)}$  then the

• intensity of red is ${\displaystyle I_{(r,c)}.R=255}$ ,
• intensity of green is ${\displaystyle I_{(r,c)}.G=153}$ ,
• intensity of red is ${\displaystyle 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 ${\displaystyle 2\times 2}$  sub matrix of the image I for the four pixels:

• ${\displaystyle I_{(r,c)}:=(250,103,21)}$  ${\displaystyle I_{(r,c+1)}:=(230,153,102)}$
• ${\displaystyle I_{(r+1,c)}:=(255,50,12)}$  ${\displaystyle I_{(r+1,c+1)}:=(151,30,20)}$

The calculated moving average for this square is:

• Red: ${\displaystyle A_{red}:=round\left({\frac {250+230+255+151}{4}}\right)=222}$
• Green: ${\displaystyle A_{green}:=round\left({\frac {103+153+50+30}{4}}\right)=84}$
• Blue: ${\displaystyle A_{blue}:=round\left({\frac {21+102+12+20}{4}}\right)=39}$

The calculate moving average for the ${\displaystyle 2\times 2}$  sub matrix of the image I will replace all original colors of the square. Let ${\displaystyle IMA\in Mat(m\times n,RGB)}$  the image with the moving average applied for all ${\displaystyle 2\times 2}$  sub matrices, then the selected sub matrix above in IMA will look like this:

• ${\displaystyle IMA_{(r,c)}:=(222,84,39)}$  ${\displaystyle IMA_{(r,c+1)}:=(222,84,39)}$
• ${\displaystyle IMA_{(r+1,c)}:=(222,84,39)}$  ${\displaystyle IMA_{(r+1,c+1)}:=(222,84,39)}$

The last step assigns the calculated average color rgb(222, 84, 39) to all pixels of the 2x2-square submatrix.

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 ${\displaystyle V:=\mathbb {Z} \times \mathbb {Z} }$  with the neutral element ${\displaystyle 0_{V}:=(0,0)}$  as an additive group with addition:

${\displaystyle (v_{1},v_{2})+(w_{1},w_{2}):=(v_{1}+w_{1},v_{2}+w_{2})}$  and ${\displaystyle 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 ${\displaystyle R_{i}}$ . The moving average is calculated for all pixels in the rectangle ${\displaystyle R_{i}}$  similar to ${\displaystyle 2\times 2}$  mentioned above. The calculated moving average from the original image I is assigned to all pixels of the square/rectangle ${\displaystyle R_{i}}$  in IMA. If the width and height of the rectangles ${\displaystyle 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.