# Is image padding really necessary while using a filter?

I do know that we must pad an image while using a filter. But what I am suggesting is that is it really necessary? Can't we just calculate at run time if the current cell/pixel is an edge cell/pixel and then use the padding value directly over there. It might add an overhead to the function but so does the entire padding function.

Only reason I think the padding function should be used is if the padded version of original image is going to be used in many different function which will overtake the overhead of dynamically calculating the edge cell/pixel values(which might never occur). Am I wrong?

• Welcome to SE.DSP! It depends how you are calculating the filtering. If you are using an FFT, then you must pad, otherwise you'll get "time aliasing" from the use of circular convolution by the FFT. If your image is $N\times M$ and your filter kernel is $P \times Q$ then your padding must be at least $(N+P-1) \times (M+Q-1)$ to get the expected linear convolution result. – Peter K. May 19 '17 at 15:12

## 1 Answer

Adding code to your filter function to detect and resolve edges implies code running needlessly throughout most of the image. Image area grows quadratically with image size.

Padding grows linearly with image size (as does the perimeter), so adding padding instead of edge-detection code seems a more efficient option.

Also, when adding padding, you are free to choose what to fill the padding with, according to your particular problem. You may choose to fill in with a constant value, or mirror the image, or get overlap data from adjacent images, etc.