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?

    1. using namespace cv;
      using namespace std;

      int main(int argc, char** argv)
      // Read the image file
      Mat image = imread("C:\\Users\\SIF\\source\\repos\\ConsoleApplication1\\ConsoleApplication1\\Car.jpg");

      // Check for failure
      if (image.empty())
      cout << "Could not open or find the image" << endl;
      cin.get(); //wait for any key press
      return -1;

      //Erode the image with 3x3 kernel
      Mat image_eroded_with_3x3_kernel;
      dilate (image, image_eroded_with_3x3_kernel, getStructuringElement(MORPH_DILATE, Size(3, 3)));
      //erode(image, image_eroded_with_3x3_kernel, getStructuringElement(MORPH_DILATE, Size(3, 3)));

      //Erode the image with 5x5 kernel
      Mat image_eroded_with_5x5_kernel;
      dilate(image, image_eroded_with_5x5_kernel, getStructuringElement(MORPH_RECT, Size(5, 5)));

      //Define names of the windows
      String window_name = "Lotus";
      String window_name_eroded_with_3x3_kernel = "Lotus eroded with 3 x 3 kernel";
      String window_name_eroded_with_5x5_kernel = "Lotus eroded with 5 x 5 kernel";

      // Create windows with above names

      // Show our images inside the created windows.
      imshow(window_name, image);
      imshow(window_name_eroded_with_3x3_kernel, image_eroded_with_3x3_kernel);
      imshow(window_name_eroded_with_5x5_kernel, image_eroded_with_5x5_kernel);

      waitKey(0); // Wait for any keystroke in the window

      destroyAllWindows(); //destroy all opened windows

      return 0;