简体   繁体   中英

Not sure how the hist function in MATLAB works

I am not very sure how the hist function in MATLAB works. I seem to have few problems with it.

Bascially, in the code below, i am trying to run the rotation invariant Uniform Local Binary Pattern(LBP) code. I have no problem with the LBP code but the problem is with hist function(indicated in the code below).

The problem is that the range i should get is from 0:9 but when i apply the histogram function i get values greater than 9 such as 35, 27 and even values such as 178114.Not very sure how to correct it.

I2 = imread('test.png');
RIUniformHist=[];
m=size(I2,1);
n=size(I2,2);
for i=1:10:m
    for j=1:10:n
        for k=i+1:i+8
           for l=j+1:j+8
             J0=I2(k,l);
             I3(k-1,l-1)=I2(k-1,l-1)>J0;
             I3(k-1,l)=I2(k-1,l)>J0;
             I3(k-1,l+1)=I2(k-1,l+1)>J0; 
             I3(k,l+1)=I2(k,l+1)>J0;
             I3(k+1,l+1)=I2(k+1,l+1)>J0; 
             I3(k+1,l)=I2(k+1,l)>J0; 
             I3(k+1,l-1)=I2(k+1,l-1)>J0; 
             I3(k,l-1)=I2(k,l-1)>J0;
             LBP=I3(k-1,l-1)*2^7+I3(k-1,l)*2^6+I3(k-1,l+1)*2^5+I3(k,l+1)*2^4+I3(k+1,l+1)*2^3+I3(k+1,l)*2^2+I3(k+1,l-1)*2^1+I3(k,l-1)*2^0;
             bits = bitand(LBP, 2.^(7:-1:0))>0;
             if nnz(diff(bits([1:end, 1]))) <= 2
                RIULBP(k,l)=abs(I3(k-1,l-1)-I3(k-1,l))+ abs(I3(k-1,l)-I3(k-1,l+1))+ abs(I3(k-1,l+1)-I3(k,l+1))+ abs(I3(k,l+1)-I3(k+1,l+1))+abs(I3(k+1,l+1)-I3(k+1,l))+abs(I3(k+1,l)-I3(k+1,l-1))+abs(I3(k+1,l-1)-I3(k,l-1));
             else
                RIULBP(k,l)=9;
             end
           end
        end
        RIULBP=uint8(RIULBP);
        RIULBPv=reshape(RIULBP,1,size(RIULBP,1)*size(RIULBP,2));   
        RIUHist=hist(RIULBPv,0:9); % problem
        RIUniformHist = [RIUniformHist RIUHist];
    end
end

The vector returned by

RIUHist=hist(data, bins)

is the count of how many elements of data are nearest the point identified by the bins vector. So if you have a value of 178114 , that juts means that there were 178114 elements of data that were nearest to the matching index in bins .

You can use

[RIUHist, binsOut] = hist(data)

to let Matlab choose the bins (I believe it uses 20 bins) or

[RIUHist, binsOut] = hist(data, binCount)

To let Matlab choose the bins, but force a certain number of bins (I often use 100 or 200).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM