image_denoise, a C code which uses the median filter to try to remove noise from an image.
A gray scale image can be represented using a 2D array of nonnegative integers over some range 0 to GMAX. The value 0 indicates black, and GMAX white. Intermediate values represent shades of gray in a natural way. Note, however, that the eye has a nonlinear response to intensity, so that the value GMAX/2 will not be perceived as halfway between 0 and GMAX. That is a separate issue.
A color image can be represented using a set of three 2D arrays, which can be thought of as R, G, and B, and which represent the intensity of the red, green and blue signals that combine to form the color image. A common maximum value is assumed, RGBMAX.
The ASCII PGM and PPM formats are a convenient, if somewhat inefficient, way to store such images. These ASCII formats are easy to read and write from a program.
In the example considered here, a good image is damaged by the addition of "salt and pepper" noise; that is, a scattering of individual pixels have been randomly reset to the lowest or highest possible values. In a gray scale picture, such noise looks as though salt and pepper were added to the picture.
It turns out that the median filter can clear up most of the noise, but not all of it. Because the noise values are extreme (0 or 255), it is not unlikely for a 2x2 block of noisy values to occur. Such a block will not be filtered out by the NEWS median filter!
Since an image is fairly "smooth", each good pixel should actually be fairly close to the values of good pixels nearby, while this will not be true for the salt and pepper pixels. So one way to make the noise go away, at the cost of some minor blurring, is to replace each pixel by the median value of itself and its neighbors.
There are sophisticated programs for applying the filters to a noisy image, with the user allowed to specify the shape of a rectangular neighborhood about each pixel. However, in this example, we go through some of the lower level details "by hand", applying simple versions of the median filter, in which each pixel is replaced by the median of nearby values.
By the way, my example programs need to read ASCII image files, either in PGM or PPM format. One way to create them is with the IMAGEMAGICK convert program:
convert balloons.png -compress none balloons.ppm
The computer code and data files described and made available on this web page are distributed under the MIT license
image_denoise is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.
IMAGE_COMPONENTS, a C code which seeks the connected "nonzero" or "nonblack" components or "segments" of an image or integer vector, array or 3D block.
IMAGE_DENOISE_OPENMP, a C code which applies simple filtering techniques to remove noise from an image, carrying out the operation in parallel using OpenMP.
IMAGE_EDGE, a C code which demonstrates a simple procedure for edge detection in images.
PGMA_IO, a C code which handles the ASCII Portable Gray Map (PGM) format.