Class Histogram


  • public class Histogram
    extends java.lang.Object
    An image histogram contains information about the distribution of channel values in an image.
    Since:
    3.0
    Author:
    Chris Jennings
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ALPHA
      A mask value to include the alpha channel in the histogram.
      static int BLUE
      A mask value to include the blue channel in the histogram.
      static int GREEN
      A mask value to include the green channel in the histogram.
      static int RED
      A mask value to include the red channel in the histogram.
    • Constructor Summary

      Constructors 
      Constructor Description
      Histogram()
      Creates a new, empty histogram.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.image.BufferedImage createHistogramImage​(java.awt.image.BufferedImage src)
      A utility method that creates an image of the histogram for an image, averaging the red, green, and blue channels together.
      static java.awt.image.BufferedImage createRGBHistogramImage​(java.awt.image.BufferedImage src)
      A utility method that creates an image of the histogram for an image, showing separate red, green, and blue channels.
      int getChannelMask()
      Returns the channel mask for the histogram.
      int getCountForLevel​(int level)
      Returns the number of samples counted for the given level.
      int getMaximumCount()
      Returns the maximum count for any level.
      double getRelativeCountForLevel​(int level)
      Returns the relative number of samples counted for the given level.
      int getSampleCount()
      Returns the total number of samples that have been measured.
      boolean isZeroAlphaIgnored()
      Returns true if pixels with an alpha of zero will not be included in the histogram.
      void measure​(int[] image)
      Measures the samples in an array of pixel data and incorporates them into the histogram statistics.
      void measure​(java.awt.image.BufferedImage image)
      Measures the samples in an image and incorporates them into the histogram statistics.
      void reset()
      Resets the histogram statistics.
      void setChannelMask​(int mask)
      Sets the channel mask for the histogram.
      void setZeroAlphaIgnored​(boolean ignoreZeroAlpha)
      Sets whether pixels with an alpha value of zero should be ignored.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BLUE

        public static final int BLUE
        A mask value to include the blue channel in the histogram.
        See Also:
        Constant Field Values
      • GREEN

        public static final int GREEN
        A mask value to include the green channel in the histogram.
        See Also:
        Constant Field Values
      • RED

        public static final int RED
        A mask value to include the red channel in the histogram.
        See Also:
        Constant Field Values
      • ALPHA

        public static final int ALPHA
        A mask value to include the alpha channel in the histogram.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Histogram

        public Histogram()
        Creates a new, empty histogram. The histogram will not be valid unit it has measured one or more image samples. The histogram will measure the red, green, and blue channels and ignore pixels with an alpha value of zero.
    • Method Detail

      • isZeroAlphaIgnored

        public boolean isZeroAlphaIgnored()
        Returns true if pixels with an alpha of zero will not be included in the histogram.
        Returns:
        true if transparent pixels are ignored
      • setZeroAlphaIgnored

        public void setZeroAlphaIgnored​(boolean ignoreZeroAlpha)
        Sets whether pixels with an alpha value of zero should be ignored. Ignored pixels are not counted in the histogram statistics.
        Parameters:
        ignoreZeroAlpha - true to ignore transparent pixels
      • getChannelMask

        public int getChannelMask()
        Returns the channel mask for the histogram.
        Returns:
        the bitwise-or of mask values for the channels included in the histogram
        See Also:
        setChannelMask(int)
      • setChannelMask

        public void setChannelMask​(int mask)
        Sets the channel mask for the histogram. The channel mask determines which channels of the image will be included in the histogram count. For a given pixel, the level measured by the histogram will be the mean of the channels included by the mask. The mask value is a bitwise-or of some combination of RED, GREEN, BLUE, and ALPHA. At least one channel must be specified by the mask.
        Parameters:
        mask - the bitwise-or of the channel masks for the channels to include
      • measure

        public void measure​(java.awt.image.BufferedImage image)
        Measures the samples in an image and incorporates them into the histogram statistics. All channels indicated by the channelMask will be averaged together to determine the histogram bucket.
        Parameters:
        image - the image to analyze
      • measure

        public void measure​(int[] image)
        Measures the samples in an array of pixel data and incorporates them into the histogram statistics. All channels indicated by the channelMask will be averaged together to determine the histogram bucket.
        Parameters:
        image - the image to analyze
      • reset

        public void reset()
        Resets the histogram statistics.
      • getCountForLevel

        public int getCountForLevel​(int level)
        Returns the number of samples counted for the given level.
        Parameters:
        level - the level to return the histogram count for (0-255)
        Returns:
        the number of samples counted at this level
      • getRelativeCountForLevel

        public double getRelativeCountForLevel​(int level)
        Returns the relative number of samples counted for the given level.
        Parameters:
        level - the level to return the histogram count for (0-255)
        Returns:
        the number of samples counted at this level over the total samples counted, from 0 to 1
      • getMaximumCount

        public int getMaximumCount()
        Returns the maximum count for any level.
        Returns:
        the largest count value
      • getSampleCount

        public int getSampleCount()
        Returns the total number of samples that have been measured.
        Returns:
        the number of pixels processed
      • createHistogramImage

        public static java.awt.image.BufferedImage createHistogramImage​(java.awt.image.BufferedImage src)
        A utility method that creates an image of the histogram for an image, averaging the red, green, and blue channels together.
        Parameters:
        src - the image to analyze
        Returns:
        a simple graph of the image's histogram
      • createRGBHistogramImage

        public static java.awt.image.BufferedImage createRGBHistogramImage​(java.awt.image.BufferedImage src)
        A utility method that creates an image of the histogram for an image, showing separate red, green, and blue channels.
        Parameters:
        src - the image to analyze
        Returns:
        a simple graph of the image's histogram