My solution to this issue was to write a bunch of code to handle the behavior of the average calculation at the 0 and 360 points. It was a pain in the neck, and I probably have it wrong :(.
Today I retook the issue and found various ways of doing the average calculation with less code and more math. This stack overflow link has really cool links and ideas. I really like the following algorithm to handle the calculation:
x = y = 0 foreach angle { x += cos(angle) y += sin(angle) } average_angle = atan2(y, x)There is also a link on wikipedia briefly explaining stuff about means of circular quantities. Here is a paper on google docs that also talks about this issue.
I unfortunately did not find a way to do it with opencv. Which is strange given that you might want the mean of an HSV image. I guess the way to go is to convert it to RGB, calculate the mean and then try to use the RGB->HSV equations to find the Hue mean. However, this seems just plain wrong. Additionally the cv::mean functions do not provide (according to the documentation) a facility to differentiate between a regular Cartesian mean and an angular mean.