# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# fspecial

Create predefined 2-D filter

Use of `fspecial` with the `'gaussian'` syntax is not recommended. Use `imgaussfilt` or `imgaussfilt3` instead.

## Syntax

``h = fspecial(type)``
``h = fspecial('average',hsize)``
``h = fspecial('disk',radius)``
``h = fspecial('gaussian',hsize,sigma)``
``h = fspecial('laplacian',alpha)``
``h = fspecial('log',hsize,sigma)``
``h = fspecial('motion',len,theta)``
``h = fspecial('prewitt')``
``h = fspecial('sobel')``

## Description

example

````h = fspecial(type)` creates a two-dimensional filter `h` of the specified `type`. Some of the filter types have optional additional parameters, shown in the following syntaxes. `fspecial` returns `h` as a correlation kernel, which is the appropriate form to use with `imfilter`.```
````h = fspecial('average',hsize)` returns an averaging filter `h` of size `hsize`.```
````h = fspecial('disk',radius)` returns a circular averaging filter (pillbox) within the square matrix of size `2*radius+1`.```
````h = fspecial('gaussian',hsize,sigma)` returns a rotationally symmetric Gaussian lowpass filter of size `hsize` with standard deviation `sigma` (positive). Not recommended. Use `imgaussfilt` or `imgaussfilt3` instead.```
````h = fspecial('laplacian',alpha)` returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator, `alpha` controls the shape of the Laplacian.```
````h = fspecial('log',hsize,sigma)` returns a rotationally symmetric Laplacian of Gaussian filter of size `hsize` with standard deviation `sigma` (positive). `sigma````
````h = fspecial('motion',len,theta)` returns a filter to approximate, once convolved with an image, the linear motion of a camera by `len` pixels, with an angle of `theta` degrees in a counterclockwise direction. The filter becomes a vector for horizontal and vertical motions. To compute the filter coefficients, `h`, for `'motion'`:Construct an ideal line segment with the desired length and angle, centered at the center coefficient of `h`.For each coefficient location `(i,j)`, compute the nearest distance between that location and the ideal line segment.`h = max(1 - nearest_distance, 0);` Normalize `h`: ```h = h/(sum(h(:)))``````
````h = fspecial('prewitt')` returns the 3-by-3 filter `h` (shown below) that emphasizes horizontal edges by approximating a vertical gradient. If you need to emphasize vertical edges, transpose the filter `h'`. [ 1 1 1 0 0 0 -1 -1 -1 ] ```
````h = fspecial('sobel')` returns a 3-by-3 filter `h` (shown below) that emphasizes horizontal edges using the smoothing effect by approximating a vertical gradient. If you need to emphasize vertical edges, transpose the filter `h'`. [ 1 2 1 0 0 0 -1 -2 -1 ] ```

## Examples

collapse all

```I = imread('cameraman.tif'); imshow(I);```

Create a motion filter and use it to blur the image. Display the blurred image.

```H = fspecial('motion',20,45); MotionBlur = imfilter(I,H,'replicate'); imshow(MotionBlur);```

Create a disk filter and use it to blur the image. Display the blurred image.

```H = fspecial('disk',10); blurred = imfilter(I,H,'replicate'); imshow(blurred);```

## Input Arguments

collapse all

Type of filter, specified as one of the following strings or character vectors.

Value

Description

`'average'`

Averaging filter

`'disk'`

Circular averaging filter (pillbox)

`'gaussian'`

Gaussian lowpass filter. Not recommended. Use `imgaussfilt` or `imgaussfilt3` instead.

`'laplacian'`

Approximates the two-dimensional Laplacian operator

`'log'`

Laplacian of Gaussian filter

`'motion'`

Approximates the linear motion of a camera

`'prewitt'`

Prewitt horizontal edge-emphasizing filter

`'sobel'`

Sobel horizontal edge-emphasizing filter

Example: `h = fspecial('sobel')`

Data Types: `char` | `string`

Size of the filter, specified as a numeric scalar or vector. Use a vector to specify the number of rows and columns in `h`. If you specify a scalar, `h` is a square matrix. When used with the `'average'` filter type, the default filter size is `[3 3]`. When used with the Laplacian of Gaussian (`'log'`) filter type, the default filter size is `[5 5]`.

Data Types: `double`

Radius of a disk-shaped filter, specified as numeric scalar.

Data Types: `double`

Standard deviation, specified as a scalar.

Data Types: `double`

Shape of the Laplacian, specified as a scalar in the range 0.0 to 1.0.

Data Types: `double`

Linear motion of camera, specified as a numeric scalar, measured in pixels. The default `len` is 9 and the default `theta` is 0, which corresponds to a horizontal motion of nine pixels.

Data Types: `double`

Angle of camera motion, specified as a numeric scalar, measured in degrees, in a counter-clockwise direction. The default `len` is 9 and the default `theta` is 0, which corresponds to a horizontal motion of nine pixels.

Data Types: `double`

## Output Arguments

collapse all

Correlation kernel, returned as a matrix of class `double`.

## Algorithms

`fspecial` creates averaging filters using

```ones(n(1),n(2))/(n(1)*n(2)) ```

`fspecial` creates Gaussian filters using

`${h}_{g}\left({n}_{1},{n}_{2}\right)={e}^{\frac{-\left({n}_{1}^{2}+{n}_{2}^{2}\right)}{2{\sigma }^{2}}}$`
`$h\left({n}_{1},{n}_{2}\right)=\frac{{h}_{g}\left({n}_{1},{n}_{2}\right)}{\sum _{{n}_{1}}\sum _{{n}_{2}}{h}_{g}}$`

`fspecial` creates Laplacian filters using

`${\nabla }^{2}=\frac{{\partial }^{2}}{\partial {x}^{2}}+\frac{{\partial }^{2}}{\partial {y}^{2}}$`
`${\nabla }^{2}=\frac{4}{\left(\alpha +1\right)}\left[\begin{array}{ccc}\frac{\alpha }{4}& \frac{1-\alpha }{4}& \frac{\alpha }{4}\\ \frac{1-\alpha }{4}& -1& \frac{1-\alpha }{4}\\ \frac{\alpha }{4}& \frac{1-\alpha }{4}& \frac{\alpha }{4}\end{array}\right]$`

`fspecial` creates Laplacian of Gaussian (LoG) filters using

`${h}_{g}\left({n}_{1},{n}_{2}\right)={e}^{\frac{-\left({n}_{1}^{2}+{n}_{2}^{2}\right)}{2{\sigma }^{2}}}$`
`$h\left({n}_{1},{n}_{2}\right)=\frac{\left({n}_{1}^{2}+{n}_{2}^{2}-2{\sigma }^{2}\right){h}_{g}\left({n}_{1},{n}_{2}\right)}{2\pi {\sigma }^{6}\sum _{{n}_{1}}\sum _{{n}_{2}}{h}_{g}}$`