Other Morphological Operators

Opening

  1. Mat performOpening(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage, tempImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply morphological opening to the image using the structuring element
  19.     erode(inputImage, tempImage, element);
  20.     dilate(tempImage, outputImage, element);
  21.  
  22.     // Return the output image
  23.     return outputImage;
  24. }

Closing

  1. Mat performClosing(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage, tempImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply morphological opening to the image using the structuring element
  19.     dilate(inputImage, tempImage, element);
  20.     erode(tempImage, outputImage, element);
  21.  
  22.     // Return the output image
  23.     return outputImage;
  24. }

Morphological Gradient

  1. Mat performMorphologicalGradient(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage, tempImage1, tempImage2;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply morphological gradient to the image using the structuring element
  19.     dilate(inputImage, tempImage1, element);
  20.     erode(inputImage, tempImage2, element);
  21.  
  22.     // Return the output image
  23.     return tempImage1 - tempImage2;
  24. }

TopHat

  1. Mat performTopHat(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply top hat operation to the image using the structuring element
  19.     outputImage = inputImage - performOpening(inputImage, morphologyElement, morphologySize);
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }

BlackHat

  1. Mat performBlackHat(Mat inputImage, int morphologyElement, int morphologySize)
  2. {
  3.     Mat outputImage;
  4.     int morphologyType;
  5.  
  6.     if(morphologyElement == 0)
  7.         morphologyType = MORPH_RECT;
  8.  
  9.     else if(morphologyElement == 1)
  10.         morphologyType = MORPH_CROSS;
  11.  
  12.     else if(morphologyElement == 2)
  13.         morphologyType = MORPH_ELLIPSE;
  14.  
  15.     // Create the structuring element for erosion
  16.     Mat element = getStructuringElement(morphologyType, Size(2*morphologySize + 1, 2*morphologySize + 1), Point(morphologySize, morphologySize));
  17.  
  18.     // Apply black hat operation to the image using the structuring element
  19.     outputImage = performClosing(inputImage, morphologyElement, morphologySize) - inputImage;
  20.  
  21.     // Return the output image
  22.     return outputImage;
  23. }

Leave a Reply

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