Slimming the shapes

We achieve this effect using an operation called erosion. This is the operation that makes a shape thinner by peeling the boundary layers of all the shapes in the image:

Let's look at the function that performs morphological erosion:

Mat performErosion(Mat inputImage, int erosionElement, int erosionSize)
{

Mat outputImage;
int erosionType;

if(erosionElement == 0)
erosionType = MORPH_RECT;
else if(erosionElement == 1)
erosionType = MORPH_CROSS;
else if(erosionElement == 2)
erosionType = MORPH_ELLIPSE;

// Create the structuring element for erosion
Mat element = getStructuringElement(erosionType, Size(2*erosionSize + 1, 2*erosionSize + 1), Point(erosionSize, erosionSize));

// Erode the image using the structuring element
erode(inputImage, outputImage, element);

// Return the output image
return outputImage;
}

You can check out the full code in the .cpp files to understand how to use this function. We basically build a structuring element using a built-in OpenCV function. This object is used as a probe to modify each pixel based on certain conditions. These conditions refer to what's happening around that particular pixel in the image. For example, is it surrounded by white pixels? Or is it surround by black pixels? Once we have an answer, we take the appropriate action.