Slimming/Thickening The Shapes

Erosion

  1. Mat performErosion(Mat inputImage, int erosionElement, int erosionSize)
  2. {
  3.     Mat outputImage;
  4.     int erosionType;
  5.  
  6.     if(erosionElement == 0)
  7.         erosionType = MORPH_RECT;
  8.  
  9.     else if(erosionElement == 1)
  10.         erosionType = MORPH_CROSS;
  11.  
  12.     else if(erosionElement == 2)
  13.         erosionType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(erosionType, Size(2*erosionSize + 1, 2*erosionSize + 1), Point(erosionSize, erosionSize));
  17.  
  18.     // Erode the image using the structuring element
  19.     erode(inputImage, outputImage, element);
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }

Dilation

  1. Mat performDilation(Mat inputImage, int dilationElement, int dilationSize)
  2. {
  3.     Mat outputImage;
  4.     int dilationType;
  5.  
  6.     if(dilationElement == 0)
  7.         dilationType = MORPH_RECT;
  8.  
  9.     else if(dilationElement == 1)
  10.         dilationType = MORPH_CROSS;
  11.  
  12.     else if(dilationElement == 2)
  13.         dilationType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for dilation
  16.     Mat element = getStructuringElement(dilationType, Size(2*dilationSize + 1, 2*dilationSize + 1), Point(dilationSize, dilationSize));
  17.  
  18.     // Dilate the image using the structuring element
  19.     dilate(inputImage, outputImage, element);
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }

Leave a Reply

Your email address will not be published. Required fields are marked *