Filtering Images

Image filtering is an important part of computer vision. For most of computer vision applications, filtering should be done before anything else. OpenCV supports lots of in-build filtering methods for images. Here is the list of filtering methods that I am going to discuss with you in the following posts (with OpenCV 2.4.5 and C++ )



Here is the list of image filtering methods which are explained using examples with OpenCV 2.1  in C style (not C++)
  • Eroding
  • Dilating
  • Inverting
Here is the original image which I am going to filter using above methods.

Original Image
If you have not install and configure OpenCV yet, please refer to Installing & Configuring with Visual Studio.



Eroding

Eroding is a simple way of filtering images. Here is how it can be done with OpenCV.

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

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

        //erode and display the eroded image
        cvErode(img, img, 0, 2);
        cvNamedWindow("Eroded");
        cvShowImage("Eroded", img);
       
        cvWaitKey(0);
       
        //cleaning up
        cvDestroyWindow("MyWindow");
        cvDestroyWindow("Eroded");
        cvReleaseImage(&img);
       
        return 0;
}

///////////////////////////////////////////////////////////////////////////////////////


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

Eroded Image

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

If you want more explanation about various methods in the above computer application , please refer to Capturing Images & Videos.



Dilating 

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");
        cvNamedWindow("MyWindow");
        cvShowImage("MyWindow", img);

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

        cvWaitKey(0);
       
        //cleaning up
        cvDestroyWindow("MyWindow");
        cvDestroyWindow("Dilated");
        cvReleaseImage(&img);
       
        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.


Inverting 

Inverting an image is like taking the negative of an image.

///////////////////////////////////////////////////////////////////////////////////////


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

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

        //invert and display the inverted image
        cvNot(img, img);
        cvNamedWindow("Inverted");
        cvShowImage("Inverted", img);

        cvWaitKey(0);
      
        //cleaning up
        cvDestroyWindow("MyWindow");
        cvDestroyWindow("Inverted");
        cvReleaseImage(&img);
      
        return 0;
}


///////////////////////////////////////////////////////////////////////////////////////


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

Inverted Image

 New OpenCV functions which are not found earlier are explained here
  • cvNot(img, img)
This function inverts every bit in every element of the image in the 1st parameter and places the result in the image in the 2nd parameter.
This function can process images in place. That means same variable can be used for the 1st and 2nd parameters.

e.g - For a 8 bit image, the value 0 will be mapped to (255-0)=255
                                        the value 46 will be mapped to (255-46)=209

        For a 16 bit image, the value 0 will be mapped to (65535-0)=65535
                                   the value 46 will be mapped to (65535-46)=65489



Next Tutorial : How to Add Trackbar

Previous Tutorial : Write Image & Video to File




17 comments:

  1. Awesome blog!!! Really useful!!! Thank You very much!!

    ReplyDelete
  2. Thanks for blog.

    ReplyDelete
  3. how to sharpen the image using opencv? i want to use unsharp masking to sharp the image tell me their is any function in opencv to do this masking

    ReplyDelete
  4. Dear Mr. Shermal
    Thank you very much for the blog!
    - Vishal V. Parkar

    ReplyDelete
  5. Very nice blog with excelent explained material. Thank you

    ReplyDelete
  6. I have two images of shirts ,one is standard size shirt image while the other image is of any size shirt. I want to make the any other size shirt image equal to standard size shirt.how can I do this in opencv?????(this cannot be done using resize command because shirt have different parts like chest size and abdomen size)

    ReplyDelete
  7. im using visual studio 2013 and i cant include stdafx.h please help

    ReplyDelete
    Replies
    1. hi,I want to apply Interquartile Range on image...IQR=Q3-Q1...I dont know how to find IQR from the 8*8 window in opencv...Plz can you help me...my email id is ardev.9feb2012@gmail.com

      Delete
  8. Dear shermal I tried your all before codes on my opencv 2.4.9 and just i commented the "#include stdhafx" and they worked properly but none of this codes worked ,can you help me please which changes should I make ?

    ReplyDelete
  9. Very helpful set of tutorials !! Thanx a lot !!

    ReplyDelete
  10. Thanks for this explanation.
    i have been looking for such.
    Please, what if i want to use opencv version 2.1, will it work

    ReplyDelete
  11. Thank you very much for th explanation!!...Loved the tutorials.

    ReplyDelete