# Top-hat transform

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Top-hat_transform
> Markdown URL: https://mediated.wiki/source/Top-hat_transform.md
> Source: https://en.wikipedia.org/wiki/Top-hat_transform
> Source revision: 1319047607
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{short description|Operation that extracts small elements and details from given images}}
In [mathematical morphology](/source/mathematical_morphology) and [digital image processing](/source/digital_image_processing), a '''top-hat transform''' is an operation that extracts small elements and details from given [image](/source/image)s. There exist two types of top-hat transform: the ''white top-hat transform'' is defined as the difference between the input image and its [opening](/source/opening_(morphology)) by some [structuring element](/source/structuring_element), while the ''black top-hat transform'' is defined dually as the difference between the [closing](/source/closing_(morphology)) and the input image. Top-hat transforms are used for various image processing tasks, such as [feature extraction](/source/feature_extraction), background equalization, [image enhancement](/source/image_enhancement), and others.

==Mathematical definitions==

Let <math>f:E\mapsto \mathbb{R}</math> be a [grayscale](/source/grayscale) image, mapping points from a [Euclidean space](/source/Euclidean_space) or discrete [grid](/source/Lattice_graph) ''E'' (such as <math>\mathbb{R}^2</math> or <math>\mathbb{Z}^2</math>) into the real line. Let <math>b(x)</math> be a structuring element of grayscale.

Then, the white top-hat transform of ''f'' is given by:

:<math>T_w(f)=f-f \circ b</math>,

where <math>\circ</math> denotes the [opening operation](/source/Opening_(morphology)).

The black top-hat transform of ''f'' (sometimes called the ''bottom-hat'' transform<ref>{{Cite web|last=Tcheslavski|first=Gleb V.|url=http://www.ee.lamar.edu/gleb/dip/10-3%20-%20Morphological%20Image%20Processing.pdf|title=Morphological Image Processing: Gray-scale morphology|year=2010|accessdate=4 November 2013}}</ref>
) is given by:

:<math>T_b(f)=f\bullet b-f</math>,

where <math>\bullet</math> is the [closing operation](/source/Closing_(morphology)).

==Properties==

The white top-hat transform returns an image, containing those "objects" or "elements" of an input image that:
* Are "smaller" than the structuring element (i.e., places where the structuring element does not fit in), and 
* are '''brighter''' than their surroundings.

The black top-hat returns an image, containing the "objects" or "elements" that:
* Are "smaller" than the structuring element, and 
* are '''darker''' than their surroundings.

The size, or width, of the elements that are extracted by the top-hat transforms can be controlled by the choice of the structuring element <math>b</math>. The bigger the latter, the larger the elements extracted.

Both top-hat transforms are images that contain only non-negative values at all pixels.

One of its most important uses in [image segmentation](/source/image_segmentation) is to adjust nonuniform lighting conditions on an image and provide a better threshold value for separating objects.

== Example ==
Assume that the user is only interested in small blobs on the image and wants to remove the larger bright objects. In this case, the white top-hat transform can remove larger bright objects and retain small blobs by selecting the size of the [structuring element](/source/structuring_element) that is between removed objects and objects of interest. The radius of six largest bright objects are approximately 50 to 100 pixels whereas the radius of objects of interest are around 2 to 4 pixels. In addition, the objects of interest are circular shapes so we choose a disk shaped structuring element with radius 5. However, selecting different shapes and sizes for the structuring element result in different images depending on whether objects fit in the structuring element or not.

{{Multiple image
| align = center
| direction = 
| width = 500
| image1 = Cosmos original.jpg
| caption1 = Original image (870×1550)
| image2 = Cosmos tophat.jpg
| caption2 = Applied top-hat transform with disk shape SE radius 5
}}

The other example is an image under nonuniform illumination, where the user wants to extract objects separately from background. The common method for image segmentation is to threshold the input image based on intensity value. However, if the image is under nonuniform lighting, it is possible that segmentation errors might present themselves since some objects in darker area have close intensity values as background intensity values and would not be extracted by only utilizing threshold method. In this case, before [Otsu's method](/source/Otsu's_method) is applied to input image, white top-hat transform should be implemented to correct nonuniform lighting condition and make obvious contrast between background and objects. Therefore, the objects can be extracted entirely from background without segmentation errors. The threshold values are 0.5216 and 0.2 and normalized to <math>[0,1]</math> for original image and applied white top-hat transform respectively.

{{Multiple image
| align = center
| direction = 
| width = 300
| image1 = Lighting original.jpg
| caption1 = Nonuniform lighting condition input image
| image2 = Lighting threshold.jpg
| caption2 = Threshold a nonuniform lighting input image
}}{{Multiple image
| align = center
| direction = 
| width = 300
| image1 = Lighting tophat.jpg
| caption1 = Top-hat transform applied to input image
| image2 = Lighting tophat threshold.jpg
| caption2 = Threshold image after top-hat transform applied
}}

==References==
* <cite id=serra82>''Image Analysis and Mathematical Morphology'' by Jean Serra, {{ISBN|0-12-637240-3}} (1982)</cite>
* ''Image Analysis and Mathematical Morphology, Volume 2: Theoretical Advances'' by Jean Serra, {{ISBN|0-12-637241-1}} (1988)
* ''An Introduction to Morphological Image Processing'' by Edward R. Dougherty, {{ISBN|0-8194-0845-X}} (1992)
* ''Hands-on Morphological Image Processing'' by Edward R. Dougherty and R. Lotufo, {{ISBN|0-8194-4720-X}} (2003)

{{reflist}}
* ''Digital Image Processing'' (''Third Edition'') by Rafael C. Gonzalez and Richard E. Woods, {{ISBN|978-93-325-7032-0}}(2008)

Category:Mathematical morphology
Category:Digital geometry

---
Adapted from the Wikipedia article [Top-hat transform](https://en.wikipedia.org/wiki/Top-hat_transform) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Top-hat_transform?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
