I'm trying to compute the most common color in an image. Using calcHist function, I understand I can get an histogram whose bins in a single channel are the pixel value, and the value of each bin is how many times is that value present in the image.
Also, it is easy to get the max value of the histogram (that is a double) and its position using minMaxLoc function.
But what I want is the whole color (the Vec3b object with the value of each channel).
Here's what I have so far:
split(image, lab_channels);
histSize = 256;
range[0] = 0;
range[1] = 256;
const float *lRange = {range};
calcHist( &lab_channels[0], 1, 0, Mat(),
l_hist, 1, &histSize, &lRange,
true, false );
histSize = 256;
range[0] = 0;
range[1] = 256;
const float *aRange = {range};
calcHist( &lab_channels[1], 1, 0, Mat(),
a_hist, 1, &histSize, &aRange,
true,
false );
histSize = 256;
range[0] = 0;
range[1] = 256;
const float *bRange = {range};
calcHist( &lab_channels[2], 1, 0, Mat(),
b_hist, 1, &histSize, &bRange, t
rue, false );
minMaxLoc(l_hist, 0, 0, 0, &maxPos);
result[0] = maxPos.y;
minMaxLoc(a_hist, 0, 0, 0, &maxPos);
result[1] = maxPos.y;
minMaxLoc(b_hist, 0, 0, 0, &maxPos);
result[2] = maxPos.y;
where result is my Vec3b vector with the most common color.
This code works fine (it does retrieve a color), but the problem is that it gets the most common color separately from each channel and finally it's not necessarely the most common color in the combined channels.
So, if I do this procedure, but for the 3 channels (like it's shown in opencv documentation), how can I get this Vec3b vector from the resulting histogram? or at least another data type I could cast or transform to it.