Median Filter using C++ and OpenCV: Image Processing

Basic Theory

Median filter also reduces the noise in an image like low pass filter, but it is better than low pass filter in the sense that it preserves the edges and other details.  The process of calculating the intensity of a central pixel is same as that of low pass filtering except instead of averaging all the neighbors, we sort the window and replace the central pixel with a median from the sorted window. For example, lets we have a window like this

Now we sort the given window and get the sorted array as [1 1 2 2 3 3 5 6 7]. The median of this array is 4th element i.e 3. Now we replace the central element 7 with 4. That’s it.

Source Code

 

Output

 

SHARE Median Filter using C++ and OpenCV: Image Processing

You may also like...

12 Responses

  1. Anonymous says:

    can u explain about this function? for(int y = 0; y < src.rows; y++)
    for(int x = 0; x < src.cols; x++)
    dst.at(y,x) = 0.0;

    for(int y = 1; y < src.rows – 1; y++){
    for(int x = 1; x < src.cols – 1; x++){

  2. arnanda says:

    what the function of this code ? why use 0.0; ? at dst.at(y,x) = 0.0;

  3. Hello arnanda
    This is just an initialization. You can skip this step and this doesn't affect the output.

  4. Anonymous says:

    can u explain about this function? for(int y = 0; y < src.rows; y++)
    for(int x = 0; x < src.cols; x++)
    dst.at(y,x) = 0.0;

    for(int y = 1; y < src.rows – 1; y++){
    for(int x = 1; x < src.cols – 1; x++){

  5. The first three line is just an initialization of image pixels to 0. You can safely remove this step. The next two loops iterate over all the image pixels and apply the median filter.

  6. Anonymous says:

    we can not remove it if we use it in sobel operator. because it's use to iterates border of array

  7. Max says:

    It cleans the image, sets it to black

  8. mina says:

    please can you tell me how i can add opencv library ?

    i didn't undestand why you've add this

    #include
    #include
    #include

  9. Anonymous says:

    my comment is not shown !!!! why

  10. Anonymous says:

    hii Bibek Subedi
    could you please help me
    only some part of the image is filtered ,remaining is getting displayed as its source image.

  11. Anonymous says:

    Input image should be loaded in grayscale. Please check.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share