Matlab commands for filtering

I have compiled some excerpts from the Matlab help manual for your reference. For more details, please consult the users' manual for the image processing toolbox.

[FSPECIAL command] [FILTER2 command] [EDGE command]

You may wish to try the following example to see how Matlab can create an embossing effect using Sobel filtering:

    Example
    -------
	load trees
        I = ind2gray(X,map);
        h = fspecial('sobel');	  /* creates a 3-by-3 horizontal Sobel filter */
        I2 = filter2(h,I);        /* carries out filtering using the Sobel filter */
        imshow(mat2gray(I2),64)
Embossed Image

Matlab provides the FSPECIAL command to create some well known filters, including the Sobel filter and the Log-of-Gaussian filter. The syntax is as below:

 FSPECIAL Create predefined filters.
    H = FSPECIAL(TYPE) creates a two-dimensional filter H of the
    specified type. (FSPECIAL returns H as a computational
    molecule, which is the appropriate form to use with FILTER2.)
    TYPE is a string having one of these values:
 
         'gaussian'  for a Gaussian lowpass filter
         'sobel'     for a Sobel horizontal edge-emphasizing
                        filter 
         'prewitt'   for a Prewitt horizontal edge-emphasizing
                        filter 
         'laplacian' for a filter approximating the
                        two-dimensional Laplacian operator
         'log'       for a Laplacian of Gaussian filter
         'average'   for an averaging filter
         'unsharp'   for an unsharp contrast enhancement filter
 
    Depending on TYPE, FSPECIAL can take additional parameters
    which you can supply.  These parameters all have default
    values. 
 
    H = FSPECIAL('gaussian',N,SIGMA) returns a rotationally
    symmetric Gaussian lowpass filter with standard deviation
    SIGMA (in pixels). N is a 1-by-2 vector specifying the number
    of rows and columns in H. (N can also be a scalar, in which
    case H is N-by-N.) If you do not specify the parameters,
    FSPECIAL uses the default values of [3 3] for N and 0.5 for
    SIGMA.
 
    H = FSPECIAL('sobel') returns this 3-by-3 horizontal edge
    finding and y-derivative approximation filter:
 
        [1 2 1;0 0 0;-1 -2 -1].
 
    To find vertical edges, or for x-derivates, use -h'.
 
    H = FSPECIAL('prewitt') returns this 3-by-3 horizontal edge
    finding and y-derivative approximation filter:
 
        [1 1 1;0 0 0;-1 -1 -1].
 
    To find vertical edges, or for x-derivates, use -h'.
 
    H = FSPECIAL('laplacian',ALPHA) returns a 3-by-3 filter
    approximating the shape of the two-dimensional Laplacian
    operator. The parameter ALPHA controls the shape of the
    Laplacian and must be in the range 0.0 to 1.0. FSPECIAL uses
    the default value of 0.2 if you do not specify ALPHA.
 
    H = FSPECIAL('log',N,SIGMA) returns a rotationally symmetric
    Laplacian of Gaussian filter with standard deviation SIGMA
    (in pixels). N is a 1-by-2 vector specifying the number of
    rows and columns in H. (N can also be a scalar, in which case
    H is N-by-N.) If you do not specify the parameters, FSPECIAL
    uses the default values of [5 5] for N and 0.5 for SIGMA.
 
    H = FSPECIAL('average',N) returns an averaging filter. N is a
    1-by-2 vector specifying the number of rows and columns in
    H. (N can also be a scalar, in which case H is N-by-N.) If
    you do not specify N, FSPECIAL uses the default value of 
    [3 3].
 
    H = FSPECIAL('unsharp',ALPHA) returns a 3-by-3 unsharp
    contrast enhancement filter. FSPECIAL creates the unsharp
    filter from the negative of the Laplacian filter with
    parameter ALPHA. ALPHA controls the shape of the Laplacian
    and must be in the range 0.0 to 1.0. FSPECIAL uses the
    default value of 0.2 if you do not specify ALPHA.
 
    Example
    -------
        I = imread('saturn.tif');
        h = fspecial('unsharp',0.5);
        I2 = filter2(h,I)/255;
        imshow(I), figure, imshow(I2)
 
    See also CONV2, EDGE, FILTER2, FSAMP2, FWIND1, FWIND2.


Matlab provides the FILTER2 command to carry out 2-dimensional filtering using a filter (or mask) of your choice. You may supply your own filter, or of course, you may use special filters provided by Matlab using the FSPECIAL command. The syntax is as below:

 FILTER2 Two-dimensional digital filter.
    Y = FILTER2(B,X) filters the data in X with the 2-D FIR
    filter in the matrix B.  The result, Y, is computed 
    using 2-D correlation and is the same size as X. 
 
    Y = FILTER2(B,X,'shape') returns Y computed via 2-D
    correlation with size specified by 'shape':
      'same'  - (default) returns the central part of the 
                correlation that is the same size as X.
      'valid' - returns only those parts of the correlation
                that are computed without the zero-padded
                edges, size(Y) < size(X).
      'full'  - returns the full 2-D correlation, 
                size(Y) > size(X).
 
    FILTER2 uses CONV2 to do most of the work.  2-D correlation
    is related to 2-D convolution by a 180 degree rotation of the
    filter matrix (i.e. reflection about the x and y axes).
 
    See also FILTER, CONV2.

 Overloaded methods
    help uint8/filter2.m


To find edges, you may use the FILTER2 command in conjunction with the Sobel or LOG filters created using the FSPECIAL command. However, Matlab makes your work even simpler because it provides some special edge finding commands for you.

 EDGE Find edges in intensity image.
    BW = EDGE(I,METHOD) returns a binary image BW of the same
    size as I, with 1's where the function finds edges in I and
    0's elsewhere.
 
    METHOD is a string having one of these values:
    
      'sobel'     (default) finds edges using the Sobel
                  approximation to the derivative. It returns
                  edges at those points where the gradient of I
                  is maximum.
 
      'prewitt'   finds edges using the Prewitt approximation to 
                  the derivative. It returns edges at those
                  points where the gradient of I is maximum.
 
      'roberts'   finds edges using the Roberts approximation to 
                  the derivative. It returns edges at those
                  points where the gradient of I is maximum.
 
      'log'       finds edges by looking for zero crossings after
                  filtering I with a Laplacian of Gaussian
                  filter.
 
      'zerocross' finds edges by looking for zero crossings after
                  filtering I with a filter you specify.
 
    BW = EDGE(I,METHOD,THRESH) specifies the sensitivity
    threshold. EDGE ignores all edges that are not stronger than
    THRESH. If you do not specify THRESH, EDGE chooses the value
    automatically.
 
    BW = EDGE(I,METHOD,THRESH,DIRECTION) specifies directionality
    for the 'sobel' and 'prewitt' methods. DIRECTION is a string
    specifying whether to look for 'horizontal' or 'vertical'
    edges, or 'both' (the default).
 
    BW = EDGE(I,'log',THRESH,SIGMA) specifies the 'log' method,
    using SIGMA as the standard deviation of the Laplacian of
    Gaussian filter. The default SIGMA is 2; the size of the
    filter is N-by-N, where N=CEIL(SIGMA*2)*2+1. If THRESH is
    empty ([]), EDGE chooses the sensitivity threshold
    automatically.
 
    BW = EDGE(I,'zerocross',THRESH,H) specifies the 'zerocross'
    method, using the specified filter H. If THRESH is empty
    ([]), EDGE chooses the sensitivity threshold automatically.
 
    For the 'log' and 'zerocross' methods, if you specify a
    threshold of 0, the output image has closed contours, because
    it includes all of the zero crossings in the input image.
 
    [BW,THRESH] = EDGE(...) returns the sensitivity threshold.
 
    Class Support
    -------------
    I can be of class uint8 or double. BW is of class uint8.
 
    Example
    -------
    Find the edges of the alumgrns.tif image using the Roberts
    method:
 
        I = imread('alumgrns.tif');
        BW = edge(I,'roberts');
        imshow(I), figure, imshow(BW)
Original ImageEdge Map
 
    See also FSPECIAL.


[Return to FSPECIAL] [Return to FILTER2] [Return to EDGE]