Previous Image Processing : Contrasting and Filtering Next

Smoothing an Image

Smoothing is often used to reduce noise within an image or to produce a less pixelated image. Most smoothing methods are based on low pass filters. See Low Pass Filtering for more information.

Smoothing is also usually based on a single value representing the image, such as the average value of the image or the middle (median) value. The following examples show how to smooth using average and middle values:

Smoothing with Average Values

The following example shows how to use the SMOOTH function to smooth an image with a moving average. Surfaces of the original and smooth images are displayed to show how discontinuous values are made more continuous. This example uses the photomicrograph image of human red blood cells contained within the rbcells.jpg file in the examples/data directory. Complete the following steps for a detailed description of the process.


Example Code
See smoothingwithsmooth.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example.

  1. Import the image from the rbcells.jpg file:
  2. file = FILEPATH('rbcells.jpg', $  
       SUBDIRECTORY = ['examples', 'data'])  
    READ_JPEG, file, image  
    imageSize = SIZE(image, /DIMENSIONS)  
    

     

  3. Initialize the display:
  4. DEVICE, DECOMPOSED = 0  
    LOADCT, 0  
    

     

  5. Create a window and display the original image:
  6. WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Original Image'  
    TV, image  
    

     

    The following figure shows the original image. This image contains many varying pixel values within the background.

     

    Figure 8-24: Original Red Blood Cells Image

    Figure 8-24: Original Red Blood Cells Image

     

  7. Create another window and display the original image as a surface:
  8. WINDOW, 1, TITLE = 'Original Image as a Surface'  
    SHADE_SURF, image, /XSTYLE, /YSTYLE, CHARSIZE = 2., $  
       XTITLE = 'Width Pixels', $  
       YTITLE = 'Height Pixels', $  
       ZTITLE = 'Intensity Values', $  
       TITLE = 'Red Blood Cell Image'  
    

     

    The following figure shows the surface of the original image. This image contains many discontinuous values shown as sharp peaks (spikes) in the middle range of values.

     

    Figure 8-25: Surface of Original Red Blood Cells Image

    Figure 8-25: Surface of Original Red Blood Cells Image

     

  9. Smooth the image with the SMOOTH function, which uses the average value of each group of pixels affected by the 5 by 5 kernel applied to the image:
  10. smoothedImage = SMOOTH(image, 5, /EDGE_TRUNCATE)  
    

     

    The width argument of 5 is used to specify that a 5 by 5 smoothing kernel is to be used.

     

  11. Create another window and display the smoothed image as a surface:
  12. WINDOW, 2, TITLE = 'Smoothed Image as a Surface'  
    SHADE_SURF, smoothedImage, /XSTYLE, /YSTYLE, CHARSIZE = 2., $  
       XTITLE = 'Width Pixels', $  
       YTITLE = 'Height Pixels', $  
       ZTITLE = 'Intensity Values', $  
       TITLE = 'Smoothed Cell Image'  
    

     

    The following figure shows the surface of the smoothed image. The sharp peaks in the original image have been decreased.

     

    Figure 8-26: Surface of Average-Smoothed Red Blood Cells Image

    Figure 8-26: Surface of Average-Smoothed Red Blood Cells Image

     

  13. Create another window and display the smoothed image:
  14. WINDOW, 3, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Smoothed Image'  
    TV, smoothedImage  
    

     

    The following figure shows the smoothed image. Less variations between pixel values occur within the background of the resulting image.

     

    Figure 8-27: Average-Smoothed Red Blood Cells Image

    Figure 8-27: Average-Smoothed Red Blood Cells Image

Smoothing with Median Values

The following example shows how to use IDL's MEDIAN function to smooth an image by median values. Surfaces of the original and smooth images are displayed to show how discontinuous values are made more continuous. This example uses the photomicrograph image of human red blood cells contained within the rbcells.jpg file in the examples/data directory. Complete the following steps for a detailed description of the process.


Example Code
See smoothingwithmedian.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example.

  1. Import the image from the rbcells.jpg file:
  2. file = FILEPATH('rbcells.jpg', $  
       SUBDIRECTORY = ['examples', 'data'])  
    READ_JPEG, file, image  
    imageSize = SIZE(image, /DIMENSIONS)  
    

     

  3. Initialize the display:
  4. DEVICE, DECOMPOSED = 0  
    LOADCT, 0  
    

     

  5. Create a window and display the original image:
  6. WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Original Image'  
    TV, image  
    

     

    The following figure shows the original image. This image contains many varying pixel values within the background.

     

    Figure 8-28: Original Red Blood Cells Image

    Figure 8-28: Original Red Blood Cells Image

     

  7. Create another window and display the original image as a surface:
  8. WINDOW, 1, TITLE = 'Original Image as a Surface'  
    SHADE_SURF, image, /XSTYLE, /YSTYLE, CHARSIZE = 2., $  
       XTITLE = 'Width Pixels', $  
       YTITLE = 'Height Pixels', $  
       ZTITLE = 'Intensity Values', $  
       TITLE = 'Red Blood Cell Image'  
    

     

    The following figure shows the surface of the original display. This image contains many discontinuous values shown as sharp peaks (spikes) in the middle range of values.

     

    Figure 8-29: Surface of Original Red Blood Cells Image

    Figure 8-29: Surface of Original Red Blood Cells Image

     

  9. Smooth the image with the MEDIAN function, which uses the middle value of each group of pixels affected by the 5 by 5 kernel applied to the image:
  10. smoothedImage = MEDIAN(image, 5)  
    

     

  11. Create another window and display the smoothed image as a surface:
  12. WINDOW, 2, TITLE = 'Smoothed Image as a Surface'  
    SHADE_SURF, smoothedImage, /XSTYLE, /YSTYLE, CHARSIZE = 2., $  
       XTITLE = 'Width Pixels', $  
       YTITLE = 'Height Pixels', $  
       ZTITLE = 'Intensity Values', $  
       TITLE = 'Smoothed Cell Image'  
    

     

    The following figure shows the smoothed surface. The sharp peaks in the original image are decreased by the MEDIAN function.

     

    Figure 8-30: Surface of Middle-Smoothed Red Blood Cells Image

    Figure 8-30: Surface of Middle-Smoothed Red Blood Cells Image

     

  13. Create another window and display the smoothed image:
  14. WINDOW, 3, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Smoothed Image'  
    TV, smoothedImage  
    

     

    The following figure shows the results of applying the median filter. Less variations occur within the background of the resulting image, yet feature edges remain clearly defined.

     

    Figure 8-31: Middle-Smoothed Red Blood Cells Image

    Figure 8-31: Middle-Smoothed Red Blood Cells Image

  IDL Online Help (March 06, 2007)