imtophat

Top-hat filtering

Syntax

IM2 = imtophat(IM,SE)
IM2 = imtophat(IM,NHOOD)
gpuarrayIM2 = imtophat(gpuarrayIM,___)

Description

IM2 = imtophat(IM,SE) performs morphological top-hat filtering on the grayscale or binary input image IM. Top-hat filtering computes the morphological opening of the image (using imopen) and then subtracts the result from the original image. imtophat uses the structuring element SE, where SE is returned by strel. SE must be a single structuring element object, not an array containing multiple structuring element objects.

This function supports code generation (see Tips).

IM2 = imtophat(IM,NHOOD) where NHOOD is an array of 0s and 1s that specifies the size and shape of the structuring element, is the same as imptophat(IM,strel(NHOOD)).

gpuarrayIM2 = imtophat(gpuarrayIM,___) performs the operation on a GPU. NHOOD is the structuring element specified by strel(NHOOD), if NHOOD is an array of 0s and 1s that specifies the structuring element neighborhood. If NHOOD is a gpuArray, strel(gather(NHOOD)) specifies the structuring element neighborhood.

Class Support

IM can be numeric or logical and must be nonsparse. The output image IM2 has the same class as the input image. If the input is binary (logical), the structuring element must be flat.

gpuarrayIM must be a gpuArray of type uint8 or logical. When used with a gpuArray, the structuring element must be flat and two-dimensional.

The output has the same class as the input.

Examples

expand all

Use Top-hat Filtering to Correct Uneven Illumination

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.

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

Use imadjust to improve the visibility of the result.

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

Use Top-hat Filtering to Correct Uneven Illumination on the GPU

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)

More About

expand all

Tips

  • This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function 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 Code Generation Using a Shared Library.

    When generating code, the image input argument, IM, must be 2-D or 3-D and the structuring element input argument, SE, must be a compile-time constant.

See Also

| |

Was this topic helpful?