Main Content


Top-hat filtering



J = imtophat(I,SE) performs morphological top-hat filtering on the grayscale or binary image I using the structuring element SE. Top-hat filtering computes the morphological opening of the image (using imopen) and then subtracts the result from the original image.

J = imtophat(I,nhood) top-hat filters the image I, where nhood is a matrix of 0s and 1s that specifies the structuring element neighborhood.

This syntax is equivalent to imtophat(I,strel(nhood)).


collapse all

This example shows how to use top-hat filtering with a disk-shaped structuring element to remove uneven background illumination from an image with a dark background.

Read an image and display it.

original = imread('rice.png');

Create the structuring element.

se = strel('disk',12);

Perform the top-hat filtering and display the image.

tophatFiltered = imtophat(original,se);

Use imadjust to improve the visibility of the result.

contrastAdjusted = imadjust(tophatFiltered);

Input Arguments

collapse all

Input image, specified as a grayscale image or binary image of any dimension.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical

Structuring element, specified as a single strel object or offsetstrel object. If the image I is data type logical, the structuring element must be flat.

Structuring element neighborhood, specified as a matrix of 0s and 1s.

Example: [0 1 0; 1 1 1; 0 1 0]

Output Arguments

collapse all

Top-hat filtered image, returned as a grayscale image or binary image. J has the same data type as input image I.


  • If the dimensionality of the image I is greater than the dimensionality of the structuring element, then the imtophat function applies the same morphological opening to all planes along the higher dimensions.

    You can use this behavior to perform top-hat filtering on RGB images. Specify a 2-D structuring element for RGB images to operate on each color channel separately.

  • When you specify a structuring element neighborhood, imtophat determines the center element of nhood by floor((size(nhood)+1)/2).

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also