How can i average Hue value in image?

8 views (last 30 days)
pp
pp on 4 Jan 2014
Commented: Richard Hartley on 5 Dec 2016
hello,I'm beginner and I try to do this.
First i separate HSV in image and try to find V value having almost the same.
Second average Hue value in that area.
Can you please tell let me know the code to display the Hue.
thanks a lot.

Answers (2)

Richard Hartley
Richard Hartley on 4 Dec 2016
Edited: Image Analyst on 4 Dec 2016
I cannot give Matlab code (someone else may), and I am not sure that I quite understand you pictures. However, for sure, one cannot average Hue just by adding up the numbers and averaging, because the hue represents an angle.
To do it properly, you have to do this (non-Matlab code)
sumsin = 0.0
sumcos = 0.0
for (i=1:n)
sumsin = sumsin + Sin[Hue[i]];
sumcos = sumcos + Cos[Hue[i]];
endfor
averageH = Atan2[sumsin, sumcos];
This is assuming that Hue is given in radians. If it is in degrees (as I believe in MSDN) then you have to make the appropriate corrections.
  2 Comments
Image Analyst
Image Analyst on 4 Dec 2016
Good point Richard. When the hues do not cross the 0/360 red/purple axis, the straight mean and your trig method are the same.
However when the hues cross the axis, your result is still accurate while the straight mean is not.
The magenta * is the straight mean, and the red circle is your method.
By the way, the MATLAB code for your code is this:
sumsin = sum(sind(hue(:))) % Use (:) if you want to be able to handle 2-D hue images.
sumcos = sum(cosd(hue(:)))
averageH = atan2d(sumsin, sumcos) % Compute mean hue angle
Attached below is the m-file I used to create the demo images, in case anyone is interested.
Richard Hartley
Richard Hartley on 5 Dec 2016
Well, if the hue values do not cross the axis, the result of my method and straight averaging may be close, but they will not be identical. Thanks for providing Matlab code.

Sign in to comment.


Image Analyst
Image Analyst on 4 Jan 2014
Create a binary image of the v part, then call mean:
hsv=rgb2hsv(rgbImage);
h = hsv(:,:,1);
s = hsv(:,:,2);
v = hsv(:,:,3);
binaryImage = v > 0.82 & v < 0.90;
meanHue = mean(h(binaryImage));
  4 Comments
pp
pp on 5 Jan 2014
So,if i will analysis other image,how can i know what V value that i will choose.
Image Analyst
Image Analyst on 5 Jan 2014
You can either try to detect the uniform central region automatically based on it's variation, or you can use a circle of known, fixed coordinates. Or you can have the user draw an ellipse using imellipse (like in the attached example). Or you can plot profiles to let the user inspect the values (like you already did) and ask the user for the values via inputdlg(). Which way do you want to do it?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!