How do I calculate average hue, since the numbers loop around?
I will get silly answers sometimes. For example, in HSB/HSL, the average of hue=10 (slightly orange red) and hue=350 (slightly blue red) would arithmetically be hue=(10+350)/2=180 (cyan).
What if the values were 0° and 180°, then there are two answers: 90° and 270°; the same thing applies whenever the two inputs are opposite, there's two answers and no clear choice.
- The program "Image Color Summarizer" (Linux version) has a file called "peravg.pm" which says: "compute the average and median of a periodic data set, like a set of angles or hues". The main page offers a Windows version and a webpage image upload along with an API.
The author explains:
The colour summarizer will produce descriptive colour statistics for an image. Reported will be the average, median, minimum and maximum of each component of RGB, HSV, LCH and Lab. Average hues are calculated using mean of circular quantities.
Some of the questions the summarizer will answer are:
- what is the average color hue, saturation and value in my image?
- what are the colours that are most representative of the image?
- what is the image's human readable colour description (e.g. dark pure blue)?
- what do the RGB, HSV, LCH and Lab histograms look like?
- what are the RGB, HSV, LCH and Lab pixel values?
The paper: "Perceiving the average hue of color arrays" (May 1 2014), by Jacquelyn Webster, Paul Kay, and Michael A. Webster examines the subject from the viewpoint of an observer's perception of color.