This transform extracts finer details from the images. This is the difference between the input image and its morphological opening. This gives us the objects in the image that are smaller than the structuring element and brighter than the surroundings. Depending on the size of the structuring element, we can extract various objects in the given image:
If you look at the output image carefully, you can see those black rectangles. It means that the structuring element was able to fit in there, and so those regions are blackened out. Here is the function:
Mat performTopHat(Mat inputImage, int morphologyElement, int morphologySize) ...