# (Manual) median filter

[u]Hi there![/u]

I have an assignment that i need some help with.
I have to create a function:

This function should compute the mean pixel color for all sub-images (or sub-matrices) of size [b]p x p[/b] pixels centered around each pixel in input image [b]X[/b], where [b]p = 2d + 1[/b] and [b]d = {0,1,2,3...}[/b] is an input argument to the function. The function should handle RGB and grayscale images, and it should contain an additional input argument that specifies one of two options as to how the border on the image is handled.

In the first option, the border pixels of [b]Y[/b] (the pixels where you cannot compute a mean color for a full [b]p x p[/b] sub-image in [b]X[/b]) should be set to the original [b]X[/b] pixel color. All other pixels in output image Y should maintain the mean pixel color found for the corresponding sub-image in [b]X[/b].

[u][italic]Example (d = 1,grayscale image):[/italic][/u]
X =
7 7 8 4 5
7 1 7 4 5
4 3 3 7 6
6 1 9 8 7
2 1 1 2 7

Y =
7 7 8 4 5
7 5 5 5 5
4 5 5 6 6
6 3 4 6 7
2 1 1 2 7

In the second option, a mean color value is computed for the border pixels in [b]Y[/b] by adjusting the size of the corresponding sub-image [b]p x p[/b] in [b]X[/b] accordingly (to ensure that you do not attempt to access a matrix element that does not exist).

[italic][u]Example (d = 1, grayscale image):[/u][/italic]
X =
7 7 8 4 5
7 1 7 4 5
4 3 3 7 6
6 1 9 8 7
2 1 1 2 7

Y =
6 6 5 6 5
5 5 5 5 5
4 5 5 6 6
3 3 4 6 6
3 3 4 6 6

If anyone could tell me how to approach this, i would appreciate it very much!