imhist and histogram giving different results

9 views (last 30 days)
I'm trying to obtain a histogram of a 1-D signal (type double in the range 0-1). I've tried using two functions - imhist and histogram (also histcounts, which produces the same results as histogram) - and they gave me different results, even though the number of bins was the same in both cases.
Could someone explain to me, which function is the "proper" one to calculate histogram of a signal? What is the difference between them?
My simplified code is below. Result of imhist is plotted directly with result of histogram in figure 1, and in figure 2 to plot both results I used bar function so that the bins from both results are overlapping.You can see that these results are not the same.
I = rand(1e4,1);
[p,lp] = imhist(I(:),256);
figure(1); clf;
ph = histogram(I(:),256); hold on; bar(lp,p); hold off
figure(2); clf;
bar(ph.Values); hold on; bar(p); hold off

Accepted Answer

dpb
dpb on 7 Sep 2022
For <imhist>, the n bins of the histogram are each half-open intervals computed for a double as you've passed as
(p1.5)/(n1) x <(p−0.5)/(n−1)
<histcounts> sorts X into bins such that X(i)is in the kth bin if edges(k)X(i) < edges(k+1). The last bin also includes the right bin edge, so that it contains X(i) if edges(end-1)X(i)edges(end).
Neither is unquestionably "right" nor "wrong", just "different".

More Answers (1)

Steven Lord
Steven Lord on 7 Sep 2022
I suspect that imhist and histogram are just using different algorithms to select the bin edges in the case where you only specify the number of bins. What do you see if you specify the bin edges for histogram using the bin edges computed by imhist? The second output from imhist contains the bin locations. You can pass these in as the second input for histogram. Or you could compare the second output from imhist with the BinEdges property of the object returned by histogram (or the second output argument from histcounts.)
  2 Comments
Michal Dudek
Michal Dudek on 8 Sep 2022
Thank you @dpb and @Steven Lord for your comments and suggestions. With them I was able to better understand the behavior of the three aforementioned functions, but I still don't quite understand how the histogram / histcounts functions calculate bin edges - especially the upper / last one. More of it below.
  • The imhist function returns bin locations, which, is not the same as bin edges obtained from histogram or specified in its input. If I provide the bin locations to the histogram function, I get histogram with N-1 bins (so in this case 255 bins).
  • The way imhist function calculates the histogram is, in my opinion, flawed, as the first and last bins practicaly have width of only half of the width of other bins, since the signal values are in the range [0-1] and their edges are below 0 and above 1, respectively.
I = rand(1e4,1);
[p,lp] = imhist(I(:),256);
pp = 1; [pp-1.5, pp-0.5]./(256-1) % condition (n−1.5)/(N−1) ≤ x <(n−0.5)/(N−1)
ans = 1×2
-0.0020 0.0020
pp = 256; [pp-1.5, pp-0.5]./(256-1) % condition (n−1.5)/(N−1) ≤ x <(n−0.5)/(N−1)
ans = 1×2
0.9980 1.0020
  • In order to get the same results using imhist and histogram functions, one needs to shift the bin locations obtained from imhist by a half width of the bin to obtain bin edges, and then add one more edge above 1 (upper edge):
lp2 = [lp - 0.5/(256-1); (256-0.5)/(256-1)]; % shift bin locations and add one more edge
figure(1); clf;
ph = histogram(I(:),lp2); hold on; bar(lp,p); hold off
figure(2); clf;
bar(ph.Values-p') % difference between results from histogram and imhist
  • The curious thing is that while obtaining bin edges from histogram (or histcount), the first edge starts at 0, but the last one does not end exactly at 1:
[~,phe] = histcounts(I(:),256);
[phe(1:3);phe(end-2:end)]
ans = 2×3
0 0.0039 0.0078 0.9931 0.9970 1.0010
I'll try to look more into this issue.
Bruno Luong
Bruno Luong on 8 Sep 2022
@Michal Dudek "The way imhist function calculates the histogram is, in my opinion, flawed"
Not really IMO, it mainly designed to handle discrete values.
It might be not suitable for continuous value as in your case

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!