ImageDev

TopHat

Detects the compact dark or bright areas of a grayscale image.

Access to parameter description

The Top-Hat transformation allows detecting the dark areas of a grayscale image, or the valleys of function $f$, using the closing function.
By changing the size of the closing and the bounds of the threshold, one may choose the width and the depth of the selected valleys.

Similarly, this algorithm allows detecting the white areas of a grayscale image, or the narrow peaks of function $f$, using an opening.
By changing the values of the size of the opening and the bounds of the threshold, it is possible to choose the width and the height of the selected peaks.

It would be impossible to obtain the same result with a simple threshold.

The Top-Hat segmentation extracts small elements and details from given images. It detects the dark or the bright areas, corresponding to the valleys or the narrow peaks.
There are two types of Top-Hat transforms:

<b> Figure 1.</b> Black Top-Hat computation
Figure 1. Black Top-Hat computation

The Top-Hat algorithm is divided into two phases: See also

Function Syntax

This function returns outputBinaryImage.
// Function prototype
std::shared_ptr< iolink::ImageView > topHat( std::shared_ptr< iolink::ImageView > inputGrayImage, int32_t kernelRadius, TopHat::ObjectLightness objectLightness, iolink::Vector2d thresholdRange, std::shared_ptr< iolink::ImageView > outputBinaryImage = NULL );
This function returns outputBinaryImage.
// Function prototype.
top_hat( input_gray_image,
         kernel_radius = 3,
         object_lightness = TopHat.ObjectLightness.BRIGHT_OBJECTS,
         threshold_range = [128, 255],
         output_binary_image = None )
This function returns outputBinaryImage.
// Function prototype.
public static IOLink.ImageView
TopHat( IOLink.ImageView inputGrayImage,
        Int32 kernelRadius = 3,
        TopHat.ObjectLightness objectLightness = ImageDev.TopHat.ObjectLightness.BRIGHT_OBJECTS,
        double[] thresholdRange = null,
        IOLink.ImageView outputBinaryImage = null );

Class Syntax

Parameters

Parameter Name Description Type Supported Values Default Value
input
inputGrayImage
The input gray level image. Image Binary, Label or Grayscale nullptr
input
kernelRadius
The half size of the structuring element in pixels. A square structuring element always has an odd side length (3x3, 5x5, etc.) which is defined by twice the kernel radius + 1. Int32 >=1 3
input
objectLightness
The lightness of objects to detect.
BRIGHT_OBJECTS A white Top-Hat is applied to detect small bright areas.
DARK_OBJECTS A black Top-Hat is applied to detect small dark areas.
Enumeration BRIGHT_OBJECTS
input
thresholdRange
The low and high threshold levels. Vector2d Any value {128.f, 255.f}
output
outputBinaryImage
The output binary image. Its dimensions are forced to the same values as the input. Image nullptr
Parameter Name Description Type Supported Values Default Value
input
input_gray_image
The input gray level image. image Binary, Label or Grayscale None
input
kernel_radius
The half size of the structuring element in pixels. A square structuring element always has an odd side length (3x3, 5x5, etc.) which is defined by twice the kernel radius + 1. int32 >=1 3
input
object_lightness
The lightness of objects to detect.
BRIGHT_OBJECTS A white Top-Hat is applied to detect small bright areas.
DARK_OBJECTS A black Top-Hat is applied to detect small dark areas.
enumeration BRIGHT_OBJECTS
input
threshold_range
The low and high threshold levels. vector2d Any value [128, 255]
output
output_binary_image
The output binary image. Its dimensions are forced to the same values as the input. image None
Parameter Name Description Type Supported Values Default Value
input
inputGrayImage
The input gray level image. Image Binary, Label or Grayscale null
input
kernelRadius
The half size of the structuring element in pixels. A square structuring element always has an odd side length (3x3, 5x5, etc.) which is defined by twice the kernel radius + 1. Int32 >=1 3
input
objectLightness
The lightness of objects to detect.
BRIGHT_OBJECTS A white Top-Hat is applied to detect small bright areas.
DARK_OBJECTS A black Top-Hat is applied to detect small dark areas.
Enumeration BRIGHT_OBJECTS
input
thresholdRange
The low and high threshold levels. Vector2d Any value {128f, 255f}
output
outputBinaryImage
The output binary image. Its dimensions are forced to the same values as the input. Image null

Object Examples

auto foam = readVipImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "foam.vip" );

TopHat topHatAlgo;
topHatAlgo.setInputGrayImage( foam );
topHatAlgo.setKernelRadius( 3 );
topHatAlgo.setObjectLightness( TopHat::ObjectLightness::BRIGHT_OBJECTS );
topHatAlgo.setThresholdRange( {128.0, 255.0} );
topHatAlgo.execute();

std::cout << "outputBinaryImage:" << topHatAlgo.outputBinaryImage()->toString();
foam = imagedev.read_vip_image(imagedev_data.get_image_path("foam.vip"))

top_hat_algo = imagedev.TopHat()
top_hat_algo.input_gray_image = foam
top_hat_algo.kernel_radius = 3
top_hat_algo.object_lightness = imagedev.TopHat.BRIGHT_OBJECTS
top_hat_algo.threshold_range = [128.0, 255.0]
top_hat_algo.execute()

print( "output_binary_image:", str( top_hat_algo.output_binary_image ) )
ImageView foam = Data.ReadVipImage( @"Data/images/foam.vip" );

TopHat topHatAlgo = new TopHat
{
    inputGrayImage = foam,
    kernelRadius = 3,
    objectLightness = TopHat.ObjectLightness.BRIGHT_OBJECTS,
    thresholdRange = new double[]{128.0, 255.0}
};
topHatAlgo.Execute();

Console.WriteLine( "outputBinaryImage:" + topHatAlgo.outputBinaryImage.ToString() );

Function Examples

auto foam = readVipImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "foam.vip" );

auto result = topHat( foam, 3, TopHat::ObjectLightness::BRIGHT_OBJECTS, {128.0, 255.0} );

std::cout << "outputBinaryImage:" << result->toString();
foam = imagedev.read_vip_image(imagedev_data.get_image_path("foam.vip"))

result = imagedev.top_hat( foam, 3, imagedev.TopHat.BRIGHT_OBJECTS, [128.0, 255.0] )

print( "output_binary_image:", str( result ) )
ImageView foam = Data.ReadVipImage( @"Data/images/foam.vip" );

IOLink.ImageView result = Processing.TopHat( foam, 3, TopHat.ObjectLightness.BRIGHT_OBJECTS, new double[]{128.0, 255.0} );

Console.WriteLine( "outputBinaryImage:" + result.ToString() );