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

MATLAB Discussion :

[Débutant] Calcul de la covariance


Sujet :

MATLAB

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de Alucard9800XT
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    96
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 96
    Par défaut [Débutant] Calcul de la covariance
    Bonsoir donc voila dans mon programme je recupere des echantillons d'image, puis je calcul la moyenne et la covariance
    pour la covariance je transforme dabord ma matrice en vecteur affin que le resultat de la covariance soit un seul element
    mais j'obtien un message d'erreur
    donc voici mon programme:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Z1=imcrop(Z0);
    [N1,M1,K1]=size(Z1);
    Pix1=N1*M1;
    Z11=Z1(:,:,1);
    Z12=Z1(:,:,2);
    Z13=Z1(:,:,3);
    moy1 = mean([mean(mean(Z11)) mean(mean(Z12)) mean(mean(Z13))])
    Y11=reshape(Z11,1,Pix1);
    Y12=reshape(Z12,1,Pix1);
    Y13=reshape(Z13,1,Pix1);
    cov1 = mean([cov(Y11) cov(Y12) cov(Y13)]);
    Prob1=Pix1/Pix0;
    et voici mon erreur delivré par Matlab:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    moy1 =
     
      155.4033
     
    ??? Error using ==> minus
    Integers can only be combined with integers of the same class, or scalar doubles.
     
    Error in ==> cov at 64
      xc = x - repmat(sum(x)/m,m,1);  % Remove mean
     
    Error in ==> ttt2 at 38
    cov1 = mean([cov(Y11) cov(Y12) cov(Y13)]);
     
    ??? Error while evaluating uimenu Callback.
    et merci

  2. #2
    Membre confirmé Avatar de Alucard9800XT
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    96
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 96
    Par défaut
    c'est bon j'ai fait ça et j'ai plus d'erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    Z1=imcrop(Z0);
    [N1,M1,K1]=size(Z1);
    Pix1=N1*M1;
    Z11=Z1(:,:,1);
    Z12=Z1(:,:,2);
    Z13=Z1(:,:,3);
    moy1 = mean([mean(mean(Z11)) mean(mean(Z12)) mean(mean(Z13))])
    Y11=reshape(Z11,1,Pix1);
    Y12=reshape(Z12,1,Pix1);
    Y13=reshape(Z13,1,Pix1);
    cov1 = mean([cov(double(Y11)) cov(double(Y12)) cov(double(Y13))])
    Prob1=Pix1/Pix0;
    desoler pour le derangement

  3. #3
    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
    En effet, il semble que Y11, Y12 et Y13 soient de type uint???.
    COV ne peut donc pas directement calculer avec, il faut les convertir en double :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    cov1 = mean([cov(double(Y11)) cov(double(Y12)) cov(double(Y13))]);
    ... mais...
    Citation Envoyé par Alucard9800XT
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Y11=reshape(Z11,1,Pix1);
    Y12=reshape(Z12,1,Pix1);
    Y13=reshape(Z13,1,Pix1);
    cov1 = mean([cov(double(Y11)) cov(double(Y12)) cov(double(Y13))])
    Les variables Y? sont inutiles :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    cov1 = mean([cov(double(Z11(:))) ; cov(double(Z12(:))) ; cov(double(Z13(:)))])
    ... mais...
    Citation Envoyé par Alucard9800XT
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Z11=Z1(:,:,1);
    Z12=Z1(:,:,2);
    Z13=Z1(:,:,3);
    moy1 = mean([mean(mean(Z11)) mean(mean(Z12)) mean(mean(Z13))])
    Pourquoi ne pas simplement faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    moy1 = mean(double(Z1(:)))

  4. #4
    Membre confirmé Avatar de Alucard9800XT
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    96
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 96
    Par défaut
    Voici le code modifié
    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
    screen = get(0, 'ScreenSize');
    x=screen(3);
    y=screen(4);
    pos=[50 50 900 600];
    H=figure('name','test1','menubar','none','numbertitle','off',...
         'Position',pos,'color',[0.7 0.7 0.7])
    %afficher l'image dans la fenetre
    Z=X(:,:,1);
    Z0=X;
    [N0,M0,L0]=size(Z0);
    Pix0=N0*M0;
     
     
     
    imshow(Z0);title('Veuillez selectionner grace au curseur 5 classes l''une apres l''autre')
    %relevée des échantillons et calcul de leurs statistiques
    %Echantillon 1 
    Z1=imcrop(Z0);
    [N1,M1,K1]=size(Z1);
    Pix1=N1*M1;
    moy1 = mean(double(Z1(:)))
    cov1 = mean([cov(double(Z11(:))) ; cov(double(Z12(:))) ; cov(double(Z13(:)))])
    Prob1=Pix1/Pix0;
    %Echantillon 2
    Z2=imcrop(Z0);
    [N2,M2,K2]=size(Z2);
    moy2 = mean(double(Z2(:)))
    cov2 = mean([cov(double(Z21(:))) ; cov(double(Z22(:))) ; cov(double(Z23(:)))])
    Pix2=N2*M2;
    Prob2=Pix2/Pix0;
    %Echantillon 3
    Z3=imcrop(Z0);
    [N3,M3,K3]=size(Z3);
    moy3 = mean(double(Z3(:)))
    cov3 = mean([cov(double(Z31(:))) ; cov(double(Z32(:))) ; cov(double(Z33(:)))])
    Pix3=N3*M3;
    Prob3=Pix3/Pix0;
    %Echantillon 4
    Z4=imcrop(Z0);
    [N4,M4,K4]=size(Z4);
    moy4 = mean(double(Z4(:)))
    cov4 = mean([cov(double(Z41(:))) ; cov(double(Z42(:))) ; cov(double(Z43(:)))])
    Pix4=N4*M4;
    Prob4=Pix4/Pix0;
    %Echantillon 5
    Z5=imcrop(Z0);
    [N5,M5,K5]=size(Z5);
    moy5 = mean(double(Z5(:)))
    cov5 = mean([cov(double(Z51(:))) ; cov(double(Z52(:))) ; cov(double(Z53(:)))])
    Pix5=N5*M5;
    Prob5=Pix5/Pix0;
    %Echantillon 6
    Z6=imcrop(Z0);
    [N6,M6,K6]=size(Z6);
    moy6 = mean(double(Z6(:)))
    cov6 = mean([cov(double(Z61(:))) ; cov(double(Z62(:))) ; cov(double(Z63(:)))])
    Pix6=N6*M6;
    Prob6=Pix6/Pix0;
     
     
    close;
    %*************************************test********************************
     
    for k=1:N0
        for l=1:M0
    f1=max(-log(cov1)^0.5-0.5*(double(Z0(k,l))-moy1)'*cov1^-1*(double(Z0(k,l))-moy1));
    f2=max(-log(cov2)^0.5-0.5*(double(Z0(k,l))-moy2)'*cov2^-1*(double(Z0(k,l))-moy2));
    f3=max(-log(cov3)^0.5-0.5*(double(Z0(k,l))-moy3)'*cov3^-1*(double(Z0(k,l))-moy3));
    f4=max(-log(cov4)^0.5-0.5*(double(Z0(k,l))-moy4)'*cov4^-1*(double(Z0(k,l))-moy4));
    f5=max(-log(cov5)^0.5-0.5*(double(Z0(k,l))-moy5)'*cov5^-1*(double(Z0(k,l))-moy5));
    f6=max(-log(cov6)^0.5-0.5*(double(Z0(k,l))-moy6)'*cov6^-1*(double(Z0(k,l))-moy6));
     
               if              f1>f2 & f1>f3 & f1>f4 & f1>f5 &  f1>f6
     
                         Z(k,l,1)=255;Z(k,l,2)=0;Z(k,l,3)=0; %  ROUGE
     
                   elseif        f2>f1 & f2>f3 & f2>f4 & f2>f5 &  f2>f6
     
                         Z(k,l,1)=0;Z(k,l,2)=255;Z(k,l,3)=0;  %  VERT
     
                    elseif       f3>f1 & f3>f2 & f3>f4 & f3>f5 &  f3>f6
     
                         Z(k,l,1)=0;Z(k,l,2)=0;Z(k,l,3)=255;  %  BLEU
     
                    elseif       f4>f1 & f4>f2 & f4>f3 & f4>f5 &  f4>f6
     
                         Z(k,l,1)=255;Z(k,l,2)=255;Z(k,l,3)=0;  %  JAUNE
     
                   elseif        f5>f1 & f5>f2 & f5>f3 & f5>f4 &  f5>f6
     
                         Z(k,l,1)=255;Z(k,l,2)=0;Z(k,l,3)=255;  %   MAGENTA
     
                   elseif       f6>f1 & f6>f2 & f6>f3 & f6>f4 &  f6>f5
     
                         Z(k,l,1)=0;Z(k,l,2)=255;Z(k,l,3)=255;  %   CYAN
                   end
     
            end
    end
     
    subplot(122);imshow(Z);
    je ne sais pas comment regler le problème des warning
    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
    H =
     
         2
     
     
    H =
     
         2
     
    Warning: Image is too big to fit on screen; displaying at 56% scale.
    > In truesize>Resize1 at 308
      In truesize at 44
      In imshow at 161
      In ttt3 at 19
    Warning: Image is too big to fit on screen; displaying at 56% scale.
    > In truesize>Resize1 at 308
      In truesize at 44
      In imshow at 161
      In imcrop>ParseInputs at 193
      In imcrop at 89
      In ttt3 at 22
     
    moy1 =
     
       69.1311
     
     
    cov1 =
     
      315.4640
     
    Warning: Image is too big to fit on screen; displaying at 56% scale.
    > In truesize>Resize1 at 308
      In truesize at 44
      In imshow at 161
      In imcrop>ParseInputs at 193
      In imcrop at 89
      In ttt3 at 29
     
    moy2 =
     
      104.5775
     
     
    cov2 =
     
      159.7448
     
    Warning: Image is too big to fit on screen; displaying at 56% scale.
    > In truesize>Resize1 at 308
      In truesize at 44
      In imshow at 161
      In imcrop>ParseInputs at 193
      In imcrop at 89
      In ttt3 at 36
     
    moy3 =
     
       59.9118
     
     
    cov3 =
     
       28.6492
     
    Warning: Image is too big to fit on screen; displaying at 56% scale.
    > In truesize>Resize1 at 308
      In truesize at 44
      In imshow at 161
      In imcrop>ParseInputs at 193
      In imcrop at 89
      In ttt3 at 43
     
    moy4 =
     
       96.3344
     
     
    cov4 =
     
      152.2151
     
    Warning: Image is too big to fit on screen; displaying at 56% scale.
    > In truesize>Resize1 at 308
      In truesize at 44
      In imshow at 161
      In imcrop>ParseInputs at 193
      In imcrop at 89
      In ttt3 at 50
     
    moy5 =
     
      174.9419
     
     
    cov5 =
     
      156.1653
     
    Warning: Image is too big to fit on screen; displaying at 56% scale.
    > In truesize>Resize1 at 308
      In truesize at 44
      In imshow at 161
      In imcrop>ParseInputs at 193
      In imcrop at 89
      In ttt3 at 57
     
    moy6 =
     
      149.4792
     
     
    cov6 =
     
       26.1447
    et merci

  5. #5
    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
    Les warning ne sont pas très important ici. Les images sont trop grosses par rapport à la taille de l'écran. IMSHOW les redimensionne donc pour qu'elles puissent apparaitre en entier.

  6. #6
    Expert confirmé
    Avatar de Caro-Line
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    9 458
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 9 458
    Par défaut
    Un peu de vectorisation, de pré-allocation...

    1ère partie sur les calculs de moyi et covi :
    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
     
    %relevée des échantillons et calcul de leurs statistiques
    %on ne conserve dans des vecteurs moy et covariance
    for k = 1:6
        Zk = imcrop(Z0);
    %on n'a pas besoin de garder Zk pour la suite, on l'écrase à chq fois
        [N,M,K]=size(Zk);%idem pour N, M, K
        Pix = N*M;
        moy(k) = mean(double(Zk(:)));
        Zk1=Zk(:,:,1);
        Zk2=Zk(:,:,2);
        Zk3=Zk(:,:,3);
        covariance(k) = mean([cov(double(Zk1(:))) ; cov(double(Zk2(:))) ; cov(double(Zk3(:)))]);
        Prob=Pix/Pix0;%si doit servir dans la suite mettre Pix(k)
    end
    ça gagne du temps non ?

    Et pour le test :
    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
     
    %Le Z utilisé ici est bien tout autre que le Z défini au début (et qui ne
    %semble servir à rien ?) ?
    Z = zeros(N0,M0,3);%preallocation, permet d'accélérer le traitement
    for k=1:N0
        for lind=1:M0 %le l n'est pas forcément très lisible et ce confond avec le 1
            f = zeros(1,6);
            for indice = 1:6
                f(indice)=max(-log(covariance(indice))^0.5-0.5*(double(Z0(k,lind))-moy(indice))'*covariance(indice)^-1*(double(Z0(k,lind))-moy(indice)));
            end
            [maximumF,indMaxF] = max(f);
            switch indMaxF %indice du f le plus grand
                case 1
                    Z(k,lind,1)=255; %  ROUGE
                case 2
                    Z(k,lind,2)=255;  %  VERT
                case 3
                    Z(k,lind,3)=255;  %  BLEU
                case 4
                    Z(k,lind,1)=255;
                    Z(k,lind,2)=255;  %  JAUNE
                case 5
                    Z(k,lind,1)=255;
                    Z(k,lind,3)=255;  %   MAGENTA
                case 6
                    Z(k,lind,2)=255;
                    Z(k,lind,3)=255;  %   CYAN
            end
        end
    end
    bon après comme il n'y a pas le code entier il faut peut-être mettre des indices à d'autres données. Mais ça te donne déjà des idées...

  7. #7
    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
    Citation Envoyé par caro95470
    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
            maximumF,indMaxF] = max(f);
            switch indMaxF %indice du f le plus grand
                case 1
                    Z(k,lind,1)=255; %  ROUGE
                case 2
                    Z(k,lind,2)=255;  %  VERT
                case 3
                    Z(k,lind,3)=255;  %  BLEU
                case 4
                    Z(k,lind,1)=255;
                    Z(k,lind,2)=255;  %  JAUNE
                case 5
                    Z(k,lind,1)=255;
                    Z(k,lind,3)=255;  %   MAGENTA
                case 6
                    Z(k,lind,2)=255;
                    Z(k,lind,3)=255;  %   CYAN
            end
    Continuons un peu la vectorisation...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    [maximumF,indMaxF] = max(f);
    Z(k,lind,1)=255*(ismember(indMaxF,[1 4 5]));
    Z(k,lind,2)=255*(ismember(indMaxF,[2 4 6]));
    Z(k,lind,3)=255*(ismember(indMaxF,[3 5 6]));
    Ou encore plus fort... la botte secrète ISMEMBC
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    [maximumF,indMaxF] = max(f);
    Z(k,lind,1)=255*(ismembc(indMaxF,[1 4 5]));
    Z(k,lind,2)=255*(ismembc(indMaxF,[2 4 6]));
    Z(k,lind,3)=255*(ismembc(indMaxF,[3 5 6]));

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Calculer la cross-covariance en ignorant les NaN ?
    Par helaaa dans le forum MATLAB
    Réponses: 1
    Dernier message: 24/08/2011, 12h16
  2. Calcul d'une matrice de Variance Covariance
    Par ANOVA dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 19/05/2010, 18h22
  3. Réponses: 3
    Dernier message: 03/12/2009, 17h27
  4. Calcul matrice variance-covariance
    Par Alucard9800XT dans le forum MATLAB
    Réponses: 2
    Dernier message: 10/05/2007, 13h18
  5. [TP7] Calculer sin, cos, tan, sqrt via le FPU
    Par zdra dans le forum Assembleur
    Réponses: 8
    Dernier message: 25/11/2002, 05h09

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