Main Content


Top-hat filtering



J = imtophat(I,SE) performs morphological top-hat filtering on the grayscale or binary image I, returning the filtered image, J. Top-hat filtering computes the morphological opening of the image (using imopen) and then subtracts the result from the original image. SE is a single structuring element object returned by the strel or offsetstrel functions.

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. The imtophat function determines the center element of the neighborhood by floor((size(nhood)+1)/2).

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');

Figure contains an axes. The axes contains an object of type image.

Create the structuring element.

se = strel('disk',12);

Perform the top-hat filtering and display the image.

tophatFiltered = imtophat(original,se);

Figure contains an axes. The axes contains an object of type image.

Use imadjust to improve the visibility of the result.

contrastAdjusted = imadjust(tophatFiltered);

Figure contains an axes. The axes contains an object of type image.

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 class as input image I.

Extended Capabilities

See Also



Introduced before R2006a