IMAGE_DENOISE is a MATLAB program which uses the median filter to try to remove noise from an image.
In MATLAB, a black and white or 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.
An image can be read into MATLAB using the imread function in the Image Processing Toolbox.
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.
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.
MATLAB provides a command medfilt2 which will do this, 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", using finally the median command to get what we want.
Along the way, it should become clear how to deal with grayscale and RGB images. Moreover, some other issues arise, such as ensuring that the processed data retains the "unsigned 8-bit integer" property of the original data.
The functions image_denoise_gray_news and image_denoise_rgb_news use the central pixel and its north, east, west and south neighbors.
The functions image_denoise_gray_3x3 and image_denoise_rgb_3x3 use the central pixel and the entire layer of 8 pixels that lie at most one pixel away in the north/south and east/west directions.
The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.
IMAGE_DENOISE is available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version and a MATLAB version.
IMAGE_COMPONENTS, a MATLAB library which seeks the connected "nonzero" or "nonblack" components of an image or integer vector, array or 3D block.
IMAGE_CONTRAST, a MATLAB program which applies image processing techniques to increase the contrast in an image.
IMAGE_DECIMATE, a MATLAB library which compresses an image by dropping the even rows and columns of data.
IMAGE_DENOISE_SPMD, a MATLAB program which demonstrates the SPMD parallel programming feature for image operations; the client reads an image, the workers process portions of it, and the client assembles and displays the results.
IMAGE_DIFFUSE, a MATLAB library which uses diffusion to smooth out an image.
IMAGE_DOUBLE, a MATLAB library which doubles the height and width of an image by repeating each row and column.
IMAGE_EDGE, a MATLAB library which demonstrates a simple procedure for edge detection in images.
IMAGE_MATCH_GENETIC, a MATLAB program which tries to match a 256x256 JPEG image by blending 32 colored rectangles, using ideas from genetic algorithms, based on an example by Nick Berry.
IMAGE_NOISE, MATLAB programs which add noise to an image.
IMAGE_QUANTIZATION, a MATLAB library which demonstrates how the KMEANS algorithm can be used to reduce the number of colors or shades of gray in an image.
IMAGE_RGB_TO_GRAY, MATLAB programs which makes a grayscale version of an RGB image.
IMAGE_THRESHOLD, MATLAB programs which make a black and white version of a grayscale image by setting all pixels below or above a threshold value to black or white.
MathWorks documentation for the Image Processing Toolbox is available at http://www.mathworks.com/access/helpdesk/help/pdf_doc/images/images_tb.pdf.
BALLOONS is an RGB image of a couple holding balloons.
GLASSWARE is a grayscale image of a few glass vases.
You can go up one level to the MATLAB source codes.