Here is something you can do:
1st) Calculate the joint histogram. use calcHist
with the 3-channel image, instead of calculating one independent histogram for each channel.
2nd) look for the maximum in the joint histogram, just as you did with the channel histograms, using minMaxLoc
.
Now, it would be wonderful if we knew what color is assigned to each bin in the joint histogram, but this is not straightforward. You can either look at the OpenCV implementation, or ask someone about it ..or try the following:
3rd) Set all the joint histogram values to 0, except the value corresponding to the maximum you just found. Set the value of this bin to the maximum value (i.e 1.0 if this is a normalized histogram). Then do a backprojection of the histogram to the original image (use calcBackProject for that). The pixels with the most-frequent color (the one you are looking for) should be highlighted in the backprojection image.
Hope that it helps!