## medfilt2

Main Content

### Description

example

performs median filtering of the image in two dimensions. Each output pixel contains the median value in a 3-by-3 neighborhood around the corresponding pixel in the input image.

performs median filtering, where each output pixel contains the median value in the -by- neighborhood around the corresponding pixel in the input image.

controls how pads the image boundaries.

### Examples

collapse all

### Remove Salt and Pepper Noise from Image

Read image into workspace and display it.

Add salt and pepper noise.

Use a median filter to filter out the noise.

Display results, side-by-side.

### Input Arguments

collapse all

### — Input image

2-D grayscale image | 2-D binary image

Input image, specified as a 2-D grayscale or binary image.

**Data Types: ** | | | | | | | |

### — Neighborhood size

(default) | 2-element vector

Neighborhood size, specified as a 2-element vector of positive integers.

**Data Types: ** | | | | | | | | |

### — Padding option

(default) | |

Padding option, specified as one of the following values.

Value | Description |
---|---|

(default) | Pad the image with s. |

Symmetrically extend the image at the boundaries. | |

If the class of is , then pad the image with s; otherwise, pad with s. |

**Data Types: ** |

### Output Arguments

collapse all

### — Output image

numeric matrix

Output image, returned as a numeric matrix of the same class as the input image .

### Tips

Median filtering is a nonlinear operation often used in image processing to reduce "salt and pepper" noise. A median filter is more effective than convolution when the goal is to simultaneously reduce noise and preserve edges. For information about performance considerations, see .

If the input image is of an integer class, then all the output values are returned as integers. If the number of pixels in the neighborhood () is even, then some of the median values might not be integers. In these cases, the fractional parts are discarded. Logical input is treated similarly. For example, the true median for the following 2-by-2 neighborhood in a array is 4.5, but discards the fractional part and returns 4.

If you specify as or , then the padding can skew the median near the image boundary. Pixels within one-half the width of the neighborhood () of the edges can appear distorted.

### Algorithms

On the CPU, uses to perform the filtering.

### References

[1] Lim, Jae S., *Two-Dimensional Signal and Image Processing*, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 469-476.

### Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

supports the generation of C code (requires MATLAB

^{®}Coder™). Note that if you choose the generic target platform, generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Types of Code Generation Support in Image Processing Toolbox.When generating code, the argument must be a compile-time constant.

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

When generating code, the argument must be a compile-time constant.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

Padding options, specified through the argument, are not supported on the GPU.

If you perform median filtering using a GPU, then the neighborhood must be square with odd-length sides between 3 and 15.

For more information, see Image Processing on a GPU.

**Introduced before R2006a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

## ftrans2

Main Content

### Description

produces the two-dimensional FIR filter that corresponds to the one-dimensional FIR filter using the transform . The transform matrix contains coefficients that define the frequency transformation to use.

example

uses the McClellan transform matrix .

### Examples

collapse all

### Design Circularly Symmetric 2-D Bandpass Filter

Use to design an approximately circularly symmetric two-dimensional bandpass filter with passband between 0.1 and 0.6 (normalized frequency, where 1.0 corresponds to half the sampling frequency, or π radians). Since transforms a one-dimensional FIR filter to create a two-dimensional filter, first design a one-dimensional FIR bandpass filter using the Signal Processing Toolbox function .

Use with the default McClellan transformation to create the desired approximately circularly symmetric filter.

### Input Arguments

collapse all

### — FIR filter

numeric matrix

FIR filter, specified as a numeric matrix. must be a 1-D Type I (even symmetric, odd-length) filter such as can be returned by (Signal Processing Toolbox), (Signal Processing Toolbox), or (Signal Processing Toolbox).

**Data Types: **

### — Transform matrix

numeric matrix

The transform matrix, specified as a numeric matrix. contains coefficients that define the frequency transformation to use. By default, uses a McClellan transform matrix.

**Data Types: **

### Output Arguments

collapse all

### — 2-D FIR filter

numeric matrix

2-D FIR filter, returned as a numeric matrix. returns as a computational molecule, which is the appropriate form to use with . If is -by- and has length , then is size -by-.

### Algorithms

The transformation below defines the frequency response of the two-dimensional filter returned by .

where *B(*ω*)* is the Fourier transform of the one-dimensional filter :

and *T(*ω_{1}*,*ω_{2}*) * is the Fourier transform of the transformation matrix :

The returned filter is the inverse Fourier transform of *H(*ω_{1}*,*ω_{2}*)*:

### References

[1] Lim, Jae S., *Two-Dimensional Signal and Image Processing*, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 218-237.

**Introduced before R2006a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

## imgaussfilt

Main Content

### Description

filters image with a 2-D Gaussian smoothing kernel with standard deviation of 0.5, and returns the filtered image in .

example

filters image with a 2-D Gaussian smoothing kernel with standard deviation specified by .

uses name-value arguments to control aspects of the filtering.

### Examples

collapse all

### Smooth Image with Gaussian Filter

Read image to be filtered.

Filter the image with a Gaussian filter with standard deviation of 2.

Display the original and filtered image in a montage.

### Input Arguments

collapse all

### — Image to be filtered

numeric array

Image to be filtered, specified as a numeric array of any dimension.

**Data Types: ** | | | | | | |

### — Standard deviation of Gaussian distribution

(default) | positive number | 2-element vector of positive numbers

Standard deviation of the Gaussian distribution, specified as a positive number or a 2-element vector of positive numbers. If you specify a scalar, then uses a square Gaussian kernel.

**Data Types: ** | | | | | | | | |

### Name-Value Arguments

Specify optional comma-separated pairs of arguments. is the argument name and is the corresponding value. must appear inside quotes. You can specify several name and value pair arguments in any order as .

**Example:**

### — Size of Gaussian filter

positive, odd integer | 2-element vector of positive, odd integers

Size of the Gaussian filter, specified as a positive, odd integer or 2-element vector of positive, odd integers. If you specify a scalar, then uses a square filter. The default filter size is .

**Data Types: ** | | | | | | | | |

### — Image padding

(default) | numeric scalar | |

Image padding, specified as one of the following.

Value | Description |
---|---|

numeric scalar | Pad image with elements of constant value. |

Pad with circular repetition of elements within the dimension. | |

Pad by repeating border elements of array. | |

Pad image with mirror reflections of itself. |

**Data Types: ** | | | | | | | | | | | |

### — Domain in which to perform filtering

(default) | |

Domain in which to perform filtering, specified as one of the following values:

Value | Description |
---|---|

Perform convolution in the spatial or frequency domain, based on internal heuristics. | |

Perform convolution in the frequency domain. | |

Perform convolution in the spatial domain. |

**Data Types: ** |

### Output Arguments

collapse all

### — Filtered image

numeric array

Filtered image, returned as a numeric array of the same class and size as the input image, .

### Tips

If image contains elements with values or , then the behavior of for frequency domain filtering is undefined. This can happen if you set the name-value argument to or if you set it to and uses frequency domain filtering. To restrict the propagation of s and s in the output in a manner similar to , consider setting the name-value argument to .

If you set the name-value argument to , then uses an internal heuristic to determine whether spatial or frequency domain filtering is faster. This heuristic is machine dependent and may vary for different configurations. For optimal performance, try both options, and , to determine the best filtering domain for your image and kernel size.

If you do not specify the name-value argument, then uses padding by default, which is different from the default used by .

### Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

supports the generation of C code (requires MATLAB

^{®}Coder™). Note that if you choose the generic target platform, generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Types of Code Generation Support in Image Processing Toolbox.does not support the name-value argument specified as . Filtering is always done in the spatial domain in generated code.

When generating code, all string and character vector input arguments must be compile-time constants.

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

does not support the name-value argument specified as . Filtering is always done in the spatial domain in generated code.

When generating code, all string and character vector input arguments must be compile-time constants.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Image Processing on a GPU.

**Introduced in R2015a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

## imboxfilt

Main Content

### Description

example

filters image with a 2-D, 3-by-3 box filter. A box filter is also called a mean filter.

example

filters image with a 2-D box filter with size specified by .

example

uses name-value pair arguments to control aspects of the filtering.

### Examples

collapse all

### Compute Mean Filter Over Specified Neighborhood

Read image into the workspace.

Perform the mean filtering using an 11-by-11 filter.

Display the original image and the filtered image, side-by-side.

### Compute Local Area Sums Over Specified Neighborhood

Read image into the workspace.

Change the data type of the image to to avoid integer overflow.

Filter image, calculating local area sums, using a 15-by-15 box filter. To calculate local area sums, rather than the mean, set the parameter to 1.

Display the original image and the filtered image, side-by-side.

### Input Arguments

collapse all

### — Image to be filtered

numeric array

Image to be filtered, specified as a numeric array of any dimension. If the input image has more than two dimensions (), such as for an RGB image, then performs box filtering of all 2-D planes along the higher dimensions.

If contains or , then the behavior of is undefined. This can happen when integral image based filtering is used. To restrict the propagation of and in the output, consider using instead.

**Data Types: ** | | | | | | |

### — Size of box filter

(default) | positive, odd integer | 2-element vector of positive, odd integers

Size of box filter, specified as a positive odd integer or 2-element vector of positive, odd integers. If is scalar, then the box filter is square.

**Data Types: ** | | | | | | | | |

### Name-Value Arguments

Specify optional comma-separated pairs of arguments. is the argument name and is the corresponding value. must appear inside quotes. You can specify several name and value pair arguments in any order as .

**Example:**

### — Padding pattern

(default) | | | numeric scalar

Padding pattern, specified as one of the following values or a numeric scalar. If you specify a scalar value, input image pixels outside the bounds of the image are implicitly assumed to have the scalar value.

Value | Description |
---|---|

Input image values outside the bounds of the image are computed by implicitly assuming the input image is periodic. | |

Input image values outside the bounds of the image are assumed equal to the nearest image border value. | |

Input image values outside the bounds of the image are computed by mirror-reflecting the array across the array border. |

**Data Types: ** | | | | | | | | | | |

### — Normalization factor applied to box filter

, if scalar, and , if vector (default) | numeric scalar

Normalization factor applied to box filter, specified as a numeric scalar.

The default has the effect of a mean filter — the pixels in the output image are the local means of the image over the neighborhood determined by . To get local area sums, set to . To avoid overflow in such circumstances, consider using double precision images by converting the input image to class .

**Data Types: ** | | | | | | | | |

### Output Arguments

collapse all

### — Filtered image

numeric array

Filtered image, returned as a numeric array of the same size as the input image .

### Algorithms

performs filtering using either convolution-based filtering or integral image filtering, using an internal heuristic to determine which filtering approach to use.

### Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

supports the generation of C code (requires MATLAB

^{®}Coder™). Note that if you choose the generic target platform, generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Types of Code Generation Support in Image Processing Toolbox.When generating code, all character vector input arguments must be compile-time constants.

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

When generating code, all character vector input arguments must be compile-time constants.

**Introduced in R2015b**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

You can also select a web site from the following list:

### How to Get Best Site Performance

Contact your local office

## 2d matlab filter

## modefilt

Main Content

### Description

example

performs mode filtering on the 2-D image or 3-D volume . Each output pixel in contains the mode (most frequently occurring value) in the neighborhood around the corresponding pixel in . If is 2-D, uses a 3-by-3 mode filter. If is 3-D, uses a 3-by-3-by-3 mode filter. pads by mirroring border elements.

Mode filtering can be useful for processing categorical data, where other types of filtering, such as median filtering, are not available.

also specifies the size of the filter neighborhood. is a vector of positive, odd integers. When is 2-D, specify as a 1-by-2 vector. When is 3-D, specify as a 1-by-3 vector.

also specifies how pads array boundaries.

### Examples

collapse all

### Apply Mode Filter to Categorical Labeled Image

Load an image () and the corresponding categorical labeled version of the image () into the workspace.

View the original image, .

View the categorical labeled image, . The categorical image labels four separate categories: sky, grass, building, and sidewalk. For viewing, convert these categories to colors using the function.

Perform mode filtering on the categorical labeled image, , using the default filter size and padding method.

View the filtered categorical labeled image, . In the filtered image, the edges between labeled regions are more distinct.

### Use Mode Filter on Labeled Volume

Read a labeled volume of an MRI. The volume is stored in the workspace variable .

Display the labeled volume. For clarity, add a title to the display.

Perform mode filtering on the labeled volume, specifying the size of the filter.

Display the filtered labeled volume. For clarity, add a title to the display.

### Input Arguments

collapse all

### — 2-D image or 3-D volume

2-D or 3-D categorical, logical, or numeric array

2-D image or 3-D volume, specified as a categorical, logical, or numeric array.

**Data Types: ** | | | | | | | | |

### — Filter size

vector of positive odd integers.

Filter size, specified as a vector of positive odd integers. For 2-D images, specify a vector of the form . The default for 2-D images is . For 3-D volumes, specify a vector of the form . The default for 3-D volumes is .

**Data Types: ** | | | | | | | | |

### — Padding method

(default) | |

Padding method, specified as one of the following values.

Value | Description |
---|---|

Pad array with a mirror reflection of itself. | |

Pad array by repeating border elements. | |

Pad array with s for numeric data or with s for categorical data. |

**Example: **

**Data Types: ** |

### Output Arguments

collapse all

### — Filtered image or volume

numeric array

Filtered image or volume, returned as a numeric array of the same size and class as the input image .

### Tips

When the neighborhood has more than one pixel in a tie for the mode value, the function uses the following tie-breaking algorithm:

If the center pixel is one of the mode values in the tie, the function uses this value.

If the center pixel is not one of the mode values in the tie, the function uses the mode with the smallest numeric value.

For categorical input, the function chooses the first category (among the categories tied for mode) that appears in the list returned by .

treats RGB images as 3-D volumes. To do channel-wise filtering of an RGB image, specify as , as in this code: .

### Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

supports the generation of C code (requires MATLAB

^{®}Coder™). For more information, see Code Generation for Image Processing.Only 1-D and 2-D inputs are supported.

Input images of data type are not supported.

The argument must be a compile-time constant.

**Introduced in R2020a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

You can also select a web site from the following list:

### How to Get Best Site Performance

Contact your local office

## wiener2

Main Content

**The syntax has been removed. Use the syntax instead.**

### Description

filters the grayscale image using a pixel-wise adaptive low-pass Wiener filter. specifies the size (-by-) of the neighborhood used to estimate the local image mean and standard deviation. The additive noise (Gaussian white noise) power is assumed to be .

The input image has been degraded by constant power additive noise. uses a pixelwise adaptive Wiener method based on statistics estimated from a local neighborhood of each pixel.

example

returns the estimates of the additive noise power calculates before doing the filtering.

### Examples

collapse all

### Remove Noise By Adaptive Filtering

This example shows how to use the function to apply a Wiener filter (a type of linear filter) to an image adaptively. The Wiener filter tailors itself to the local image variance. Where the variance is large, performs little smoothing. Where the variance is small, performs more smoothing.

This approach often produces better results than linear filtering. The adaptive filter is more selective than a comparable linear filter, preserving edges and other high-frequency parts of an image. In addition, there are no design tasks; the function handles all preliminary computations and implements the filter for an input image. , however, does require more computation time than linear filtering.

works best when the noise is constant-power ("white") additive noise, such as Gaussian noise. The example below applies to an image of Saturn with added Gaussian noise.

Read the image into the workspace.

Convert the image from truecolor to grayscale.

Add Gaussian noise to the image

Display the noisy image. Because the image is quite large, display only a portion of the image.

Remove the noise using the function.

Display the processed image. Because the image is quite large, display only a portion of the image.

### Input Arguments

collapse all

### — Input image

2-D numeric array

Input image, specified as a 2-D numeric array.

**Data Types: ** | | | |

### — Neighborhood size

(default) | 2-element numeric vector of the form

Neighborhood size, specified as a 2-element vector where is the number of rows and is the number of columns.

**Data Types: ** | | | | | | | | | |

### — Additive noise

(default) | numeric array

Additive noise, specified as a numeric array. If you do not specify noise, then calculates the mean of the local variance, .

**Data Types: ** |

### Output Arguments

collapse all

### — Filtered image

numeric array

Filtered image, returned as a numeric array the same size and class as the input image .

### — Estimate of additive noise power

numeric array

Estimate of additive noise power, returned as a numeric array.

### Algorithms

estimates the local mean and variance around each pixel.

and

where is the *N*-by-*M* local neighborhood of each pixel in the image . then creates a pixelwise Wiener filter using these estimates,

where ν^{2} is the noise variance. If the noise variance is not given, uses the average of all the local estimated variances.

### References

[1] Lim, Jae S. *Two-Dimensional Signal and Image Processing*, Englewood Cliffs, NJ, Prentice Hall, 1990, p. 548, equations 9.44, 9.45, and 9.46.

### Extended Capabilities

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Image Processing on a GPU.

**Introduced before R2006a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

You can also select a web site from the following list:

### How to Get Best Site Performance

Contact your local office

### Similar news:

- Better than us actors
- Diy tumbler cups
- 2005 bmw convertible
- Asus com service
- Big man one piece
- Wig long hair
- Electric stove with remote
- Samsung earbud review
- Fl studio basic tutorials

## filter2

Main Content

### Description

example

applies a finite impulse response filter to a matrix of data according to coefficients in a matrix .

example

returns a subsection of the filtered data according to . For example, returns only filtered data computed without zero-padded edges.

### Examples

collapse all

### 2-D Pedestal

You can digitally filter images and other 2-D data using the function, which is closely related to the function.

Create and plot a 2-D pedestal with interior height equal to one.

Filter the data in according to a filter coefficient matrix , and return the full matrix of filtered data.

Rotate 180 degrees and convolve the result with . The output is equivalent to filtering the data in with the coefficients in .

### Input Arguments

collapse all

### — Coefficients of rational transfer function

matrix

Coefficients of the rational transfer function, specified as a matrix.

**Data Types: ** | | | | | | | | | | **Complex Number Support: **Yes

### — Input data

matrix

Input data, specified as a matrix. If one or both of and are of type , then the output is also of type . Otherwise, returns type .

**Data Types: ** | | | | | | | | | | **Complex Number Support: **Yes

### — Subsection of filtered data

(default) | |

Subsection of the filtered data, specified as one of these values:

— Return the central part of the filtered data, which is the same size as .

— Return the full 2-D filtered data.

— Return only parts of the filtered data that are computed without zero-padded edges.

### Algorithms

The function filters data by taking the 2-D convolution of the input and the coefficient matrix rotated 180 degrees. Specifically, is equivalent to .

### Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

### Thread-Based Environment

Run code in the background using MATLAB® or accelerate code with Parallel Computing Toolbox™ .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

The inputs and must be single or double arrays. Both real and complex types are supported.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

**Introduced before R2006a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

You can also select a web site from the following list:

### How to Get Best Site Performance

Contact your local office

**20654**20655 20656