I have been trying to do a program that applies a mean filter over images, and I think I am close to do it correctly, but there still small flaws in the images. For instance:

Original racing: <http://s72.photobucket.com/user/john_smith140/media/gp4_zpstafhejk5.jpg.html?filters[user]=139318132&filters[recent]=1&sort=1&o=2>

Original Triangles: <http://s72.photobucket.com/user/john_smith140/media/input_zpsz2cfhrc7.jpeg.html?filters[user]=139318132&filters[recent]=1&sort=1&o=3>

Modified racing: <http://s72.photobucket.com/user/john_smith140/media/racing_zpsmzmawjml.jpeg.html?filters[user]=139318132&filters[recent]=1&sort=1&o=0>

Modififed triangles: <http://s72.photobucket.com/user/john_smith140/media/triangles_zpsaretjfid.jpeg.html?filters[user]=139318132&filters[recent]=1&sort=1&o=1>

black background white dots, original: <http://s72.photobucket.com/user/john_smith140/media/black%20background%20white%20dots_zpsuofaagnl.jpg.html?sort=3&o=2>

black background white dots, same array: <http://s72.photobucket.com/user/john_smith140/media/one%20array_zpswteno2eb.jpg.html?sort=3&o=1>

black background white dots, different arrays: <http://s72.photobucket.com/user/john_smith140/media/two%20array_zpskbyjg97o.jpg.html?sort=3&o=0>

I can think into two causes for the flaws. One the algorithm itself and the other in the process of convert a char to float and then float to char again.

Char to float conversion it is necessary because the read function of ifstream reads char and then I need to multiply every by 1/9, so it needs to be floating point. Then convert back to char so the write function can write it back.

Some explanations about the algorithm. I start to calculate the color value from the second pixel of the second row and then proceeds until the second last pixel of the second last row. That's because I am using a kernel of 3x3, that way I don't go beyond the limits of the image (and so of the char array in which I stored it). For a image of 1024x768, it will have size of 1024x768\*3 (3 color components). So it starts from position: bitsPerPixel \* image\_width + bitsPerPixel or 3\*1024+ 3 = 4099, the 2° pixel of the 2° row. Then it will calculate the mean until the 2° last pixel of the 2° last row which should be:
imageSize - row\_size - bitsPerPixel or (1024\*768-3) - 1024\*3 - 3. 
In the interval it will calculate the value of every position in the char array, which means the value of each color channel of a pixel will be calculated by the color channel of the surrounding pixels.
Here is the code:

```
int size2 = bpp*width;
float a1_9 = (1.0f/9.0f);
float tmp;
for (int i=size2+bpp; i<size-size2-bpp; i++) {
tmp = a1_9 * ((float) image [i-size2-bpp] + (float) image [i-size2] + (float) image [i-size2+bpp] + (float) image [i-bpp] + (float) image [i] + (float) image [i+bpp] + (float)image [i+size2-bpp] + (float) image [i+size2] + (float) image [i+size2+bpp]);
    image [i] = char (tmp);
    float temp = (float) image [i];
}

```

I printed the values for one interaction of the racing car screenshot, corresponding to the values of the position one million and got this:

```
Image values are: -56 -57 -57 9 -43 -41 108 108 109 
tmp it is: 8.88889
temp it is: 8

```

The values seems about right at first glance (doing the average on hand), so I don't have much of an idea of what's going wrong. Any help will be appreciated.