A=imread('MRI.jpeg');%A=double(A(:,:,1));
%A=[175 172;169 170];

%max=0;min=0;
maximum=max(max(A));
minimum=min(min(A));
[rm,cm]=size(A);
%max=255;min=0;
CM=zeros(maximum-minimum+1);
for i=1:rm
    for j=1:cm-1
      %  if A & B>0
      
            x=A(i,j);
            y=A(i,j+1);
            CM(x-minimum+1,y-minimum+1)=CM(x-minimum+1,y-minimum+1)+1;
            CM(y-minimum+1,x-minimum+1)=CM(y-minimum+1,x-minimum+1)+1;
            % end        
    end
end
%CM2=CM;
%CM=CM2/(sum(sum(CM2)));
CM=CM/(sum(sum(CM)));
%Image A gives the rows for that reason rows have to be summed in px
px=sum(CM');
py=sum(CM);
%And now the Entropies Hx and Hy!
Hx=0;Hy=0;
for k=1:(maximum-minimum+1)
		if  px(k)>0	
            Hx=Hx-px(k)*log2(px(k));
        end
		if  py(k)>0	
            Hy=Hy-py(k)*log2(py(k));
        end    
    end
    
%And the joint entropy Hxy
i=0;j=0;Hxy=0;Hom=0;Contr=0;
 for i=1:(maximum-minimum+1)
	for j=1:(maximum-minimum+1),
		if  CM(i,j)>0 
			%Hxy=Hxy-(1000*CM(k,l))*log10( 1000*CM(k,l) );
            Hxy=Hxy-(CM(i,j))*log2(CM(i,j));
            Hom=Hom+CM(i,j)/(1+abs(i-j));
            Contr=Contr+((i-j)^2)*CM(i,j);
            
        end
    end
end

%The mutual information is given according to the following formulae

 
MI = Hx + Hy - Hxy;
%-----------------------------------------------------------------------------
for k=1:(maximum-minimum+1)
	for l=1:(maximum-minimum+1),
		if  CM(k,l)>0 
            CCM(k,l)=-log2(CM(k,l));
        end
    end
end
    %surfl(CCM);shading interp; colormap bone
    figure, imagesc(CCM)
%-----------------------------------------------------------------------------
%fprintf('Hx=%f\tHy=%f\tHxy=%f\tMi=%f\t%f\t%f\t%f\n\n',Hx, Hy, Hxy, MI, 2*MI/Hxy,Hxy-MI,(Hx+Hy)/Hxy);
%fprintf('%f\t%f\t%f\t%f\t%f\t%f\t%f\n',Hx, Hy, Hxy, MI, 2*MI/Hxy,Hxy-MI,(Hx+Hy)/Hxy);
fprintf('%f\t%f\t%f\t%f\n',Hxy,Hom,Contr, MI);

 