IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Images Discussion :

[Débutante] Mixture gaussien algorithme EM


Sujet :

Images

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Septembre 2007
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 17
    Par défaut [Débutante] Mixture gaussien algorithme EM
    bonjour,

    mon problème est le suivant je suis debutante en MATLAB et je considère l'histogramme de l'image couleur comme une mixture de gaussiennes et je dois détecter les paramètres de ces gauss en utilisant l'algorithme EM( EM_GM) mais je n'arrive pas à le faire

    merci de m'aider
    Fichiers attachés Fichiers attachés

  2. #2
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Sur quel problème exactement bloques-tu ? Quel est le contexte de ton étude ?

    As-tu déjà essayer d'écrire quelques programmes ? Y-a-t-il des erreurs ?

  3. #3
    Membre averti
    Inscrit en
    Septembre 2007
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 17
    Par défaut
    je travail sur la detection d'image et la je suis bloque sur le point de l'application de l'algorithme em pour l'approximation des gaussienne d'un histogramme d'image exemple lenna

  4. #4
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    C'est ce que fait le fichier que tu as attaché, non ?

  5. #5
    Membre averti
    Inscrit en
    Septembre 2007
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 17
    Par défaut
    oui mais je n'arrive pas a l'apliquer

  6. #6
    Membre averti
    Inscrit en
    Septembre 2007
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 17
    Par défaut
    voici un exemple de programme
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    im=imread('autumn.tif'); im=double(im);X=hist(im,); 
    [W,M,V,L] = EM_GM_fast(X,3,[],[],1,[])
    et voila ce que j'ai comme erreur
    Input data must be n x d!/n
    ??? One or more output arguments not assigned during call to 'em_gm_fast'.
    jatends avec impatience votre aide merci
    voila EM_GM_fast
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    function [W,M,V,L] = EM_GM_fast(X,k,ltol,maxiter,pflag,Init)
    % [W,M,V,L] = EM_GM_fast(X,k,ltol,maxiter,pflag,Init) 
    % 
    % EM algorithm for k multidimensional Gaussian mixture estimation
    % (EM_GM_fast is the modified version of EM_GM for speed enchancement.
    %  The functionalities of EM_GM_fast and EM_GM are identical.)
    %
    % Note: EM_GM_fast requires more memory than EM_GM to execute.
    %       If EM_GM_fast does not provide any speed gain or is slower than EM_GM,
    %       more memory is needed or EM_GM should be used instead.
    %
    % Inputs:
    %   X(n,d) - input data, n=number of observations, d=dimension of variable
    %   k - maximum number of Gaussian components allowed
    %   ltol - percentage of the log likelihood difference between 2 iterations ([] for none)
    %   maxiter - maximum number of iteration allowed ([] for none)
    %   pflag - 1 for plotting GM for 1D or 2D cases only, 0 otherwise ([] for none)
    %   Init - structure of initial W, M, V: Init.W, Init.M, Init.V ([] for none)
    %
    % Ouputs:
    %   W(1,k) - estimated weights of GM
    %   M(d,k) - estimated mean vectors of GM
    %   V(d,d,k) - estimated covariance matrices of GM
    %   L - log likelihood of estimates
    %
    % Written by
    %   Patrick P. C. Tsui,
    %   PAMI research group
    %   Department of Electrical and Computer Engineering
    %   University of Waterloo, 
    %   March, 2006
    %
    %   Michael Boedigheimer
    %   Amgen
    %   Dept of Computational Biology
    %   Thousand Oaks CA, 91320
    %   Dec, 2005
    % 
     
    %%%% Validate inputs %%%%
    if nargin <= 1,
        disp('EM_GM must have at least 2 inputs: X,k!/n')
        return
    elseif nargin == 2,
        ltol = 0.1; maxiter = 1000; pflag = 0; Init = [];
        err_X = Verify_X(X);
        err_k = Verify_k(k);
        if err_X | err_k, return; end
    elseif nargin == 3,
        maxiter = 1000; pflag = 0; Init = [];
        err_X = Verify_X(X);
        err_k = Verify_k(k);
        [ltol,err_ltol] = Verify_ltol(ltol);    
        if err_X | err_k | err_ltol, return; end
    elseif nargin == 4,
        pflag = 0;  Init = [];
        err_X = Verify_X(X);
        err_k = Verify_k(k);
        [ltol,err_ltol] = Verify_ltol(ltol);    
        [maxiter,err_maxiter] = Verify_maxiter(maxiter);
        if err_X | err_k | err_ltol | err_maxiter, return; end
    elseif nargin == 5,
         Init = [];
        err_X = Verify_X(X);
        err_k = Verify_k(k);
        [ltol,err_ltol] = Verify_ltol(ltol);    
        [maxiter,err_maxiter] = Verify_maxiter(maxiter);
        [pflag,err_pflag] = Verify_pflag(pflag);
        if err_X | err_k | err_ltol | err_maxiter | err_pflag, return; end
    elseif nargin == 6,
        err_X = Verify_X(X);
        err_k = Verify_k(k);
        [ltol,err_ltol] = Verify_ltol(ltol);    
        [maxiter,err_maxiter] = Verify_maxiter(maxiter);
        [pflag,err_pflag] = Verify_pflag(pflag);
        [Init,err_Init]=Verify_Init(Init);
        if err_X | err_k | err_ltol | err_maxiter | err_pflag | err_Init, return; end
    else
        disp('EM_GM must have 2 to 6 inputs!');
        return
    end
     
    %%%% Initialize W, M, V,L %%%%
    t = cputime;
    if isempty(Init),  
        [W,M,V] = Init_EM(X,k); L = 0;    
    else
        W = Init.W;
        M = Init.M;
        V = Init.V;
    end
    Ln = Likelihood(X,k,W,M,V); % Initialize log likelihood
    Lo = 2*Ln;
     
    %%%% EM algorithm %%%%
    niter = 0;
    while (abs(100*(Ln-Lo)/Lo)>ltol) & (niter<=maxiter),
        E = Expectation(X,k,W,M,V);     % E-step
        [W,M,V] = Maximization(X,k,E);  % M-step
        Lo = Ln;
        Ln = Likelihood(X,k,W,M,V);
        niter = niter + 1;
    end 
    L = Ln;
     
    %%%% Plot 1D or 2D %%%%
    if pflag==1,
        [n,d] = size(X);
        if d>2,
            disp('Can only plot 1 or 2 dimensional applications!/n');
        else
            Plot_GM(X,k,W,M,V);
        end
        elapsed_time = sprintf('CPU time used for EM_GM: %5.2fs',cputime-t);
        disp(elapsed_time); 
        disp(sprintf('Number of iterations: %d',niter-1));
    end
    %%%%%%%%%%%%%%%%%%%%%%
    %%%% End of EM_GM %%%%
    %%%%%%%%%%%%%%%%%%%%%%
     
    function E = Expectation(X,k,W,M,V)
    % This function is the modification of 'Expectation' in EM_GM made by 
    % Mr. Michael Boedigheimer to enchance computational speed.
    % Note: this modification requires more memory to execute.
    %       If EM_GM_fast does not provide any speed gain or is slower than EM_GM,
    %       more memory is needed or EM_GM should be used instead.
    [n,d] = size(X);
    E = zeros(n,k);
    for j = 1:k,
        if V(:,:,j)==zeros(d,d), V(:,:,j)=ones(d,d)*eps; end
        E(:,j) = W(j).*mvnpdf( X, M(:,j)', V(:,:,j) );
    end
    total = repmat(sum(E,2),1,j);
    E = E./total;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Expectation %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    function [W,M,V] = Maximization(X,k,E)
    % This function is the modification of 'Maximization' in EM_GM made by 
    % Mr. Michael Boedigheimer to enchance computational speed.
    % Note: this modification requires more memory to execute.
    %       If EM_GM_fast does not provide any speed gain or is slower than EM_GM,
    %       more memory is needed or EM_GM should be used instead.
    [n,d] = size(X);
    W = sum(E);
    M = X'*E./repmat(W,d,1);
    for i=1:k,
        dXM = X - repmat(M(:,i)',n,1);
        Wsp = spdiags(E(:,i),0,n,n);
        V(:,:,i) = dXM'*Wsp*dXM/W(i);
    end
    W = W/n;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Maximization %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    function L = Likelihood(X,k,W,M,V)
    % Compute L based on K. V. Mardia, "Multivariate Analysis", Academic Press, 1979, PP. 96-97
    % to enchance computational speed
    [n,d] = size(X);
    U = mean(X)';
    S = cov(X);
    L = 0;
    for i=1:k,
        iV = inv(V(:,:,i));
        L = L + W(i)*(-0.5*n*log(det(2*pi*V(:,:,i))) ...
            -0.5*(n-1)*(trace(iV*S)+(U-M(:,i))'*iV*(U-M(:,i))));
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Likelihood %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    function err_X = Verify_X(X)
    err_X = 1;
    [n,d] = size(X);
    if n<d,
        disp('Input data must be n x d!/n');
        return
    end
    err_X = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Verify_X %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%
     
    function err_k = Verify_k(k)
    err_k = 1;
    if ~isnumeric(k) | ~isreal(k) | k<1,
        disp('k must be a real integer >= 1!/n');
        return
    end
    err_k = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Verify_k %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%
     
    function [ltol,err_ltol] = Verify_ltol(ltol)
    err_ltol = 1;
    if isempty(ltol),
        ltol = 0.1;
    elseif ~isreal(ltol) | ltol<=0,
        disp('ltol must be a positive real number!');
        return
    end
    err_ltol = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Verify_ltol %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    function [maxiter,err_maxiter] = Verify_maxiter(maxiter)
    err_maxiter = 1;
    if isempty(maxiter),
        maxiter = 1000;
    elseif ~isreal(maxiter) | maxiter<=0,
        disp('ltol must be a positive real number!');
        return
    end
    err_maxiter = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Verify_maxiter %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    function [pflag,err_pflag] = Verify_pflag(pflag)
    err_pflag = 1;
    if isempty(pflag),
        pflag = 0;
    elseif pflag~=0 & pflag~=1,
        disp('Plot flag must be either 0 or 1!/n');
        return
    end
    err_pflag = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Verify_pflag %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    function [Init,err_Init] = Verify_Init(Init)
    err_Init = 1;
    if isempty(Init),
        % Do nothing;
    elseif isstruct(Init),
        [Wd,Wk] = size(Init.W);
        [Md,Mk] = size(Init.M);
        [Vd1,Vd2,Vk] = size(Init.V);
        if Wk~=Mk | Wk~=Vk | Mk~=Vk,
            disp('k in Init.W(1,k), Init.M(d,k) and Init.V(d,d,k) must equal!/n')
            return
        end
        if Md~=Vd1 | Md~=Vd2 | Vd1~=Vd2,
            disp('d in Init.W(1,k), Init.M(d,k) and Init.V(d,d,k) must equal!/n')
            return
        end
    else
        disp('Init must be a structure: W(1,k), M(d,k), V(d,d,k) or []!');
        return
    end
    err_Init = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Verify_Init %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    function [W,M,V] = Init_EM(X,k)
    [n,d] = size(X);
    [Ci,C] = kmeans(X,k,'Start','cluster', ...
        'Maxiter',100, ...
        'EmptyAction','drop', ...
        'Display','off'); % Ci(nx1) - cluster indeices; C(k,d) - cluster centroid (i.e. mean)
    while sum(isnan(C))>0,
        [Ci,C] = kmeans(X,k,'Start','cluster', ...
            'Maxiter',100, ...
            'EmptyAction','drop', ...
            'Display','off');
    end
    M = C';
    Vp = repmat(struct('count',0,'X',zeros(n,d)),1,k);
    for i=1:n, % Separate cluster points
        Vp(Ci(i)).count = Vp(Ci(i)).count + 1;
        Vp(Ci(i)).X(Vp(Ci(i)).count,:) = X(i,:);
    end
    V = zeros(d,d,k);
    for i=1:k,
        W(i) = Vp(i).count/n;
        V(:,:,i) = cov(Vp(i).X(1:Vp(i).count,:));
    end
    %%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Init_EM %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%
     
    function Plot_GM(X,k,W,M,V)
    [n,d] = size(X);
    if d>2,
        disp('Can only plot 1 or 2 dimensional applications!/n');
        return
    end
    S = zeros(d,k);
    R1 = zeros(d,k);
    R2 = zeros(d,k);
    for i=1:k,  % Determine plot range as 4 x standard deviations
        S(:,i) = sqrt(diag(V(:,:,i)));
        R1(:,i) = M(:,i)-4*S(:,i);
        R2(:,i) = M(:,i)+4*S(:,i);
    end
    Rmin = min(min(R1));
    Rmax = max(max(R2));
    R = [Rmin:0.001*(Rmax-Rmin):Rmax];
    clf, hold on
    if d==1,
        Q = zeros(size(R));
        for i=1:k,
            P = W(i)*normpdf(R,M(:,i),sqrt(V(:,:,i)));
            Q = Q + P;
            plot(R,P,'r-'); grid on,
        end
        plot(R,Q,'k-');
        xlabel('X');
        ylabel('Probability density');
    else % d==2
        plot(X(:,1),X(:,2),'r.');
        for i=1:k,
            Plot_Std_Ellipse(M(:,i),V(:,:,i));
        end
        xlabel('1^{st} dimension');
        ylabel('2^{nd} dimension');
        axis([Rmin Rmax Rmin Rmax])
    end
    title('Gaussian Mixture estimated by EM');
    %%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Plot_GM %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%
     
    function Plot_Std_Ellipse(M,V)
    [Ev,D] = eig(V);
    d = length(M);
    if V(:,:)==zeros(d,d),
        V(:,:) = ones(d,d)*eps;
    end
    iV = inv(V);
    % Find the larger projection
    P = [1,0;0,0];  % X-axis projection operator
    P1 = P * 2*sqrt(D(1,1)) * Ev(:,1);
    P2 = P * 2*sqrt(D(2,2)) * Ev(:,2);
    if abs(P1(1)) >= abs(P2(1)),
        Plen = P1(1);
    else
        Plen = P2(1);
    end
    count = 1;
    step = 0.001*Plen;
    Contour1 = zeros(2001,2);
    Contour2 = zeros(2001,2);
    for x = -Plen:step:Plen,
        a = iV(2,2);
        b = x * (iV(1,2)+iV(2,1));
        c = (x^2) * iV(1,1) - 1;
        Root1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);
        Root2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);
        if isreal(Root1),
            Contour1(count,:) = [x,Root1] + M';
            Contour2(count,:) = [x,Root2] + M';
            count = count + 1;
        end
    end
    Contour1 = Contour1(1:count-1,:);
    Contour2 = [Contour1(1,:);Contour2(1:count-1,:);Contour1(count-1,:)];
    plot(M(1),M(2),'k+');
    plot(Contour1(:,1),Contour1(:,2),'k-');
    plot(Contour2(:,1),Contour2(:,2),'k-');
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% End of Plot_Std_Ellipse %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Discussions similaires

  1. [débutant] Data mining : choix d'algorithme
    Par nemesisTHEone dans le forum Intelligence artificielle
    Réponses: 8
    Dernier message: 08/07/2010, 14h37
  2. [débutant] algorithme de calcul de prix HT/TTC
    Par keren dans le forum Algorithmes et structures de données
    Réponses: 2
    Dernier message: 06/06/2009, 12h14
  3. Réponses: 1
    Dernier message: 14/05/2008, 17h31
  4. Débutant en Algorithme
    Par Zecherche dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 19/12/2006, 18h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo