Top Hat Filter

Julian Sterling
-
top hat filter

Top-hat filter Mathematical Foundation Definition in Frequency Domain The top-hat transform is a nonlinear morphological operation and therefore lacks a classical linear transfer function $ H(\omega) $ in the frequency domain, unlike linear filters such as the ideal rectangular or Gaussian filters. Instead, its effects can be analyzed in terms of scale rather than frequency, where it emphasizes small-scale features analogous to a high-pass filter.

The white top-hat suppresses large-scale (low-frequency) background structures by subtracting the morphological opening, enhancing bright small objects, while the black top-hat highlights dark small features against brighter backgrounds by subtracting the image from its closing.

This behavior is studied through granulometry in mathematical morphology, which quantifies the distribution of feature sizes in the image, providing a morphological counterpart to frequency-domain analysis.[3][4]Definition in Spatial Domain The top-hat transform is defined directly in the spatial domain using basic morphological operations: erosion and dilation with a structuring element $ B $, a predefined shape (e.g., disk or square) that determines the scale of features detected.

For a grayscale image $ f: \mathbb{R}^2 \to \mathbb{R} $ and a flat structuring element $ B $ (where $ b(y) = 0 $ for $ y \in B $), the dilation $ \delta_B(f) $ at point $ x $ is and the erosion $ \varepsilon_B(f) $ is The morphological opening $ \gamma_B(f) $ is the dilation of the erosion, which smooths the image by removing small bright features while preserving larger ones.

The morphological closing $ \phi_B(f) $ is the erosion of the dilation, which fills small dark holes and smooths contours. The white top-hat transform, which extracts small bright objects, is defined as the difference between the original image and its opening: The black top-hat transform, which extracts small dark objects, is the difference between the closing and the original image: For binary images (where $ f $ takes values in {0,1}), the operations use union for dilation and intersection for erosion, but the top-hat definitions remain analogous.

These operations are translation-invariant and increasing, preserving the image's topological properties while isolating anomalies based on the size of $ B $. The choice of $ B $'s size tunes the scale: smaller elements detect finer details, larger ones broader features.

In discrete implementations, such as in OpenCV or MATLAB, the structuring element is a matrix, and sup/inf are replaced by max/min over neighborhoods.[5][6][7] Properties and Characteristics The top-hat transform is a non-linear operation within mathematical morphology and does not possess properties like transfer functions or impulse responses typical of linear filters.

Instead, its characteristics stem from the underlying erosion and dilation operations and the chosen structuring element, which defines the scale and shape of features it can detect.[7]White Top-Hat The white top-hat transform, defined as the difference between the original image and its morphological opening (where is the structuring element), extracts small bright features that are smaller than the structuring element and brighter than their surrounding background. The output is always non-negative, as the opening smooths the image by removing peaks without introducing new brighter areas.

In uniform regions or areas where the structuring element fits perfectly, the white top-hat yields zero values, effectively suppressing large-scale structures while preserving edges of small objects. The operation is translation invariant when using a symmetric structuring element, meaning shifting the input image shifts the output identically.[5]Black Top-Hat The black top-hat transform, defined as the difference between the morphological closing and the original image , highlights small dark features smaller than the structuring element and darker than their surroundings.

Similar to the white top-hat, the output is non-negative, with zero values in regions unaffected by small-scale depressions. It complements the white top-hat by targeting valleys rather than peaks, and shares properties like translation invariance under symmetric structuring elements.

Both variants are extensive operations, meaning they are monotonically increasing with respect to the input image intensity.[8]Implementation Approaches Digital Realization The digital realization of the top-hat filter in mathematical morphology involves computing the difference between the original grayscale image and the result of a morphological opening (for the white top-hat) or between the morphological closing and the original image (for the black top-hat).

The opening is performed by first applying erosion followed by dilation using a structuring element (SE), which is a small matrix defining the shape and size of the neighborhood probed around each pixel. Erosion replaces each pixel value with the minimum value in its neighborhood defined by the SE, while dilation replaces it with the maximum.

The SE can be flat (binary, like a disk or square) or grayscale, with common sizes ranging from 3x3 to 21x21 pixels depending on the scale of features to extract.[7] Naive implementations of erosion and dilation require scanning the SE over each pixel, leading to O(N * K^2) complexity, where N is the number of pixels and K is the SE diameter.

For efficiency, especially with flat SEs, optimized algorithms like the van Herk/Gil-Werman method use sliding window minima/maxima, reducing complexity to O(N) by decomposing the operation into directional passes (horizontal and vertical). This is particularly useful for large images in real-time applications.

Libraries such as OpenCV, scikit-image, and MATLAB provide built-in functions for these operations; for example, in OpenCV, the white top-hat is computed usingcv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel) , where kernel is the SE (e.g., cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)) ).[7][8] For adaptive or multi-scale top-hat filters, the SE size can be varied iteratively, or self-dual variants can be used to combine white and black top-hats for edge-preserving enhancement.

Pseudocode for a basic white top-hat using naive erosion and dilation (for illustration, not efficient) is as follows: function white_tophat([image](/page/Image), se) opened = opening([image](/page/Image), se) // [erosion](/page/Erosion) followed by dilation return [image](/page/Image) - opened end function [erosion](/page/Erosion)([image](/page/Image), se) output = zeros(size([image](/page/Image))) for each [pixel](/page/Pixel) (i,j) in [image](/page/Image) neighborhood = [image](/page/Image)[i+se_rows, j+se_cols] // shifted by SE output(i,j) = min(neighborhood) end return output end function dilation(image, se) output = zeros(size(image)) for each pixel (i,j) in image neighborhood = image[i+se_rows, j+se_cols] output(i,j) = max(neighborhood) end return output end function opening(image, se) return dilation(erosion(image, se), se) end This approach preserves the non-linear nature of the operation, effectively highlighting small bright or dark features without linear approximations like convolution.[9] Analog Realization Morphological operations, including the top-hat transform, are fundamentally defined in the continuous domain of mathematical morphology for functions over Euclidean space, where erosion and dilation are infimum and supremum operations over translated structuring functions.

However, practical analog realizations are uncommon due to the non-linear, min/max nature of the operations, which are challenging to implement in continuous-time hardware without discretization. Theoretical continuous formulations allow analysis of top-hat on analog signals or images, but implementation typically requires sampling into digital form. In specialized hardware, approximations can be achieved using optical processors or cellular neural networks (CNNs), where local min/max circuits mimic erosion/dilation on analog arrays.

For instance, CNN chips can perform real-time morphological filtering by configuring feedback templates equivalent to SEs, enabling parallel processing at video rates. Such implementations, developed since the 1990s, offer advantages in speed for embedded vision systems but are limited by chip size and noise sensitivity compared to digital methods. Passive optical setups using shadow-casting or interference patterns have also been proposed for binary morphology, though grayscale top-hat remains predominantly digital.

Overall, digital realizations dominate due to flexibility and integration with modern computing pipelines.[10]Applications Signal Processing Uses In signal processing, the top-hat filter acts as an ideal band-pass filter with a flat response in the frequency domain. It passes a specified band of frequencies while attenuating others, useful for isolating signals in communication systems. The filter's rectangular transfer function results in a sinc-like impulse response in the time domain, which can introduce ringing artifacts due to the Gibbs phenomenon near transients.

These effects are often mitigated by windowing or using approximations with smoother transitions.Image Processing Uses In image processing, the 2D top-hat filter functions as a band-pass filter, with a rectangular region in the frequency domain passing mid-range spatial frequencies and attenuating low and high frequencies. The impulse response is a 2D sinc function. For an ideal band-pass, the kernel can be implemented efficiently using the fast Fourier transform (FFT), such as via fft2, multiplication with the mask, and ifft2 in MATLAB, or cv::dft in OpenCV.

A key application is in astronomical imaging, such as cosmic microwave background (CMB) analysis, where a top-hat filter in Fourier space extracts point sources by isolating mid-frequency signals from large-scale gradients and noise.

For example, in processing data from telescopes like Planck, it improves detection of compact sources without requiring prior beam profile knowledge.[11] It is important to distinguish the linear top-hat filter, which operates via convolution in the spatial domain or multiplication in the frequency domain, from the nonlinear morphological top-hat transform, defined as the residue after erosion and dilation operations to detect small bright objects on darker backgrounds.

Unlike the morphological version, the linear filter depends on frequency cutoffs rather than structuring elements, avoiding shape biases in non-isotropic images.Morphological Applications The morphological top-hat transform is used in image processing to extract small-scale bright (white top-hat) or dark (black top-hat) features.

In microscopy, it isolates small rounded objects like cells or particles based on size and intensity.[12] It aids in infrared imaging for detecting spot targets, such as vehicles or hotspots, by separating them from cluttered backgrounds.[13] Additional uses include medical imaging for enhancing tissue contrasts, astronomical star detection, and industrial defect inspection, adapting to various resolutions and noise levels.[5][14]Related Concepts Similar Filters The moving average filter serves as a discrete low-pass approximation to the top-hat filter, providing finite support through uniform weighting of samples within a window, but it exhibits a gradual roll-off without a sharp cutoff, resulting in poorer frequency selectivity compared to the ideal rectangular response of the top-hat.[15] This finite impulse response avoids infinite tails but introduces minimal ringing, making it suitable for noise reduction in applications requiring preserved step responses, though at the cost of broader transition bands.[15] In contrast to the top-hat's sinc kernel, which extends infinitely and causes oscillatory artifacts, the moving average prioritizes computational simplicity and temporal locality.[16] The Gaussian filter offers a smooth roll-off in the frequency domain, eliminating ringing due to its non-oscillatory impulse response with no negative lobes, but this comes at the expense of a broader transition band that reduces sharpness relative to the top-hat's abrupt cutoff.[16] Unlike the top-hat, which achieves perfect passband flatness but induces Gibbs phenomenon artifacts from its discontinuous frequency response, the Gaussian provides gradual smoothing ideal for applications sensitive to overshoot, such as biological signal analysis.[16] Butterworth and Chebyshev filters represent practical analog approximations to the ideal top-hat response, using pole placements to achieve steeper roll-offs than simple RC circuits while remaining realizable with finite-order designs.[17] The Butterworth filter maintains a maximally flat passband without ripple, offering a balanced trade-off but with slower attenuation beyond the cutoff compared to the top-hat's infinite sharpness; higher orders improve steepness but extend the impulse response and introduce ringing.[17][16] Chebyshev filters provide even steeper transitions by allowing controlled ripple in the passband, enhancing selectivity over Butterworth at the cost of passband variations, though both suffer less severe artifacts than the top-hat's ideal perfection, which demands infinite order for true realizability and often leads to temporal smearing in practice.[17][16]Mathematical Connections The top-hat filter, defined by its rectangular frequency response $ H(\omega) $, embodies a core duality in Fourier analysis: the inverse Fourier transform of this rectangular function yields the sinc function as the impulse response $ h(t) $.

Specifically, where $ \omega_c $ is the cutoff frequency and $ \mathrm{sinc}(x) = \sin(\pi x)/(\pi x) $.[18] This pairing highlights the rectangular function as a fundamental building block for deriving transforms in signal processing, with the converse transform of the sinc yielding the rectangular response due to Fourier duality properties.[19] In the limit as the bandwidth $ \omega_c $ approaches infinity, the top-hat filter's impulse response narrows while maintaining unit area, approximating the Dirac delta distribution $ \delta(t) $.

This representation arises from considering the rectangular pulse of height $ 1/\epsilon $ and width $ \epsilon $ as $ \epsilon \to 0^+ $, providing a rigorous foundation for generalized functions in filter theory.

The top-hat filter connects directly to sampling theory through its role as the ideal low-pass filter, with a sharp cutoff at the Nyquist frequency $ f_N = f_s/2 $ (where $ f_s $ is the sampling rate), ensuring bandlimitation to prevent aliasing in the Nyquist-Shannon theorem.[20] This rectangular response passes all frequencies below $ f_N $ unattenuated while fully attenuating those above, theoretically enabling perfect reconstruction of bandlimited signals from samples.[21] Generalizations of the top-hat filter extend to higher dimensions, where the passband becomes a hyper-rectangle or spherical region in multidimensional frequency space, facilitating applications in vector-valued signals.[22] Further adaptations include non-uniform bandwidths, such as elliptical or adaptive passbands, to accommodate anisotropic or varying signal characteristics in advanced processing frameworks.[23]

People Also Asked

Top-hatfilter- Wikipedia?

The white top-hat suppresses large-scale (low-frequency) background structures by subtracting the morphological opening, enhancing bright small objects, while the black top-hat highlights dark small features against brighter backgrounds by subtracting the image from its closing.

Top-hat filter?

In discrete implementations, such as in OpenCV or MATLAB, the structuring element is a matrix, and sup/inf are replaced by max/min over neighborhoods.[5][6][7] Properties and Characteristics The top-hat transform is a non-linear operation within mathematical morphology and does not possess properties like transfer functions or impulse responses typical of linear filters.

Removing small objects in grayscale images with atophatfilter...?

For a grayscale image $ f: \mathbb{R}^2 \to \mathbb{R} $ and a flat structuring element $ B $ (where $ b(y) = 0 $ for $ y \in B $), the dilation $ \delta_B(f) $ at point $ x $ is and the erosion $ \varepsilon_B(f) $ is The morphological opening $ \gamma_B(f) $ is the dilation of the erosion, which smooths the image by removing small bright features while preserving larger ones.

tophatfilter?

These operations are translation-invariant and increasing, preserving the image's topological properties while isolating anomalies based on the size of $ B $. The choice of $ B $'s size tunes the scale: smaller elements detect finer details, larger ones broader features.

TopHatand BlackHatTransform using... - GeeksforGeeks?

The opening is performed by first applying erosion followed by dilation using a structuring element (SE), which is a small matrix defining the shape and size of the neighborhood probed around each pixel. Erosion replaces each pixel value with the minimum value in its neighborhood defined by the SE, while dilation replaces it with the maximum.