gives the morphological top-hat transform of image with respect to structuring element ker.


gives the top-hat transform with respect to a range-r square.


applies top-hat transform to an array of data.


  • TopHatTransform works with binary, grayscale, and other images.
  • TopHatTransform works with 3D as well as 2D images, and also with data arrays of any rank.
  • The structuring element ker is a matrix containing 0s and 1s.
  • TopHatTransform automatically pads structuring elements to have odd dimensions.
  • TopHatTransform[image,r] is equivalent to TopHatTransform[image,BoxMatrix[r]].
  • If multiple image channels are present, TopHatTransform operates on each of them separately.
  • TopHatTransform preserves regions smaller than the structuring element whose channel values are larger than their surroundings.


open allclose all

Basic Examples  (4)

Extract thin lines from a multichannel image:

Remove disks larger than the structuring element:

Correct uneven illumination to count grains of rice:

Correct uneven illumination and remove the large feature:

Applications  (4)

Extract small, bright regions from a color image:

Subtract from the original image to remove small features:

Locate small bright regions of a specific color channel:

Use the mask as the alpha channel:

Correct uneven background illumination in a grayscale image:

Isolate the lightning in an image:

Interactive Examples  (1)

Use Manipulate to extract successively larger objects:

Wolfram Research (2008), TopHatTransform, Wolfram Language function, (updated 2012).


Wolfram Research (2008), TopHatTransform, Wolfram Language function, (updated 2012).


@misc{reference.wolfram_2020_tophattransform, author="Wolfram Research", title="{TopHatTransform}", year="2012", howpublished="\url{}", note=[Accessed: 18-January-2021 ]}


@online{reference.wolfram_2020_tophattransform, organization={Wolfram Research}, title={TopHatTransform}, year={2012}, url={}, note=[Accessed: 18-January-2021 ]}


Wolfram Language. 2008. "TopHatTransform." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2012.


Wolfram Language. (2008). TopHatTransform. Wolfram Language & System Documentation Center. Retrieved from