Applying a 'mean filter' I am just trying to apply a "mean filter" with C#. The working code below gets window size (3,5,7,9,...) from textBoxWinSize and applies the mean filter.

I am new and I want your comments and suggestions about my code so I can improve myself.

Can I make it faster? How? Is there any unnecessary or absurd part in the code? Can it be improved in any other way?

I do not care about border pixels for now.

private void buttonApplyFilter_Click(object sender, EventArgs e)
{
    int R, G, B,aveR,aveG,aveB;
    double sumR,sumG,sumB;
    Color pixel_value;
    Bitmap bmp = new Bitmap(bmpOrig);
    int WinSize = Int32.Parse(textBoxWinSize.Text);
    int col,row,n,m;
    for (col = (WinSize-1); col &lt; (bmp.Width-WinSize); col++)
        for (row = (WinSize-1); row &lt; (bmp.Height-WinSize); row++)
        {
            sumB = 0;
            sumG = 0;
            sumR = 0;
            for (m=-(WinSize-1)/2; m&lt;=(WinSize-1)/2;m++)
                for (n=-(WinSize-1)/2;n&lt;=(WinSize-1)/2;n++)
                {
                    pixel_value = bmp.GetPixel(col+m, row+n);
                    R = (pixel_value.R);
                    G =(pixel_value.G);
                    B = (pixel_value.B);
                    sumR = sumR + R;
                    sumG = sumG + G;
                    sumB = sumB + B;
                }
            aveR = (int)(sumR / (WinSize * WinSize));
            aveG = (int)( sumG / (WinSize * WinSize)); 
            aveB = (int)( sumB / (WinSize * WinSize));
            bmp.SetPixel(col, row, Color.FromArgb(aveR, aveG, aveB));
        }
    pictureBox1.Image = bmp;

}
First up, I think you're doing too much in the button click handler. The logic for applying the mean filter should be moved to its own method, something like

private static Bitmap ApplyMeanFilter(Bitmap input, int windowSize)


Now it can be re-used and unit tested more easily.



I think this line is wrong


pixel_value = bmp.GetPixel(col+m, row+n)



in that bmp should be bmpOrig. The reasoning is that you're changing pixels in bmp as you go, which will have an effect on pixels that are processed later. That is, they won't be assigned the mean of their original surrounding pixels.



Assuming the bmp should be bmpOrig, we can optimise this quite a bit.

Say that instead of a bitmap, we have a 2D array of ints.

input

\begin{array}{c | c c c c}
  &amp; 0 &amp; 1 &amp; 2 &amp; 3 \\
\hline
0 &amp; 1 &amp; 2 &amp; 3 &amp; -1 \\
1 &amp; 4 &amp; 1 &amp; 6 &amp; -3 \\
2 &amp; 4 &amp; 5 &amp; 9 &amp; -3 \\
3 &amp; 4 &amp; -2 &amp; 6 &amp; -3 
\end{array}

We can compute another 2D array such that the \$(i, j)\$-th entry is the sum of entries in the submatrix \$(0, 0), (i, j)\$:

var sums = new int[height, width];
for (var i = 0; i &lt; height; i++)
{
    var sum = 0;
    for (var j = 0; j &lt; width; j++)
    {
        sum += input[i, j];
        sums[i, j] = i &gt; 0 ? sums[i - 1, j] + sum : sum;
    }
}


sums

\begin{array}{c | c c c c}
  &amp; 0 &amp; 1 &amp; 2 &amp; 3 \\
\hline
0 &amp; 1 &amp; 3 &amp; 6 &amp; 5 \\
1 &amp; 5 &amp; 8 &amp; 17 &amp; 13 \\
2 &amp; 9 &amp; 17 &amp; 35 &amp; 28 \\
3 &amp; 13 &amp; 19 &amp; 43 &amp; 33 
\end{array}

Now suppose we want to find the sum of the values in the 3x3 square around entry \$(i, j) = (2, 2)\$:

\begin{array}{c | c c c c}
  &amp; 0 &amp; 1 &amp; 2 &amp; 3 \\
\hline
0 &amp; 1 &amp; 2 &amp; 3 &amp; -1 \\
1 &amp; 4 &amp; 1 &amp; 6 &amp; -3 \\
2 &amp; 4 &amp; 5 &amp; \textbf{9} &amp; -3 \\
3 &amp; 4 &amp; -2 &amp; 6 &amp; -3 
\end{array}

We can do this quickly using the matrix we just computed. The answer is given by

  sums[3, 3] - sums[3, 0] - sums[0, 3] + sums[0, 0]
= 33 - 13 - 5 + 1
= 16


In general, if halfWindow = (windowSize - 1) / 2 then the sum of values in the windowSize * windowSize square around entry \$(i, j)\$ is given by

sums[i + halfWindow,     j + halfWindow] -
sums[i - halfWindow - 1, j + halfWindow] -
sums[i + halfWindow,     j - halfWindow - 1] +
sums[i - halfWindow - 1, j - halfWindow - 1]


The beauty is, computing the sum of the surrounding windowSize * windowSize values this way is independent of windowSize.



Getting back to the original problem of bitmaps, let's suppose we have a struct that will let us sum up the RGB values of the pixels. I named it Clr because I'm not very imaginative today; I'm sure you can think of a better name. Let's also assume we have operators +, - and / defined in the obvious way.

Putting it all together would look something like this

var sums = new Clr[input.Height, input.Width];
for (var i = 0; i &lt; input.Height; i++)
{
    var sum = new Clr();
    for (var j = 0; j &lt; input.Width; j++)
    {
        sum += Clr.FromColor(input.GetPixel(j, i));
        sums[i, j] = i &gt; 0 ? sums[i - 1, j] + sum : sum;
    }
}

var output = new Bitmap(input);
var windowArea = windowSize * windowSize;
var halfWindow = (windowSize - 1) / 2;
for (var i = windowSize - 1; i &lt; input.Height - windowSize; i++)
{
    for (var j = windowSize - 1; j &lt; input.Width - windowSize; j++)
    {
        var clr = sums[i + halfWindow,     j + halfWindow] -
                  sums[i - halfWindow - 1, j + halfWindow] -
                  sums[i + halfWindow,     j - halfWindow - 1] +
                  sums[i - halfWindow - 1, j - halfWindow - 1];

        output.SetPixel(j, i, (clr / windowArea).ToColor());
    }
}

return output;


Running this on a 256x256 image, I got the following timings

windowSize       this               original
 3          00:00:00.0910750    00:00:00.4720078
 5          00:00:00.0899366    00:00:01.2674758
 7          00:00:00.0853111    00:00:02.2569419
 9          00:00:00.0938911    00:00:03.4408123
11          00:00:00.0840854    00:00:04.9614386




Other things to try that might improve performance:


Use a Clr[][] instead of a Clr[,]. See Why are multi-dimensional
arrays in .NET slower than normal
arrays?
Doing the above will let you move sums[i + halfWindow] and sums[i - halfWindow - 1] into variables outside the inner loop.
Compute the first row of sums separately to remove the conditional in this line sums[i, j] = i &gt; 0 ? sums[i - 1, j] + sum : sum;