Class ColorHistogram

java.lang.Object
imagingbook.common.color.statistics.ColorHistogram

public class ColorHistogram extends Object
This class calculates a color histogram of a set of colors (i.e., a color image). Only the unique colors are accounted for. Colors are supplied as ARGB-encoded integers (A = alpha values being ignored). Colors are internally sorted by their frequency (in descending order). Used mainly for color quantization.
Version:
2022/11/05
  • Constructor Summary

    Constructors
    Constructor
    Description
    ColorHistogram(int[] pixels)
    Creates a color histogram instance from the supplied sequence of color pixel values (assumed to be aRGB-encoded integers).
    ColorHistogram(int[] pixelsOrig, boolean sortByFrequency)
    Creates a color histogram instance from the supplied sequence of color pixel values (assumed to be ARGB-encoded integers).
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    getColor(int index)
    Returns the unique color with the given index.
    int[]
    Returns an array with all (distinct) colors of this histogram as sRGB int-encoded values.
    int[]
    Returns an array with the frequencies of all (distinct) colors of this histogram.
    int
    getFrequency(int index)
    Returns the frequency of the unique color with the given index.
    int
    Returns the number of distinct colors in this color histogram.
    void
    Lists the unique colors to System.out (intended for debugging only).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ColorHistogram

      public ColorHistogram(int[] pixels)
      Creates a color histogram instance from the supplied sequence of color pixel values (assumed to be aRGB-encoded integers). The original pixel values are not modified.
      Parameters:
      pixels - aRGB-encoded integer pixel data
    • ColorHistogram

      public ColorHistogram(int[] pixelsOrig, boolean sortByFrequency)
      Creates a color histogram instance from the supplied sequence of color pixel values (assumed to be ARGB-encoded integers). The original pixel values are not modified.
      Parameters:
      pixelsOrig - original pixel values (not modified)
      sortByFrequency - set true to sort the final colors by descending frequency
  • Method Details

    • getNumberOfColors

      public int getNumberOfColors()
      Returns the number of distinct colors in this color histogram.
      Returns:
      the number of distinct colors.
    • getColors

      public int[] getColors()
      Returns an array with all (distinct) colors of this histogram as sRGB int-encoded values. Values are in no particular order but in the same order as the array returned by getFrequencies().
      Returns:
      an array of all distinct colors
    • getFrequencies

      public int[] getFrequencies()
      Returns an array with the frequencies of all (distinct) colors of this histogram. Values are in no particular order but in the same order as the array returned by getColors().
      Returns:
      an array of all distinct color frequencies
    • getColor

      public int getColor(int index)
      Returns the unique color with the given index. Colors are sorted by (decreasing) frequency.
      Parameters:
      index - The color index.
      Returns:
      The color, encoded as an ARGB integer (A is zero).
    • getFrequency

      public int getFrequency(int index)
      Returns the frequency of the unique color with the given index. Colors are sorted by (decreasing) frequency.
      Parameters:
      index - The color index.
      Returns:
      The frequency of the color.
    • listUniqueColors

      public void listUniqueColors()
      Lists the unique colors to System.out (intended for debugging only).