Dilate Images and Videos

Here is the original image which I am going to filter using above methods.

Dilating is something like opposite of the eroding an image. Here is the OpenCV code.

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

int main()
        //display the original image
        IplImage* img = cvLoadImage("C:/MyPic.jpg");
        cvShowImage("MyWindow", img);

        //dilate and display the dilated image
        cvDilate(img, img, 0, 2);
        cvShowImage("Dilated", img);

        //cleaning up
        return 0;


You can download this OpenCV visual c++ project from here

Dilated Image

New OpenCV functions which are not found earlier are explained here
  • cvDilate(img, img, 0, 2)
The 1st parameter is the source image.
The 2nd parameter is the destination image which is to be the dilated image.
Here the 3rd parameter is the structuring element used for dilation. If it is 0, a 3×3 rectangular structuring element is used. 
The 4th parameter is the number of times, dilation is applied.
This function can process images in place. That means same variable can be used for the 1st and 2nd parameters.


  1. dilate ( src, dest, getStructuringElement(MORPH_RECT,Size(3,3)) );

  2. very helpful thank you so much
    can you write a code for background subtraction with average method this one is alos change in new opencv c++ api.
    by the way thanks again this all code are very very helpful . you are done great job

  3. Why are some of the tutorials written in another version of opencv? Is there any particular reason for this?