How does fspecial average filter deal with overlap causes by edges of an image?

6 views (last 30 days)
I am applying an average filter using the fspecial command. I am wondering how the average filter works on images whose dimensions are not multiples of the user-specified hsize. For example if a 3x3 filter is chosen and applied to a 10x10 pixel image, how does this filter deal with the last row and column?

Accepted Answer

Thorsten
Thorsten on 15 Jan 2013
The filter is always centered on the pixel to be processed. At the boundary, missing values are by default replaced by zero.
imfilter(ones(10), ones(3))
Other boundary conditions can be found at the help of imfilter, e.g., 'replicate', which takes the missing values from the nearest border pixel:
imfilter(ones(10), ones(3), 'replicate')
  2 Comments
B
B on 15 Jan 2013
Ok thanks. To be clear, does the averaging include the pixels diagonal to the pixel to be processed or is it only the four pixels that share an edge with it?
Thorsten
Thorsten on 18 Jan 2013
Edited: Thorsten on 18 Jan 2013
Its includes all 3 x 3 pixels. OUTPUTIMAGE = imfilter(IMAGE, FILTER) works as follows: Center the FILTER on pixel (i, j) of IMAGE. Then weight every pixel of IMAGE with the values in FILTER at corresponding positions and sum all these values. Assign the summed value to OUTPUTIMAGE(i,j). Do this for all pixels in IMAGE.
Please do not forget to accept an answer.

Sign in to comment.

More Answers (2)

David
David on 15 Jan 2013
fspecial will just define the 3x3 filter you're using and imfilter is the function that actually applies it to the image. imfilter deals with the last row and column in the same way whatever the size of the input image.
Don't think of your 10x10 image as being divided in to 3x3 subsets, because it isn't. The filter is applied to every pixel individually: pixel (1,1), then (1,2), then (1,3), and every other pixel. Think of the filter as being applied to overlapping subsets, each position shifted by one pixel.
If you have some curious artefacts at the edge, consider the boundary options (see help imfilter).

Image Analyst
Image Analyst on 18 Jan 2013
fspecial() is not a filter. It is a kernel maker, a filter maker. You then use this kernel (filter) in a function that does the actual filtering, such as imfilter(), or conv2(). There are explanations of the "boundary or edge effects" in the help for those functions. There are different ways you can choose to handle the output value when the filter window falls off the edge of the image.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!