Boundary Padding Options for Image Filtering
When calculating an output pixel at the boundary of an image, a portion of the convolution or correlation kernel usually extends past the edge of the image. To perform the element-wise multiplication of the image neighborhood and the kernel, you can specify values for placeholder pixels outside the image. These placeholder pixels are called padding.
The figure demonstrates a situation where a filtering operation needs padding. In the figure, the position of the 3-by-3 kernel is depicted by the light blue box and extends past the edge of the image. The dashed gray boxes indicate padding, for which pixel values must be assigned.
Image filtering functions offer many options for how to specify values for padding. By
default, the imfilter
function assigns the value
0
to padding. This is called zero padding and is illustrated in the
following figure.
Zero padding can result in a dark band around the edge of the filtered image. To avoid zero-padding artifacts, alternative boundary padding methods specify padding values based on image content near the border. For example, in border replication, the value of any pixel outside the image is determined by replicating the value from the nearest border pixel. Border replication is illustrated in the following figure.
Compare Padding Options
This table compares the padding options supported by image filtering functions. The diagrams show a closeup of the 2-by-3 region in the top-left corner of an image. The edge of the image is indicated by the colored double line. Two rows and two columns of padding are added above and to the left of the image. This amount of padding accommodates a 5-by-5 filter.
Padding Option | Description | Diagram |
---|---|---|
0 or "zeros" | All padded pixels are
assigned the value
Zero
padding is the default padding option for the |
|
Constant | All padded pixels are assigned the same value
|
|
"replicate" | The padded pixels replicate the value from the nearest border pixel. Border replication can help reduce boundary artifacts such as edges and contrast compared to padding with zeros or a constant.
|
|
"symmetric" | The padded pixels are reflections of the image across the image border. Symmetric padding helps to remove edge contrast and maintain edge texture.
|
|
"circular" | The padded pixels are copied from the opposite side of the image. The effect gives an appearance of a tiled or looped image. Specify this value when you want to treat an image as periodically repeating, such as when you are performing filtering in the frequency domain. |
|
Note
Some image filtering functions support only a subset of padding options.