imtophat

Top-hat filtering

Syntax

J = imtophat(I,SE)
J = imtophat(I,nhood)

Description

example

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.

You optionally can perform the top-hat filtering using a GPU (requires Parallel Computing Toolbox™). For more information, see Image Processing on a GPU.

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

Examples

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');
imshow(original)

Create the structuring element.

se = strel('disk',12);

Perform the top-hat filtering and display the image.

tophatFiltered = imtophat(original,se);
figure
imshow(tophatFiltered)

Use imadjust to improve the visibility of the result.

contrastAdjusted = imadjust(tophatFiltered);
figure
imshow(contrastAdjusted)

You can use top-hat filtering to correct uneven illumination when the background is dark. This example uses top-hat filtering with a disk-shaped structuring element to remove the uneven background illumination from an image.

Read an image and display it.

original = imread('rice.png');
figure, imshow(original)

Create the structuring element.

se = strel('disk',12);

Perform the top-hat filtering and display the image. Note how the example passes the image to the gpuArray function before passing it to the imtophat function.

tophatFiltered = imtophat(gpuArray(original),se);
figure, imshow(tophatFiltered)

Use imadjust to improve the visibility of the result. The gather function is used to retrieve the contents of the gpuArray from the GPU.

contrastAdjusted = imadjust(gather(tophatFiltered));
figure, imshow(contrastAdjusted)

Input Arguments

collapse all

Input image, specified as a grayscale image or binary image. I can have any dimension, and must be real and nonsparse.

To perform the top-hat filtering using a GPU, specify I as a gpuArray that contains an image of type uint8 or logical.

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.

If you perform the top-hat filtering using a GPU, then SE must be flat and two-dimensional.

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.

If the filtering is performed using a GPU, then J is returned as a gpuArray that contains a grayscale or binary image of the same class as I.

Extended Capabilities

See Also

Functions

Objects

Introduced before R2006a