Median Smoothing

"Median Smoothing" is also called as "Median Blurring" or "Median Filtering". This is also a common smoothing technique. The input image is convolved with a Median kernel. Median filtering is widely used in edge detection algorithms because under certain conditions, it preserves edges while removing noise. For more information about median smoothing, please refer to here.

OpenCV Code

Following OpenCV example is also as same as the previous except for one line.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main( int argc, char** argv )
{
//create 2 empty windows
namedWindow( "Original Image" , CV_WINDOW_AUTOSIZE );
namedWindow( "Smoothed Image" , CV_WINDOW_AUTOSIZE );

// Load an image from file
Mat src = imread( "MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED );

//show the loaded image
imshow( "Original Image", src );

Mat dst;
char zBuffer[35];

for int i = 1; i  <  31; i = i + 2 )

//copy the text to the "zBuffer"
_snprintf_s(zBuffer, 35,"Kernel Size : %d x %d", i, i);

//smooth the image using Median kernel in the "src" and save it to "dst"
medianBlur( src, dst, i );

//put the text in the "zBuffer" to the "dst" image
putText( dst, zBuffer, Point( src.cols/4, src.rows/8), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255), 2 );

//show the blurred image with the text
imshow( "Smoothed Image", dst );

//wait for 2 seconds
int c = waitKey(2000);

//if the "esc" key is pressed during the wait, return
if (c == 27)
{
return 0;
}
}

//make the "dst" image, black
dst = Mat::zeros( src.size(), src.type() );

//copy the text to the "zBuffer"
_snprintf_s(zBuffer, 35,"Press Any Key to Exit");

//put the text in the "zBuffer" to the "dst" image
putText( dst, zBuffer, Point( src.cols/4,  src.rows / 2), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );

//show the black image with the text
imshow( "Smoothed Image", dst );

//wait for a key press infinitely
waitKey(0);
     
return 0;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
You can download this OpenCV visual C++ project from here

Smooth Image using Median Filter (17 x 17)
Smooth Image using Median Filter (17 x 17)


New OpenCV Functions

  • void medianBlur( InputArray src, OutputArray dst, int ksize )

This OpenCV function smooth the input image using a Median filter. All channels of the input image is processed independently. This method works in-place.
  • src - Input image ( images with 1, 3 or 4 channels / Image depth should be CV_8U for any value of "ksize". If "ksize" equals 3 or 5, image depths of CV_16U and CV_32F are also supported.
  • dst - Output image (It will be of same size and depth as the input image)
  • ksize - size of the filter ( It should be odd and greater than 1 ) (Note - The resulting filter has the size of ksize ksize )