Previous Image Processing : Contrasting and Filtering Next

Working with Histograms

The histogram of an image shows the number of pixels for each pixel value within the range of the image. If the minimum value of the image is 0 and the maximum value of the image is 255, the histogram of the image shows the number of pixels for each value ranging between and including 0 and 255. Peaks in the histogram represent more common values within the image that usually consist of nearly uniform regions. Valleys in the histogram represent less common values. Empty regions within the histogram indicate that no pixels within the image contain those values.

The following figure shows an example of a histogram and its related image. The most common value in this image is 180, composing the background of the image. Although the background appears nearly uniform, it contains many small variations.

Figure 8-3: Example of a Histogram (left) and Its Related Image (right)

Figure 8-3: Example of a Histogram (left) and Its Related Image (right)

The contrast of these variations can be increased by equalizing the image's histogram. Either the image's color table or the image itself can be equalized based on the information within the image's histogram. This section shows how to enhance the contrast within an image by modifying the image itself. See H_EQ_CT for more information on enhancing contrast by modifying the color table of an image using the image's histogram information.

During histogram equalization, the values occurring in the empty regions of the histogram are redistributed equally among the peaks and valleys. This process creates intensity gradients within these regions (replacing nearly uniform values), thus highlighting minor variations.

IDL contains the ability to perform histogram equalization and adaptive histogram equalization. The following sections show how to use these forms of histogram equalization to modify images within IDL:

Equalizing with Histograms

You can use the HIST_EQUAL function to perform basic histogram equalization within IDL. Unlike histogram equalization methods performed on color tables, the HIST_EQUAL function results in a modified image, which has a different histogram than the original image. The resulting image shows more variations (increased contrast) within uniform areas than the original image.

The following example applies histogram equalization to an image of mineral deposits to reveal previously indistinguishable features. This example uses the mineral.png file in the examples/data directory. Complete the following steps for a detailed description of the process.


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

  1. Import the image and color table from the mineral.png file:
  2. file = FILEPATH('mineral.png', $  
       SUBDIRECTORY = ['examples', 'data'])  
    image = READ_PNG(file, red, green, blue)  
    imageSize = SIZE(image, /DIMENSIONS)  
    

     

  3. Initialize the display:
  4. DEVICE, DECOMPOSED = 0  
    TVLCT, red, green, blue  
    

     

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

     

    The following figure shows the original image.

     

    Figure 8-4: The Mineral Image and Its Related Color Table

    Figure 8-4: The Mineral Image and Its Related Color Table

     

  7. Create another window and display the histogram of the original image:
  8. WINDOW, 1, TITLE = 'Histogram of Image'  
    PLOT, HISTOGRAM(image), /XSTYLE, /YSTYLE, $  
       TITLE = 'Mineral Image Histogram', $  
       XTITLE = 'Intensity Value', $  
       YTITLE = 'Number of Pixels of That Value'  
    

     

    The following figure shows the original image's histogram.

     

    Figure 8-5: Histogram of the Original Image

    Figure 8-5: Histogram of the Original Image

     

  9. Histogram equalize the image:
  10. equalizedImage = HIST_EQUAL(image)  
    

     

  11. Create another window and display the equalized image:
  12. WINDOW, 2, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Equalized Image'  
    TV, equalizedImage  
    

     

    The following figure shows the results of the histogram equalization. Small variations within the uniform regions are now much more noticeable.

     

    Figure 8-6: Equalized Mineral Image

    Figure 8-6: Equalized Mineral Image

     

  13. Create another window and display the histogram of the equalized image:
  14. WINDOW, 3, TITLE = 'Histogram of Equalized Image'  
    PLOT, HISTOGRAM(equalizedImage), /XSTYLE, /YSTYLE, $  
       TITLE = 'Equalized Image Histogram', $  
       XTITLE = 'Intensity Value', $  
       YTITLE = 'Number of Pixels of That Value'  
    

     

    The following figure shows the modified image's histogram. The resulting histogram is now more uniform than the original histogram.

     

    Figure 8-7: Histogram of the Equalized Image

    Figure 8-7: Histogram of the Equalized Image

Adaptive Equalizing with Histograms

Adaptive histogram equalization involves applying equalization based on the local region surrounding each pixel. Each pixel is mapped to an intensity proportional to its rank within the surrounding neighborhood. This type of equalization also tends to reduce the disparity between peaks and valleys within the image's histogram.

You can use the ADAPT_HIST_EQUAL function to perform the adaptive histogram equalization process within IDL. Like the HIST_EQUAL function, the ADAPT_HIST_EQUAL function results in a modified image, which has a different histogram than the original image.

The following example applies adaptive histogram equalization to an image of mineral deposits to reveal previously indistinguishable features. This example uses a the mineral.png file in the examples/data directory. Complete the following steps for a detailed description of the process.


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

  1. Import the image and color table from the mineral.png file:
  2. file = FILEPATH('mineral.png', $  
       SUBDIRECTORY = ['examples', 'data'])  
    image = READ_PNG(file, red, green, blue)  
    imageSize = SIZE(image, /DIMENSIONS)  
    

     

  3. Initialize the display:
  4. DEVICE, DECOMPOSED = 0  
    TVLCT, red, green, blue  
    

     

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

     

    The following figure shows the original image.

     

    Figure 8-8: The Mineral Image and Its Related Color Table

    Figure 8-8: The Mineral Image and Its Related Color Table

     

  7. Create another window and display the histogram of the original image:
  8. WINDOW, 1, TITLE = 'Histogram of Image'  
    PLOT, HISTOGRAM(image), /XSTYLE, /YSTYLE, $  
       TITLE = 'Mineral Image Histogram', $  
       XTITLE = 'Intensity Value', $  
       YTITLE = 'Number of Pixels of That Value'  
    

     

    The following figure shows the resulting display.

     

    Figure 8-9: Histogram of the Original Image

    Figure 8-9: Histogram of the Original Image

     

  9. Apply adaptive histogram equalization to the image:
  10. equalizedImage = ADAPT_HIST_EQUAL(image)  
    

     

  11. Create another window and display the equalized image:
  12. WINDOW, 2, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Adaptive Equalized Image'  
    TV, equalizedImage  
    

     

    The following figure shows the results of adaptive histogram equalization. All the variations within the image are now noticeable.

     

    Figure 8-10: Adaptive Equalized Mineral Image

    Figure 8-10: Adaptive Equalized Mineral Image

     

  13. Create another window and display the histogram of the equalized image:
  14. WINDOW, 3, TITLE = 'Histogram of Adaptive Equalized Image'  
    PLOT, HISTOGRAM(equalizedImage), /XSTYLE, /YSTYLE, $  
       TITLE = 'Adaptive Equalized Image Histogram', $  
       XTITLE = 'Intensity Value', $  
       YTITLE = 'Number of Pixels of That Value'  
    

     

    The following figure shows the modified image's histogram. The resulting histogram contains no empty regions and fewer extreme peaks and valleys than the original image.

     

    Figure 8-11: Histogram of the Adaptive Equalized Image

    Figure 8-11: Histogram of the Adaptive Equalized Image

  IDL Online Help (March 06, 2007)