Function File: J = imsmooth(I, name, options)

Smooth the given image using several different algorithms.

The first input argument I is the image to be smoothed. If it is an RGB image, each color plane is treated separately. The variable name must be a string that determines which algorithm will be used in the smoothing. It can be any of the following strings

"Gaussian"

Isotropic Gaussian smoothing. This is the default.

"Average"

Smoothing using a rectangular averaging linear filter.

"Disk"

Smoothing using a circular averaging linear filter.

"Median"

Median filtering.

"Bilateral"

Gaussian bilateral filtering.

"Perona & Malik"
"Perona and Malik"
"P&M"

Smoothing using nonlinear isotropic diffusion as described by Perona and Malik.

"Custom Gaussian"

Gaussian smoothing with a spatially varying covariance matrix.

In all algorithms the computation is done in double precision floating point numbers, but the result has the same type as the input. Also, the size of the smoothed image is the same as the input image.

Gaussian - Isotropic Gaussian smoothing

The image is convolved with a Gaussian filter with spread sigma. By default sigma is 0.5, but this can be changed. If the third input argument is a scalar it is used as the filter spread.

The image is extrapolated symmetrically before the convolution operation.

Average - Rectangular averaging linear filter

The image is convolved with N by M rectangular averaging filter. By default a 3 by 3 filter is used, but this can e changed. If the third input argument is a scalar N a N by N filter is used. If the third input argument is a two-vector [N, M] a N by M filter is used.

The image is extrapolated symmetrically before the convolution operation.

Disk - Circular averaging linear filter

The image is convolved with circular averaging filter. By default the filter has a radius of 5, but this can e changed. If the third input argument is a scalar r the radius will be r.

The image is extrapolated symmetrically before the convolution operation.

Median - Median filtering

Each pixel is replaced with the median of the pixels in the local area. By default, this area is 3 by 3, but this can be changed. If the third input argument is a scalar N the area will be N by N, and if it’s a two-vector [N, M] the area will be N by M.

The image is extrapolated symmetrically before the filtering is performed.

Bilateral - Gaussian bilateral filtering

The image is smoothed using Gaussian bilateral filtering as described by Tomasi and Manduchi [2]. The filtering result is computed as

J(x0, y0) = k * SUM SUM I(x,y) * w(x, y, x0, y0, I(x0,y0), I(x,y))
                 x   y

where k a normalisation variable, and

w(x, y, x0, y0, I(x0,y0), I(x,y))
  = exp(-0.5*d([x0,y0],[x,y])^2/sigma_d^2)
    * exp(-0.5*d(I(x0,y0),I(x,y))^2/sigma_r^2),

with d being the Euclidian distance function. The two parameters sigma_d and sigma_r control the amount of smoothing. sigma_d is the size of the spatial smoothing filter, while sigma_r is the size of the range filter. When sigma_r is large the filter behaves almost like the isotropic Gaussian filter with spread sigma_d, and when it is small edges are preserved better. By default sigma_d is 2, and sigma_r is 10/255 for floating points images (with integer images this is multiplied with the maximal possible value representable by the integer class).

The image is extrapolated symmetrically before the filtering is performed.

Perona and Malik

The image is smoothed using nonlinear isotropic diffusion as described by Perona and Malik [1]. The algorithm iteratively updates the image using

I += lambda * (g(dN).*dN + g(dS).*dS + g(dE).*dE + g(dW).*dW)

where dN is the spatial derivative of the image in the North direction, and so forth. The function g determines the behaviour of the diffusion. If g(x) = 1 this is standard isotropic diffusion.

The above update equation is repeated iter times, which by default is 10 times. If the third input argument is a positive scalar, that number of updates will be performed.

The update parameter lambda affects how much smoothing happens in each iteration. The algorithm can only be proved stable is lambda is between 0 and 0.25, and by default it is 0.25. If the fourth input argument is given this parameter can be changed.

The function g in the update equation determines the type of the result. By default g(d) = exp(-(d./K).^2) where K = 25. This choice gives privileges to high-contrast edges over low-contrast ones. An alternative is to set g(d) = 1./(1 + (d./K).^2), which gives privileges to wide regions over smaller ones. The choice of g can be controlled through the fifth input argument. If it is the string "method1", the first mentioned function is used, and if it is "method2" the second one is used. The argument can also be a function handle, in which case the given function is used. It should be noted that for stability reasons, g should return values between 0 and 1.

The following example shows how to set g(d) = exp(-(d./K).^2) where K = 50. The update will be repeated 25 times, with lambda = 0.25.

g = @(d) exp(-(d./50).^2);
J = imsmooth(I, "p&m", 25, 0.25, g);

Custom Gaussian - Custom Gaussian Smoothing

The image is smoothed using a Gaussian filter with a spatially varying covariance matrix. The third and fourth input arguments contain the Eigenvalues of the covariance matrix, while the fifth contains the rotation of the Gaussian. These arguments can be matrices of the same size as the input image, or scalars. In the last case the scalar is used in all pixels. If the rotation is not given it defaults to zero.

The following example shows how to increase the size of an Gaussian filter, such that it is small near the upper right corner of the image, and large near the lower left corner.

[lambda1, lambda2] = meshgrid (linspace (0, 25, columns (I)), linspace (0, 25, rows (I)));
J = imsmooth (I, "Custom Gaussian", lambda1, lambda2);

The implementation uses an elliptic filter, where only neighbouring pixels with a Mahalanobis’ distance to the current pixel that is less than 3 are used to compute the response. The response is computed using double precision floating points, but the result is of the same class as the input image.

References

[1] P. Perona and J. Malik, "Scale-space and edge detection using anisotropic diffusion", IEEE Transactions on Pattern Analysis and Machine Intelligence, 12(7):629-639, 1990.

[2] C. Tomasi and R. Manduchi, "Bilateral Filtering for Gray and Color Images", Proceedings of the 1998 IEEE International Conference on Computer Vision, Bombay, India.

See also: imfilter, fspecial.

Package: image