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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #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.