Applying morphological operators on gray-level images
More advanced morphological operators can be composited by combining the different basic morphological filters introduced in this chapter. This recipe will present two morphological operators that, when applied to gray-level images, can lead to the detection of interesting image features.
How to do it...
One interesting morphological operator is the morphological gradient that allows extracting the edges of an image. This one can be accessed through the cv::morphologyEx
function as follows:
// Get the gradient image using a 3x3 structuring element cv::Mat result; cv::morphologyEx(image, result, cv::MORPH_GRADIENT, cv::Mat());
The following result shows the extracted contours of the image's elements (the resulting image has been inverted for better viewing):
Another useful morphological operator is the top-hat transform. This operator can be used to extract local small foreground objects...