Calculating Image Statistics

The statistical properties of an image provide useful information, such as the total, mean, standard deviation, and variance of the pixel values. IDL's IMAGE_STATISTICS procedure can be used to calculate these statistical properties. The MOMENT, N_ELEMENTS, TOTAL, MAX, MEAN, MIN, STDDEV, and VARIANCE routines can also be used to calculate individual statistics, but most of these values are already provided by the IMAGE_STATISTICS procedure.

The following example shows how to use the IMAGE_STATISTICS procedure to calculate the statistical properties of an image. First, a mask is used to subtract the convection of the earth's core from the convection image contained in the convec.dat file, which is in the examples/data directory. The resulting difference represents the convection of just the earth's mantle. The IMAGE_STATISTICS procedure is applied to this difference image, and the resulting values are displayed in the Output Log. Then, a mask is derived for the non-zero values of the difference image, and the IMAGE_STATISTICS procedure is used again, this time with the mask applied through the MASK keyword. The resulting statistics can than be compared. The color table associated with this example is white for zero values and dark red for 255 values. Complete the following steps for a detailed description of the process.

Example Code
See calculatingstatistics.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering calculatingstatistics at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT calculatingstatistics.pro.

  1. Determine the path to the file:
  2. file = FILEPATH('convec.dat', $ 
       SUBDIRECTORY = ['examples', 'data']) 
    
  3. Initialize the image size parameter.
  4. imageSize = [248, 248] 
    
  5. Import the image from the file:
  6. image = READ_BINARY(file, DATA_DIMS = imageSize) 
    
  7. Initialize the display:
  8. DEVICE, DECOMPOSED = 0 
    LOADCT, 27 
    
  9. Create a window and display the image:
  10. WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $ 
       TITLE = 'Earth Mantle Convection' 
    TV, image 
     

    The following figure shows the original convection image.

    Figure 4-9: Earth Mantle Convection

    imgmath16.gif

  11. Make a mask of the core and scale it to range from 0 to 255:
  12. core = BYTSCL(image EQ 255) 
    
  13. Subtract the scaled mask from the original image:
  14. difference = image - core 
    
  15. Create another window and display the difference of the original image and the scaled mask:
  16. WINDOW, 2, XSIZE = imageSize[0], YSIZE = imageSize[1], $ 
       TITLE = 'Difference of Original & Core' 
    TV, difference 
     

    The following figure shows the convection of just the earth's mantle.

    Figure 4-10: The Difference of the Original Image and the Core

    imgmath18.gif

  17. Determine the statistics of the difference image:
  18. IMAGE_STATISTICS, difference, COUNT = pixelNumber, $ 
       DATA_SUM = pixelTotal, MAXIMUM = pixelMax, $ 
       MEAN = pixelMean, MINIMUM = pixelMin, $ 
       STDDEV = pixelDeviation, $ 
       SUM_OF_SQUARES = pixelSquareSum, $ 
       VARIANCE = pixelVariance 
    
  19. Print out the resulting statistics:
  20. PRINT, '' 
    PRINT, 'IMAGE STATISTICS:' 
    PRINT, 'Total Number of Pixels = ', pixelNumber 
    PRINT, 'Total of Pixel Values = ', pixelTotal 
    PRINT, 'Maximum Pixel Value = ', pixelMax 
    PRINT, 'Mean of Pixel Values = ', pixelMean 
    PRINT, 'Minimum Pixel Value = ', pixelMin 
    PRINT, 'Standard Deviation of Pixel Values = ', $ 
       pixelDeviation 
    PRINT, 'Total of Squared Pixel Values = ', $ 
       pixelSquareSum 
    PRINT, 'Variance of Pixel Values = ', pixelVariance 
     

    IDL prints:

    IMAGE STATISTICS: Total Number of Pixels = 61504 Total of Pixel Values = 2.61691e+006 Maximum Pixel Value = 253.000 Mean of Pixel Values = 42.5486 Minimum Pixel Value = 0.000000 Standard Deviation of Pixel Values = 48.7946 Total of Squared Pixel Values = 2.57779e+008 Variance of Pixel Values = 2380.91
  21. Derive a mask of the non-zero values of the image:
  22. nonzeroMask = difference NE 0 
    
  23. Determine the statistics of the image with the mask applied:
  24. IMAGE_STATISTICS, difference, COUNT = pixelNumber, $ 
       DATA_SUM = pixelTotal, MASK = nonzeroMask, $ 
       MAXIMUM = pixelMax, MEAN = pixelMean, $ 
       MINIMUM = pixelMin, STDDEV = pixelDeviation, $ 
       SUM_OF_SQUARES = pixelSquareSum, $ 
       VARIANCE = pixelVariance 
    
  25. Print out the resulting statistics:
  26. PRINT, '' 
    PRINT, 'MASKED IMAGE STATISTICS:' 
    PRINT, 'Total Number of Pixels = ', pixelNumber 
    PRINT, 'Total of Pixel Values = ', pixelTotal 
    PRINT, 'Maximum Pixel Value = ', pixelMax 
    PRINT, 'Mean of Pixel Values = ', pixelMean 
    PRINT, 'Minimum Pixel Value = ', pixelMin 
    PRINT, 'Standard Deviation of Pixel Values = ', $ 
       pixelDeviation 
    PRINT, 'Total of Squared Pixel Values = ', $ 
       pixelSquareSum 
    PRINT, 'Variance of Pixel Values = ', pixelVariance 
     

    IDL prints:

    MASKED IMAGE STATISTICS: Total Number of Pixels = 36325 Total of Pixel Values = 2.61691e+006 Maximum Pixel Value = 253.000 Mean of Pixel Values = 72.0416 Minimum Pixel Value = 1.00000 Standard Deviation of Pixel Values = 43.6638 Total of Squared Pixel Values = 2.57779e+008 Variance of Pixel Values = 1906.53

    The difference in the resulting statistics are because of the zero values, which are a part of the calculations for the image before the mask is applied.