Guided filtering of images
Read and display an image.
A = imread('pout.tif'); imshow(A)
Smooth the image using
imguidedfilter. In this syntax,
imguidedfilter uses the image itself as the guidance image.
Iguided = imguidedfilter(A);
For comparison, smooth the original image using a Gaussian filter defined by
imgaussfilt. Set the standard deviation of the filter to 2.5 so that the degree of smoothing approximately matches that of the guided filter.
Igaussian = imgaussfilt(A,2);
Display the result of guided filtering and the result of Gaussian filtering. Observe that the flat regions of the two filtered images, such as the jacket and the face, have similar amounts of smoothing. However, the guided filtered image better preserves the sharpness of edges, such as around the trellis and the collar of the white shirt.
A— Image to be filtered
Image to be filtered, specified as a binary, grayscale, or RGB image.
G— Image to use as a guide during filtering
Image to use as a guide during filtering, specified as a binary, grayscale, or RGB image of
the same height and width as image
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
Ismooth = imguidedfilter(A,'NeighborhoodSize',[4 4]);
'NeighborhoodSize'— Size of the rectangular neighborhood around each pixel used in guided filtering
[5 5](default) | positive integer | 2-element vector of positive integers
Size of the rectangular neighborhood around each pixel used in guided filtering, specified as
a positive integer or a 2-element vector of
positive integers. If you specify a scalar value,
Q, then the
neighborhood is a square of size
Q]. Do not specify a value greater than
the size of the image.
'DegreeOfSmoothing'— Amount of smoothing
Amount of smoothing in the output image, specified as a positive number. If you specify a small value, only neighborhoods with small variance (uniform areas) will get smoothed and neighborhoods with larger variance (such as around edges) will not be smoothed. If you specify a larger value, high variance neighborhoods, such as stronger edges, will get smoothed in addition to the relatively uniform neighborhoods. Start with the default value, check the results, and adjust the default up or down to achieve the effect you desire.
If you specify a guide image
G, then the default value of
DegreeOfSmoothing depends on
the data type of
G, and is
For example, the default degree of smoothing is
650.25 for images of data type
uint8, and the default is
0.01 for images of data type
double with pixel values in the
range [0, 1]. If you do not specify a guide image,
then the default value depends on the data type of
DegreeOfSmoothing argument specifies
a soft threshold on variance for the given neighborhood. If
a pixel's neighborhood has variance much lower than the
threshold, it will see some amount of smoothing. If a
pixel's neighborhood has variance much higher than the
threshold it will have little to no smoothing.
have different number of channels.
G are RGB images, then
imguidedfilter filters each
using the corresponding channel of
A is an RGB image and
G is a single-channel image,
each channel of
independently using the same guidance image,
A is a single-channel
G is an RGB image,
A using the combined color
statistics of all the three channels of
 Kaiming He, Jian Sun, Xiaoou Tang. Guided Image Filtering. IEEE® Transactions on Pattern Analysis and Machine Intelligence, Volume 35, Issue 6, pp. 1397-1409, June 2013.