function varargout = InfoMutuelle(varargin)
% Calcul de l'information mutuelle entre deux images
% Images de mêmes dimensions XY et de même nombre de bits
% 
% INPUT:
% - image1 P1xQ
% - image1 P2xQ
%
% OUTPUT:
% - matrice numérique : [entropie1, entropie2, information mutuelle]
%
% Plugin for OpenIsaac
% version 1
% © 2011-2018 Sié Ouattara / Alain Clément - Université d'Angers

%---------------------------------------------------------------------------------------------------

% Copyright 2007 - 2018 Université d'Angers - Author: Alain Clément
%
% This program is free software; you can redistribute it and/or modify it under the terms of the GNU
% General Public License as published by the Free Software Foundation; either version 3 of the
% License, or any later version. This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
% PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received
% a copy of the GNU General Public License along with this program; if not, write to the Free
% Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

%---------------------------------------------------------------------------------------------------

% PLUGIN PARAMETERS
if (nargin == 1) && strcmp(varargin{1},'-f')
    % input
	varargout{1} = {'IMG','image1', ...
					'IMG','image2'};
    % output
	varargout{2} = {'DATA','matrice information mutuelle'};
	return
end	

% INPUT
ObjIMG1 = varargin{1};
ObjIMG2 = varargin{2};

%---------------------------------------------------------------------------------------------------

if (IsaacIMG_get(ObjIMG1,'BitClass') ~= IsaacIMG_get(ObjIMG2,'BitClass')) || ...
	~IsaacIMG_compare(ObjIMG1,ObjIMG2,'DimXY')
	error('images incompatibles')
end
% entropie image1
[~,counts] = IsaacModule('IsaacHistN',double(IsaacIMG_img2mat(ObjIMG1)));
p = counts/sum(counts);
entrop1 = -p'*log2(p); % attention opération matricielle : ordre impératif
% entropie image2
[~,counts] = IsaacModule('IsaacHistN',double(IsaacIMG_img2mat(ObjIMG2)));
p = counts/sum(counts);
entrop2 = -p'*log2(p); % attention opération matricielle : ordre impératif
% entropie globale
ObjIMG3 = IsaacIMG_concat(ObjIMG1,ObjIMG2);
clear ObjIMG1 ObjIMG2
[~,counts] = IsaacModule('IsaacHistN',double(IsaacIMG_img2mat(ObjIMG3)));
p = counts/sum(counts);
entrop3 = -p'*log2(p); % attention opération matricielle : ordre impératif
% calcul de l'information mutuelle
infomut=entrop1+entrop2-entrop3;

%---------------------------------------------------------------------------------------------------

% OUTPUT
varargout{1} = IsaacDATA('TABN',[entrop1 entrop2 infomut],'matrice info mutuelle');
