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.
- Determine the path to the file:
- Initialize the image size parameter.
- Import the image from the file:
- Initialize the display:
- Create a window and display the image:
- Make a mask of the core and scale it to range from 0 to 255:
- Subtract the scaled mask from the original image:
- Create another window and display the difference of the original image and the scaled mask:
- Determine the statistics of the difference image:
- Print out the resulting statistics:
- Derive a mask of the non-zero values of the image:
- Determine the statistics of the image with the mask applied:
- Print out the resulting statistics:
WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $ TITLE = 'Earth Mantle Convection' TV, imageThe following figure shows the original convection image.
WINDOW, 2, XSIZE = imageSize[0], YSIZE = imageSize[1], $ TITLE = 'Difference of Original & Core' TV, differenceThe following figure shows the convection of just the earth's mantle.
IMAGE_STATISTICS, difference, COUNT = pixelNumber, $ DATA_SUM = pixelTotal, MAXIMUM = pixelMax, $ MEAN = pixelMean, MINIMUM = pixelMin, $ STDDEV = pixelDeviation, $ SUM_OF_SQUARES = pixelSquareSum, $ VARIANCE = pixelVariance
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 = ', pixelVarianceIDL 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
IMAGE_STATISTICS, difference, COUNT = pixelNumber, $ DATA_SUM = pixelTotal, MASK = nonzeroMask, $ MAXIMUM = pixelMax, MEAN = pixelMean, $ MINIMUM = pixelMin, STDDEV = pixelDeviation, $ SUM_OF_SQUARES = pixelSquareSum, $ VARIANCE = pixelVariance
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 = ', pixelVarianceIDL 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.53The 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.

