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 ...

Get OpenCV 3 Computer Vision Application Programming Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.