This is a routine uni project. There are a lot of existing examples on the forum.
Assuming the filter kernel is rectangular and uniform:
- pad the image (padarray()) to accomodate half the width of the filter kernel to overhang
- preallocate the output array to the size of the original image
- indexing by row and column in two nested loops, extract a rectangular region about the current index
- sum the pixel values in that sample
- place the sum in the output array at the current index
- repeat over entire image
- divide the result by the product of the filter kernel geometry to obtain the mean
Assuming that the filter kernel is nonuniform:
- create the filter kernel and normalize its sum (or just use fspecial())
- pad the image (padarray()) to accomodate half the width of the filter kernel to overhang
- preallocate the output array to the size of the original image
- indexing by row and column in two nested loops, extract a rectangular region about the current index
- multiply the sample region and the filter, sum the result
- place the sum in the output array at the current index
- repeat over entire image
Pay attention to image and filter class and data scaling. It will probably be easiest to convert the incoming image to floating-point by using im2double(). Moving the division outside the loop is not necessary, but it can significantly speed up processing. The division is not necessary in the second case, as the division is effected by the normalization of the filter kernel itself.
This is one example, though I admit that the thread title is completely misleading.
This example uses the second method described above. While it may be faster to implement flat filters without the multiplication (i.e. the first method above), this would be the more generalized approach.