Ask Your Question

Counting the number of colours in an image

asked 2012-10-09 12:42:06 -0600

imran gravatar image

Can someone guide me on a way to count the number of colours in an image? My idea is to take an RGB image, convert it to HSV and then split the channels so I only have the Hue channel. Then within the Hue channel, I would like to know if there are for example 50 pixels that have the value 123 and 25 pixels with the value 160 and so forth. Any guidance please on the methods I can use to do this?

The reason for doing this is, given 2 images A & B. I will have the number of colours of each image, then compare whether the two images share the same number of colours. For example, both images have 50 pixels with the value 123 etc.

Thank you in advance

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-10-09 13:42:12 -0600

Rui Marques gravatar image

From this question posted by you, I can tell that you already know how to convert an image to HSV, right?

You can read this answer on how to split the channels of an image.

And to count pixels you should probably use calcHist.

I am not sure if you really don't know these sources, after all you are not new to this answer site.

edit flag offensive delete link more


I'm not an expert but I know some functions. I've tried reading the API now and saw calcHist, I just wanted to know if there was some other method to do it since I haven't been getting it right using calcHist. Thank you for the guidance.

imran gravatar imageimran ( 2012-10-09 14:49:50 -0600 )edit

Maybe you should be more specific and tell what are your problems with calcHist.

Rui Marques gravatar imageRui Marques ( 2012-10-09 15:02:56 -0600 )edit

Sorry for not being specific in my question, I should've rephrased it better.. I was hoping there was some method other than calcHist to count the number of colours. calcHist however seems to be working now. Do you perhaps know which is the best comparision method to use when comparing 2 histograms? (Correlation, Chi-Square, Intersection or Bhattacharyya distance). I should probably make a new question for this.

imran gravatar imageimran ( 2012-10-09 15:49:37 -0600 )edit

I do not know which one is the best method. Yes, that would be a nice new question :)

Rui Marques gravatar imageRui Marques ( 2012-10-10 04:23:45 -0600 )edit

Question Tools

1 follower


Asked: 2012-10-09 12:42:06 -0600

Seen: 9,922 times

Last updated: Oct 09 '12