median filter
Median filter module
Interface
#include <codecogs/computing/graphics/median filter.h>
using namespace Computing::Graphics;
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 1References
Gonzalez, R. and Woods, R. "Digital Image Processing", 3rd ed.
Wikipedia, "Median Filter." http://en.wikipedia.org/wiki/Median_filter
FUNCTION
median
Parameters
Returns
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).
Steve Simon (October 2008)
FUNCTION
fillborder
This is an ancillary function called by median().
Example 1
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 1Parameters
Returns
The input array containing the image must not be NULL, and the dimensions must be greater than 0.
Steve Simon (October 2008)
FUNCTION
fcmp
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 1
[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.Parameters
Returns
None.
Steve Simon (October 2008)
References
Kernighan, B. and Pike, R. "The Practice of Programming"