How can I adjust the number of bins in my histogram created by IMHIST?

12 views (last 30 days)
I would like the IMHIST command to create histograms for images which use non-standard bit-depths. For example, my images have a bit-depth of 12. Since the image is stored as 16-bit this means that IMHIST assumes a total x-axis length of 2^16 when I would like it to be 2^12. I would also like the colorbar to be represented correctly in this case.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to control the bit-depth used by the histogram created by IMHIST is not available in MATLAB.
You may be able to workaround this by choosing a larger number of bins proportional to the difference in dynamic range between your data and the data type and then discarding the bins with centers above your maximum data range. For example, if you require 100 bins for the range of your data, call IMHIST with 1600 bins since the top 4 bits are never used and consequently there are only 1/16th as many non-zero bins. The multiplier will be 2 raised to the difference in the number of bits between the data type and your image bit depth (eg. if the bit depth is 13-bits, you would need to specify 800 bins). Then, after the counts are obtained retain only the first 1/16th.
bits = 12;
sampleimage = uint16(2^bits*rand(256));
% Note image has a dynamic range of 0 - 2^12-1 (4 bits are empty)
Nbins = 100;
% Since only 1/16th of the maximum uint16 range is being used, we need to
% adjust the bin size to capture 100 bins of valid data
Nbins_adj = Nbins * 2^(16-bits); % Since only 12 bits are used.
[counts,x] = imhist(sampleimage, Nbins_adj);
subplot(2,1,1);
plot(x,counts);
% As expected, only the first 1/16 are non-empty
x = x(1:Nbins+1);
counts = counts(1:Nbins+1);
subplot(2,1,2);
bar(x,counts);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!