Median filter module

You're viewing an older version of this page (#794). View the current version.

View versions (2)

Interface

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);

void 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");
	}
}

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.

GPL Licence — free for non commercial use. See Licence details.

FUNCTION

median

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

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).

Author

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>
static float *fillborder(float img[], int nx, int ny);
void 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");
     }
}

Output:

The image with the border filled in is:
1 1 1
1 1 1 
1 1 1

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

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

Author

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]

static int fcmp(const void *p1, const void *p2);
void 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);
}

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

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

None.

Author

Steve Simon (October 2008)

References

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