I have forgotten

• http://facebook.com/
• https://www.google.com/accounts/o8/id
• https://me.yahoo.com # median filter

viewed 560 times and licensed 74 times
Median filter module
Controller: stevehs17 C++

## Overview

This module is used to apply a median filter to an image. A median filter works by setting, in turn, the value of each pixel in an image (except for the pixels on the border) to the median of the values of the pixels in a window surrounding the pixel. Median filters can be used to remove scattered noise from images and smooth them, while preserving the edges of objects in the image. There are different ways to deal with the pixels on the border of the image. In this implementation, the values of those pixels are set to the value of the nearest pixel not on an edge.

### Example 1

Apply a median filter to an image.
```#include <stdio.h>
float *median(float out[], const float in[], int nx, int ny);

int main(void)
{
enum {NCOL = 3, NROW = 3};
const float in[NCOL*NROW] = {1, 1, 1, 1, 2, 1, 1, 1, 1};
float out[NCOL*NROW];

median(out, in, NCOL, NROW);
printf("The filtered image is:\n");
for (int y = 0; y < NROW; y++) {
for (int x = 0; x < NCOL; x++)
printf("%0.f ", out[y*NCOL + x]);
printf("\n");
}
return 0;
}```
Output:
```The filtered image is:
1 1 1
1 1 1
1 1 1```

### References

Gonzalez, R. and Woods, R. "Digital Image Processing", 3rd ed.

Wikipedia, "Median Filter." http://en.wikipedia.org/wiki/Median_filter

### Authors

Steve Simon (October 2008)

## Median

 float*median( float* out const float* in int nx int ny )

### Note

The dimensions of the input image must be greater than 0, and the dimensions of the filter window must be odd.

If the dimensions of the image are smaller than the dimensions of the filter window, the input array is copied to the output array, and the function returns NULL.

Note that this function has only been tested for 3x3 filter windows, and for square images (i.e. the number of rows equals the number of columns).

### Parameters

 out array for output image in array for input image nx number of columns of image ny number of rows of image

### Returns

a pointer to the output image, or NULL if the input image is smaller than the filter window

### Authors

Steve Simon (October 2008)
##### Source Code

Source code is available when you agree to a GP Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

## Fillborder

 float*fillborder( float* img int nx int ny )
This is an ancillary function called by median().

### Example 2

Set the values of the pixels on the border to the value of the nearest interior pixel.
```#include <stdio.h>
float *fillborder(float img[], int nx, int ny);

int main(void)
{
enum {NCOL = 3, NROW = 3};
float img[NCOL*NROW] = {0, 0, 0, 0, 1, 0, 0, 0, 0};

fillborder(img, NCOL, NROW);
printf("The image with the border filled in is:\n");
for (int y = 0; y < NROW; y++) {
for (int x = 0; x < NCOL; x++)
printf("%0.f ", img[y*NCOL + x]);
printf("\n");
}
return 0;
}```
Output:
```The image with the border filled in is:
1 1 1
1 1 1
1 1 1```

### Note

The input array containing the image must not be NULL, and the dimensions must be greater than 0.

### Parameters

 img array for image to be processed nx number of columns of image ny number of rows of image

### Returns

a pointer to the processed image

### Authors

Steve Simon (October 2008)
##### Source Code

Source code is available when you agree to a GP Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

## Fcmp

 intfcmp( const void* p1 const void* p2 )
This is an ancillary function used by median() -- it is the comparison function passed to the standard library function qsort() in median(). The return values are those qsort() requires.

### Example 3

[Description of Example]
```int fcmp(const void *p1, const void *p2);

int main(void)
{
float f1, f2;
int result;

f1 = 1, f2 = 2;
result = fcmp(&f1, &f2);
printf("The result of comparing %.0f and %.0f is %d\n", f1, f2, result);

result = fcmp(&f2, &f1);
printf("The result of comparing %.0f and %.0f is %d\n", f2, f1, result);

result = fcmp(&f2, &f2);
printf("The result of comparing %.0f and %.0f is %d\n", f2, f2, result);

return 0;
}```
Output:
```The result of comparing 1 and 2 is -1.
The result of comparing 2 and 1 is 1.
The result of comparing 2 and 2 is 0.```

None.

### References

Kernighan, B. and Pike, R. "The Practice of Programming"

### Parameters

 p1 a pointer to the first number to compare p2 a pointer to the second number to compare

### Returns

-1 if p1 is less than p2, 0 if p1 equals p2, and 1 otherwise

### Authors

Steve Simon (October 2008)
##### Source Code

Source code is available when you agree to a GP Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.