Search code examples
matlabhistogram

difference between histcounts and imhist matlab


histcounts and imhist are not returning the same values for counts and bin locations.

x = [0.5000, 0.6429, 0.7143, 0.6429, 0.7857, 0.2857, 0.8571, 0.6429,0, 0.7857, 0.9286, 1.0000, 0.1429, 0.8571, 0.2857, 0.8571, 0.5714, 0.0714];

[c1, l1] = histcounts(x, 6)

c1 =
     3     2     1     4     3     5

l1 =
         0    0.1700    0.3400    0.5100    0.6800    0.8500    1.0200


[c2, l2] = imhist(x, 6)

c2 =
     2     3     0     5     6     2

l2 = 
         0    0.2000    0.4000    0.6000    0.8000    1.0000

What could be the reason for that?


Solution

  • MATLAB

    close all;clear all;clc
    
    nbins=[6 16 26 46]
    
    x = [0.5000, 0.6429, 0.7143, 0.6429, 0.7857, 0.2857, 0.8571, ...
        0.6429,0, 0.7857, 0.9286, 1.0000, 0.1429, 0.8571, 0.2857, 0.8571, 0.5714, 0.0714];
    

    one can take it from one side

    for k=1:1:numel(nbins)
        figure(k);
        ax=gca;hold on;grid on
        [C1, L1] = histcounts(x,nbins(k));
        stem(L1(1:end-1),C1);hold on
        [C2, L2] = imhist(x,nbins(k));
        stem(ax,L2,C2)
    
    end
    

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    or from the other, stem graphs not shown, quite similar to the above ones.

    for k=1:1:numel(nbins)
        figure(k);
        ax=gca;hold on;grid on
        [C1, L1] = histcounts(x,nbins(k));
        stem(L1(2:end),C1);hold on
        [C2, L2] = imhist(x,nbins(k));
        stem(ax,L2,C2)
    
    end
    

    The point : imhist is a command for images and it applies an offset to all histogram bin locations depending upon the type of image fed in.

    imhist doesn't have a cut-off for tiny images, so the sequence x is assumed as image, which it is not.

    Read imhist details here.

    In particular this table shows such offset

    enter image description here