Clear Filters
Clear Filters

Difference between hist and imhist

6 views (last 30 days)
Ely Raz
Ely Raz on 9 Aug 2014
Commented: DGM on 19 May 2024
Hi,
I have a fundamental question about the hist and imhist and an explanation will be great as I am a bit lost.
I am running this script:
I = imread('pout.tif');
figure;
x=imhist(I)
imhist(I)
figure(2);
hist(x)
and when I compare the two histograms they do not look alike, why?
How can I save the imhist data so that the hist will show the same plot?
Thanks

Accepted Answer

Image Analyst
Image Analyst on 9 Aug 2014
For a gray scale image imhist() will give 256 bins. hist() only gives 10 by default. But you did not use hist() to take the histogram of the image - the badly-named "I". You used it to take a histogram of the histogram counts, x - a pretty weird/strange thing to do.
  6 Comments
Nagabhushan SN
Nagabhushan SN on 27 Aug 2018
Hi @ImageAnalyst, you've mentioned that "IMPORTANT WARNING: counts(1) is not for gray level 0!!!" So do you mean that hist doesn't give histogram in order of intensity values? So, histogram plots by imhist(grayImage) and hist(double(grayImage(:)), 256) will be different?
DGM
DGM on 19 May 2024
What that comment means is that when only the number of bins are specified, their location is entirely up to hist() to decide. Where does hist() put the bins? It arranges the bins such that they span the range of data in the input array. To be more specific, the outer edges of the end bins lie on the extrema of the input data, which will vary from image to image.
On the other hand, imhist() centers the end bins on the values which represent black and white for the numeric class of the input data. The bin locations are strictly a function of the numeric class, not the range of the data. For images of the same class, two count vectors of equal length represent the same ranges of gray levels.
In order to make hist() (or histc() or the newer histcounts()) have similar behavior, you need to specify the bin locations. Pretending we're still in <R2014b:
% we have some image which is properly-scaled for its class
inpict = imread('cameraman.tif');
inpict = im2double(inpict); % unit-scale
% the image data doesn't extend completely to [0 1]
[min(inpict(:)) max(inpict(:))]
ans = 1x2
0.0275 0.9922
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% number of bins
N = 7;
% do it with imhist()
subplot(3,1,1);
[counts1 centers1] = imhist(inpict,N); % get the counts/centers
bar(centers1,counts1,1);
% hist() will put the bins whereever it wants
% unless you actually tell it where they go
subplot(3,1,2);
[counts2 centers2] = hist(double(inpict(:)),N); %#ok<HIST>
bar(centers2,counts2,1);
% replicate imhist()'s centered bins
xrange = getrangefromclass(inpict);
os = 0.5*diff(xrange)/(N-1);
breakpoints = linspace(xrange(1)-os,xrange(2)+os,N+1).';
centers3 = linspace(xrange(1),xrange(2),N).';
counts3 = histc(inpict(:),breakpoints); %#ok<HISTC>
counts3(end-1) = sum(counts3((end-1):end));
counts3 = counts3(1:end-1);
subplot(3,1,3);
bar(centers3,counts3,1);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!