Showing posts with label Morphological Operation. Show all posts
Showing posts with label Morphological Operation. Show all posts

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;
}

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




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;
}

--------

Erosion or dilation (Morphological operations)

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

Parameters:
  • src – input image; the number of channels can be arbitrary, but the depth should be one of CV_8U, CV_16U, CV_16S, CV_32F` or ``CV_64F.
  • dst – output image of the same size and type as src.
  • element – structuring element used for erosion; if element=Mat() , a 3 x 3 rectangular structuring element is used.
  • anchor – position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
  • iterations – number of times erosion is applied.
  • borderType – pixel extrapolation method (see borderInterpolate() for details).
  • borderValue – border value in case of a constant border (see createMorphologyFilter() for details).
void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )

Parameters: Same as erode(...)

Steps:

  1. Load an image 
  2. Create a structuring element
  3. Apply erosion or dilation on the image
  4. Show result

Functions:

Example:

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

using namespace cv;
using namespace std;

int main( )
{

       Mat image,dst;
       image = imread("lena.jpg", CV_LOAD_IMAGE_COLOR);  

       // Create a structuring element
       int erosion_size = 6;  
       Mat element = getStructuringElement(cv::MORPH_CROSS,
              cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),
              cv::Point(erosion_size, erosion_size) );

       // Apply erosion or dilation on the image
       erode(image,dst,element);  // dilate(image,dst,element);
      
       namedWindow( "Display window", CV_WINDOW_AUTOSIZE );  
       imshow( "Display window", image );                 

       namedWindow( "Result window", CV_WINDOW_AUTOSIZE );   
       imshow( "Result window", dst );

       waitKey(0);                                         
       return 0;
}
--------------

Result: