# 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**

#include<iostream> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; //sort the window using insertion sort //insertion sort is best for this sorting void insertionSort(int window[]) { int temp, i , j; for(i = 0; i < 9; i++){ temp = window[i]; for(j = i-1; j >= 0 && temp < window[j]; j--){ window[j+1] = window[j]; } window[j+1] = temp; } } int main() { Mat src, dst; // Load an image src = imread("book.png", CV_LOAD_IMAGE_GRAYSCALE); if( !src.data ) { return -1; } //create a sliding window of size 9 int window[9]; dst = src.clone(); for(int y = 0; y < src.rows; y++) for(int x = 0; x < src.cols; x++) dst.at<uchar>(y,x) = 0.0; for(int y = 1; y < src.rows - 1; y++){ for(int x = 1; x < src.cols - 1; x++){ // Pick up window element window[0] = src.at<uchar>(y - 1 ,x - 1); window[1] = src.at<uchar>(y, x - 1); window[2] = src.at<uchar>(y + 1, x - 1); window[3] = src.at<uchar>(y - 1, x); window[4] = src.at<uchar>(y, x); window[5] = src.at<uchar>(y + 1, x); window[6] = src.at<uchar>(y - 1, x + 1); window[7] = src.at<uchar>(y, x + 1); window[8] = src.at<uchar>(y + 1, x + 1); // sort the window to find median insertionSort(window); // assign the median to centered element of the matrix dst.at<uchar>(y,x) = window[4]; } } namedWindow("final"); imshow("final", dst); namedWindow("initial"); imshow("initial", src); waitKey(); return 0; }

**Output**

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++){

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

Hello arnanda

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

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++){

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.

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

It cleans the image, sets it to black

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

i didn't undestand why you've add this

#include

#include

#include

my comment is not shown !!!! why

Great Technique. Clipping Path Service Provider

hii Bibek Subedi

could you please help me

only some part of the image is filtered ,remaining is getting displayed as its source image.

Input image should be loaded in grayscale. Please check.