Main Content

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 OptionDescriptionDiagram
0 or "zeros"

All padded pixels are assigned the value 0.

Zero padding is the default padding option for the imfilter, padarray, medfilt2, and ordfilt2 functions.

Two rows and columns of padding pixels with the value 0 are appended to the top and left of an image.

Constant

All padded pixels are assigned the same value C that you specify. C can be a number for numeric and logical images, or a string scalar or character vector for categorical images.

Two rows and columns of padding pixels with the value "C" are appended to the top and left of an image.

C is the constant value assigned to the padding.

"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.

"replicate" is the default padding option for the imgaussfilt and imgaussfilt3 functions.

"symmetric"

The padded pixels are reflections of the image across the image border. Symmetric padding helps to remove edge contrast and maintain edge texture.

"symmetric" is the default padding option for the medfilt3 and modefilt functions. This is the only padding option for the stdfilt and entropyfilt functions.

"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.

See Also

Topics