Have you ever been interested in getting some small details from an image? The Julia Morphology package also implements tophat and bothat operators focused on retrieving these kinds of details.
The top-hat filter is applied to enhance bright objects of interest on a dark background. It transforms an image by subtracting a result from running the opening function from the original image. It holds those elements of a source image that are smaller than the structuring element and are brighter than their neighbors.
The bottom-hat operation, on the other hand, is used to do the opposite; that is, it is used to enhance dark objects of interest on a bright background. The bottom-hat operator returns an image that is a result of subtracting the original image from a morphologically closed version of the image. The result contains objects or elements that are smaller than the structuring element, and are darker than their surroundings.
Julia implements tophat and bothat functions to achieve the functionality that we will be trying, using the following code to execute both filters on the image with geometrical figures, as seen in the preceding section. Here is our code:
using Images, ImageView, ImageMorphology
# apply tophat and bothat to an image
geom_img = load("sample-images/geometrical-figures-and-noise.jpg");
geom_img_gray = Gray.(geom_img);
geom_img_th = tophat(geom_img_gray)
geom_img_bh = bothat(geom_img_gray)
# create a preview
geom_img_new = zeros(ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}}, size(geom_img_gray) .* (1, 2));
geom_img_new_center = Int(size(geom_img_gray, 2))
geom_img_new[:, 1:geom_img_new_center] = geom_img_th
geom_img_new[:, geom_img_new_center:end - 1] = geom_img_bh
geom_img_new[:, geom_img_new_center] = 1
imshow(Gray.(geom_img_new))
As you can see from the following result, the tophat function suppressed all large objects and kept only small details of the original image: