0

I'm looking for a way to get a complete list of all the RGB values for each pixel in a given image using OpenCV, now i call this "color quantization".

The problem is that according to what I have found online, at least at this point, this "color quantization" thing is about histograms or "color reduction" or similar discrete computation solutions.

Since I know what I want and the "internet" seems to have a different opinion about what this words mean, I was wondering: maybe there is not a real solution for this ? a workable way or a working algorithm in the OpenCV lib.

  • doesn't each pixel only have a single R,G, and B value associated with it? – Boyko Perfanov Mar 06 '13 at 10:33
  • @perfanoff keep in mind that i want a list of RGB value with their associated percentage, not just 1 pixel value. – user2128456 Mar 06 '13 at 10:39
  • Possible duplicate: [Accessing certain pixel RGB value in openCV](http://stackoverflow.com/questions/8932893/accessing-certain-pixel-rgb-value-in-opencv) – volting Mar 06 '13 at 10:41
  • @volting accessing pixel by pixel it's not exactly the way i pictured this, also is basically something that would require an appropriate data-structure on my side, I'm not using any particular OpenCV method with that kind of approach. I was hoping in a really efficient algorithm on the OpenCV side. – user2128456 Mar 06 '13 at 10:49
  • So you just want a list of all the RGB values as percentages correct ? – volting Mar 06 '13 at 11:22
  • @volting yes, kind of a percentage about how many time that RGB value/triplet is present in the given image. – user2128456 Mar 06 '13 at 11:27
  • You need to look at OpenCVs histogram functions then.. [Histograms](http://docs.opencv.org/modules/imgproc/doc/histograms.html) and the [Histogram Calculation Tutorial](http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html) – volting Mar 06 '13 at 11:35

2 Answers2

2

Generally speaking, quantization is an operation that takes an input signal with real (mathematical) values to a set of discrete values. A possible algorithm to implement this process is to compute the histogram of the data, then retaining the n values that correspond to the n bins of the histogram with the higher population.

What you are trying to do would be called maybe color listing. If you ar eworking with 8 bits quantized images (type CV_8UC3), my guess is that you do what you desire by taking the histogram of the input image (bin width equal to 1) then searching the result for non-empty bins.

sansuiso
  • 9,259
  • 1
  • 40
  • 58
1

Color quantization is the conversion of infinite natural colors in the finite digital color space. Anyway to create a full color 'histogram' you can use opencv's sparse matrix implementation and write your own function to compute it. Of course you have to access the pixels one by one, if you have no other structural or continuity information about the image.

sfotiadis
  • 959
  • 10
  • 24
  • does OpenCV is able to manage and trigger different actions based on the color model adopted by the given image ? For example detecting if the image is using sRGB or Adobe RGB or a CMYK ? – user2128456 Mar 06 '13 at 11:48
  • By default, no. But you should be able to read that from the EXIF of the file and act accordingly. Btw opencv uses BGR format. See [here](http://stackoverflow.com/questions/2122706/detect-color-space-with-opencv) – sfotiadis Mar 06 '13 at 11:58
  • ok, thanks, last thing, histograms are only graphical or I can also get a numerical output like an std::vector or an std::map ? – user2128456 Mar 06 '13 at 12:02
  • to draw the histogram you need the data in numerical form beforehand. it could be as simple as an array if the mapping is known eg 0-255 gray values -> array of 256x1 – sfotiadis Mar 06 '13 at 13:32
  • yes but i was wondering how and if I can get an std-compatible numerical representation for that, in other words if there is an OpenCV method that does that or if the OpenCV data-structure are granted to be compatible with the std ones. – user2128456 Mar 06 '13 at 13:34
  • i think you can do it if the mat is Nx1. see [here](http://stackoverflow.com/questions/9790124/converting-a-row-of-cvmat-to-stdvector) – sfotiadis Mar 06 '13 at 13:42