O'Reilly logo

Learn OpenCV 4 by Building Projects - Second Edition by Prateek Joshi, Vinicius G. Mendonca, David Millan Escriva

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Morphological closing

This is the operation that closes a shape by filling the gaps, as shown in the following screenshot. This operation is also used for noise removal. It's basically dilation followed by erosion. This operation removes tiny holes in the foreground by changing small objects in the background into the foreground:

Let's quickly look at the function to perform morphological closing:

Mat performClosing(Mat inputImage, int morphologyElement, int morphologySize){    Mat outputImage, tempImage;    int morphologyType;    if(morphologyElement == 0)        morphologyType = MORPH_RECT;    else if(morphologyElement == 1)        morphologyType = MORPH_CROSS;

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required