Code covered by the BSD License  

Highlights from
Joint entropy and histogram calculation for 3D+ images

from Joint entropy and histogram calculation for 3D+ images by ja
Reasonably fast. Build for any number of dimensions.

ent(J,K,t,xyz)
function entr = ent(J,K,t,xyz)
%%this can do a joint histogram or joint entropy (or both).
%%to enable the join histogram, uncomment all command lines
%%and add "Hist" to the returned values
%%for ease of this process, all comments have two "%%"
%%all omited code has one "%".
%%also, integer values are assumed

%%initialize histogram array
%%assumes 256x256
%%any modifications to this dimension must be made below as well
%Hist = zeros(256,256);

%%size of J. For speed, it cuts a little bit of time to allocate these
%%outside the function, and pass it through
%%this assumes a 3D "image," but can work for any number of dimensions
%[x y z] = size(J);
%%total number of elements
%xyz = x*y*z
%%a 1D array from one to the number of elements
%t = 1:xyz

%%get all values of J + 1
%%this will correspond to the "x" value is the histogram
xx = J(t)+1;
%%corresponds to the "y" value in the histogram
%%multiplied by 256 because 256x256 is assumed
%%change it here if it is changed above
yy = K(t);
yy = yy*256;

%%adding these two correspond to the single-variable location in J
xx = xx + yy;
%%sort and shift
%%yy is now used for the shifted values, since we don't need it as an index
%%anymore
xx = sort(xx);
yy(1:xyz-1) = xx(2:xyz);

%%if we subtract, zz will now correspond to any location where the
%%single-variable index changes...
zz = xx - yy;
zz(xyz) = 1;
%%...the location of which just so happens to correspond to the 
%%number of counts to be at that location...
zz = find(zz ~=0);
%sm = squeeze(zz(length(zz)));
%yy = xx(zz);
%%...after we subtract each previous value.
zz(2:length(zz)) = zz(2:length(zz))-zz(1:length(zz)-1);
%%histogram is normalized
%%remove "/sm" to 'un-normalize' it..
%Hist(yy) = zz/sm;

xx = zz/xyz;
entr = -sum(xx.*log2(xx));

end

Contact us