Ask Your Question

# Counting the number of colours in an 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 close merge delete

## 1 answer

Sort by ยป oldest newest most voted

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.

more

## Comments

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.

( 2012-10-09 14:49:50 -0500 )edit

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

( 2012-10-09 15:02:56 -0500 )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.

( 2012-10-09 15:49:37 -0500 )edit

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

( 2012-10-10 04:23:45 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 5,074 times

Last updated: Oct 09 '12