Morphology Transformations

void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue = morphologyDefaultBorderValue() )

 Parameters:
  • src – Source image. The number of channels can be arbitrary. The depth should be one of CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
  • dst – Destination image of the same size and type as src .
  • op – Type of a morphological operation that can be one of the following:
    • MORPH_OPEN (2) - an opening operation
    • MORPH_CLOSE (3) - a closing operation
    • MORPH_GRADIENT (4) - a morphological gradient
    • MORPH_TOPHAT (5) - “top hat”
    • MORPH_BLACKHAT (6) - “black hat”
  • kernel – Structuring element.
  • iterations – Number of times erosion and dilation are applied.
  • borderType – Pixel extrapolation method. See borderInterpolate() for details.
  • borderValue – Border value in case of a constant border. The default value has a special meaning. See createMorphologyFilter() for details.
Brief description and an implementation is available in OpenCV documentation.

Steps:

  1. Load image and convert to gray-scale.
  2. Create a structuring element (getStructuringElement)
  3.  Apply morphological operation (morphologyEx)
  4. Show the result

Functions:


Example: 

------------

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat src = imread("lena.jpg", 0);

    // Create a structuring element (SE)
    int morph_size = 2;
    Mat element = getStructuringElement( MORPH_RECT, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
    cout<<element;

    Mat dst; // result matrix
    // Apply the specified morphology operation
    for (int i=1;i<10;i++)
    {   
    morphologyEx( src, dst, MORPH_TOPHAT, element, Point(-1,-1), i );   
    //morphologyEx( src, dst, MORPH_TOPHAT, element ); // here iteration=1
    imshow("source", src);
    imshow("result", dst);
    waitKey(1000);
    }   
    return 0;
}

------------




No comments:

Post a Comment