# how to generate a color histogram by concatenating the higher order two bits of each color component

1 view (last 30 days)
Yuzhen Lu on 9 Oct 2016
Answered: Walter Roberson on 9 Oct 2016
I want to use matlab to generate a color histogram. The standard way to do that is to concatenate the higher order two bits for each of the Red (R), Green (G) and Blue (B) values in the RGB space, which forms a 64-bin histogram. I am not quite clear about the concatenating process.
It is easy to generate a histogram for each color channel, but how to concatenate three channels to form one 1-D histogram. In particular, what does the higher order two bits mean? how does that end up with a 64-bin histogram?
Massimo Zanetti on 9 Oct 2016
What is the reference where you get this "2 higher order bits" method?
Another question, what is depth of you color channels? 8-bit? 16-bit?

Guillaume on 9 Oct 2016
I'm not sure where you've seen this standard way. I've never heard of it.
In any case, if I understood correctly:
highbits = idivide(img, 64); %only keep the two high bits of each image. Assumes uint8 image
%in R2016b only:
groupedbits = sum(highbits .* permute(uint8([16 4 1]), [1 3 2]), 3);
%in earlier versions:
groupedbits = sum(bsxfun(@times, highbits, permute(uint8([16 4 1]), [1 3 2])), 3);
You can then build your histogram any way you want
histogram(groupedbits, 64, 'BinMethod', 'integers')
Yuzhen Lu on 9 Oct 2016
The method is mentioned in papers you can find at: A Histogramm with Perceptually Smooth Color Transition for Image Retrieval http://scholar.google.com/scholar?hl=en&q=A+Histogram+with+Perceptually+Smooth+Color+Transition+for+Image+Retrieval&btnG=&as_sdt=1%2C23&as_sdtp=

Image Analyst on 9 Oct 2016
Why can't you just concatenate the counts from each color channel together?
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
countsR = imhist(redChannel, 21);
countsG = imhist(greenChannel, 21);
countsB = imhist(blueChannel, 21);
allThree = [countsR; countsG; countsB]'

Walter Roberson on 9 Oct 2016
highred = uint8(floor( double(rgbImage(:, :, 1)) / 2^6));
highgreen = uint8(floor( double(rgbImage(:, :, 2)) / 2^6));
highblue = uint8(floor( double(rgbImage(:, :, 3)) / 2^6));
output = highred * 2^4 + highgreen * 2^2 + highblue;

### Categories

Find more on Histograms in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!