You are here:Home » image processing » OPENCV Tutorial - Basic operations for images Dilate/Erode

OPENCV Tutorial - Basic operations for images Dilate/Erode

Let us do some more image processing in openCV. In this turorial we will do Dilate/Erode operation. This is a basic Morphological Operations.  To learn more about  Morphological Operations I have found a very good tutorial. You can find it here



Now we will use function Erode ( - Erodes image by using arbitrary structuring element and) Dilate -(Dilates image by using arbitrary structuring element).

lets learn a bit about functions -


void cvErode( const CvArr* src, CvArr* dst, IplConvKernel* element=NULL, int iterations=1 );

src
Source image.
dst
Destination image.
element
Structuring element used for erosion. If it is NULL, a 3×3 rectangular structuring element is used.
iterations
Number of times erosion is applied.
The function cvErode erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:



void cvDilate( const CvArr* src, CvArr* dst, IplConvKernel* element=NULL, int iterations=1 );

src
Source image.
dst
Destination image.
element
Structuring element used for erosion. If it is NULL, a 3×3 rectangular structuring element is used.
iterations
Number of times erosion is applied.
The function cvDilate dilates the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the maximum is taken:

NOTE:- Both functions supports the in-place mode. Erosion can be applied several (iterations) times. In case of color image each channel is processed independently.


After enough learning lets write a program :-


#include "cxcore.h"
#include "highgui.h"
int main()
{
            IplImage* newImg = NULL;
IplImage* dilateImg = NULL;
IplImage* erodeImg = NULL;

cvNamedWindow("src", 1);
cvNamedWindow("dilate",1);
cvNamedWindow("erode",1);

//load original image
newImg = cvLoadImage("apple.bmp",1);
cvShowImage( "src", newImg );

//make a copy of the original image
dilateImg=cvCloneImage( newImg );
erodeImg=cvCloneImage( newImg );

//dilate image
cvDilate(newImg,dilateImg,NULL,4);

//erode image
cvErode(newImg,erodeImg,NULL,4);
cvShowImage( "dilate", dilateImg );
cvShowImage( "erode", erodeImg );
cvWaitKey(0);
cvDestroyWindow( "src" ); cvDestroyWindow( "dilate" ); cvDestroyWindow( "erode" );
cvReleaseImage( &newImg ); cvReleaseImage( &dilateImg ); cvReleaseImage( &erodeImg );
return 0;

}


0 comments:

Post a Comment