Create structuring element for morphological operations

Mat getStructuringElement(int shape, Size ksize, Point anchor=Point(-1,-1))

Parameters:
  • shape – Element shape that could be one of the following:
    • MORPH_RECT (0) - a rectangular structuring element:
      E_{ij}=1
    • MORPH_ELLIPSE (2) - an elliptic structuring element, that is, a filled ellipse inscribed into the rectangle Rect(0, 0, esize.width, esize.height)
    • MORPH_CROSS (1)- a cross-shaped structuring element:
      E_{ij} =  \fork{1}{if i=\texttt{anchor.y} or j=\texttt{anchor.x}}{0}{otherwise}
    • CV_SHAPE_CUSTOM (100) - custom structuring element (OpenCV 1.x API)
  • ksize – Size of the structuring element.
  • cols – Width of the structuring element
  • rows – Height of the structuring element
  • anchor – Anchor position within the element. The default value (-1,-1) means that the anchor is at the center. Note that only the shape of a cross-shaped element depends on the anchor position. In other cases the anchor just regulates how much the result of the morphological operation is shifted.
The structuring element is required to be passed to createMorphologyFilter(), erode(), dilate() or morphologyEx().

You can also construct an arbitrary binary mask yourself and use it as the structuring element.

  Example:

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

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

using namespace cv;
using namespace std;

int main()
{

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

    return 0;
}

--------

3 comments:

  1. How is Size() defined? Is 4*morph_size the number of rows, or the number of columns? Likewise for Point().

    ReplyDelete
  2. I agree with Anonymous above. What is the "Size" of the structuring element?

    ReplyDelete
    Replies
    1. Never mind. I was initially confused by the fact that both Size and cols and rows were listed under "Parameters" before I realised that there are no rows and cols parameters to the getStructuringElement() function, but that they are just used to define the dimensions of the structuring element.

      Delete