How to count average Hue of all pixels except the black one?

5 views (last 30 days)
I have this HSV picture i already processed. my question is, how to get the average level of all colorful pixels? i dont want the black pixels being counted. just the pixels with color. any idea how?

Accepted Answer

DGM
DGM on 27 Mar 2021
Edited: DGM on 27 Mar 2021
Your problem statement is ambiguous; "just the colorful pixels" isn't the same as "non-black pixels". You'll have to decide what you actually want, but I'm going to assume "non-black pixels" for this example.
% read the image
inpict=im2double(imread('image.png'));
% i'm going to assume you don't want the white border either
% since the angles there are likely meaningless
% if that's not the case, just ignore this.
inpict=cropborder(inpict,[NaN NaN NaN NaN]);
% cropborder is in the MIMT (on the File Exchange)
% convert to some polar model of choice.
% i'll use HSV, since i guess you're using it.
hsvpict=rgb2hsv(inpict);
% pick some threshold value according to your needs
% rgb2hsv gives normalized V, so if you use a different model, this might
% need to be adjusted accordingly.
goodpix=hsvpict(:,:,3)>0.001;
% denormalize hue and extract the values of interest
% denormalizing is only needed here since rgb2hsv gives normalized H
% not all conversion tools do that.
goodhues=hsvpict(:,:,1)*360;
goodhues=goodhues(goodpix);
% average of angles isn't the simple arithmetic mean
averagehue=atan2d(sum(sind(goodhues)),sum(cosd(goodhues)))
That's the general idea. Develop a logical mask to extract the desired pixels from the hue map, and average them.
If instead you prefer to extract "just the colorful pixels", then that would also exclude white and gray pixels. Thresholding chroma may be more appropriate than saturation, so a different model might be simpler to use. If it's decided that thresholding saturation would suffice, HSV may still be one of the worst models to use due to the gross assymetry of its saturation space. Then again, I don't know what suits your technical requirements.
  6 Comments
DGM
DGM on 28 Mar 2021
You could do that if it suits your requirements. The weakness of either method is when the inputs are complementary. If averaging in RGB, you'll end up with a gray value with (essentially) undefined hue. If averaging angles, the angle becomes ambiguous and the formula I gave will give a garbage answer; after all, what angle is halfway between 0 and 180? Is it 90 or 270? So long as all your colors don't average out to a gray (they don't in this case), you should be fine. If you need speed, it might be faster to do it in RGB.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!