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

C Discussion :

Traitement d'image et langage C


Sujet :

C

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 3
    Par défaut Traitement d'image et langage C
    Bonjour,

    J'ai recemment eu un exercice a faire en langage C :
    Traiter l'image 10*10 inclue dans la fonction main :
    * Analyser les groupes de 1 et les transformer en sous-groupes
    Un groupe peut être un carré :
    111
    111
    111

    une croix :
    010
    111
    010

    Mais ne peuvent etre considérées comme un groupe les formes du genre:
    001 010
    010 100
    001 000 etc.

    On voudrait obtenir qqch du genre :

    {0,1,0,1,0,0,0,0,0,0},
    {0,1,0,1,0,0,2,2,2,0},
    {0,1,1,1,0,0,2,2,2,0},
    {0,0,0,0,0,2,2,2,2,0},
    {0,0,0,0,2,2,2,0,0,0},
    {0,0,0,0,0,2,0,0,0,0},
    {3,3,3,0,0,0,0,4,0,0},
    {3,3,3,0,0,0,4,4,4,0},
    {0,3,0,5,5,0,0,4,0,0},
    {0,0,0,5,5,0,0,0,0,0}};


    * Calculer le barycentre de chaque sous-groupe

    La correction que j'ai obtenu est la suivante :

    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
     
    #include <stdio.h>
     
    void affiche(int tableau[10][10])
    {int i=0, j=0;
     printf("\n");
     for(i=0;i<10;i++)
     {
      for(j=0;j<10;j++)
      printf("%4d",tableau[i][j]);
      printf("\n");
     }
     printf("\n");}
     
    void clustering(int tableau[10][10])
    {int groupe_actuel=2, i=0, j=0, groupe, groupe_old, i2, j2, temp;
     
     for(i=0;i<10;i++)
     {
      for(j=0;j<10;j++)
      if (tableau[i][j]==1)
      {groupe=0;
       if( (j>0)&&(i>0)&&(tableau[i][j-1]>1)&&(tableau[i-1][j]>1)&&(tableau[i-1][j]!=tableau[i][j-1]) )
       {groupe_old=tableau[i][j-1];
        groupe=tableau[i-1][j];
     
         for(j2=0;j2<10;j2++)
         if( tableau[i2][j2]==groupe_old)
         tableau[i2][j2]=groupe;}
     
        if(i>0)
        {if( tableau[i-1][j]>0 )
         groupe=tableau[i][j-1];}
     
        if(j>0)
        {if(tableau[i][j-1]>0)
         groupe=tableau[i][j-1];}
     
        if(groupe==0) 
        {groupe=groupe_actuel;
         groupe_actuel++;}
     
        tableau[i][j]=groupe;
        }
     
     }
     
        i2=2;
        j2=1;
        temp=0;
     
        while(i2 < groupe_actuel)
        {
         for(i=0;i<10;i++)
         {
          for(j=0;j<10;j++)
          if (tableau [i][j]!=0)
          { if (tableau[i][j]==i2)
            {tableau[i][j]=j2;temp++;}
          }
     
         }
          if (temp!=0)
          {j2++;temp=0;}
     i2++;
        }
     
    }
     
    main()
    {int i;
     int t[10][10]= {{0,1,0,1,0,0,0,0,0,0},
                          {0,1,0,1,0,0,1,1,1,0},
                          {0,1,1,1,0,0,1,1,1,0},
                          {0,0,0,0,0,1,1,1,1,0},
                          {0,0,0,0,1,1,1,0,0,0},
                          {0,0,0,0,0,1,0,0,0,0},
                          {1,1,1,0,0,0,0,1,0,0},
                          {1,1,1,0,0,0,1,1,1,0},
                          {0,1,0,1,1,0,0,1,0,0},
                          {0,0,0,1,1,0,0,0,0,0}};
     affiche(t);
     printf("--------------------------------\n");
     clustering(t);
     affiche(t);
    }
    Mais il semble y avoir un problème...
    Tel quel, le code est compilé sans pb mais il ya une
    erreur a l'execution (Voulez vous envoyer le rapport
    d'erreur... blablabla...).
    Du moins chez moi.

    Je ne vois pas trop d'où çà peut venir, mais
    j'ai remarqué que si on initialise i2 et j2 à 0, pas d'erreur
    a l'execution mais les groupes ne sont pas formés comme
    on voudrait.

    Si vous pouviez m'éclairer, merci d'avance.

  2. #2
    Membre émérite Avatar de crocodilex
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    697
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 697
    Par défaut
    Citation Envoyé par Jean|Jean[FR]
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    void clustering(int tableau[10][10])
    {
         int groupe_actuel=2, i=0, j=0, groupe, groupe_old, i2, j2, temp;
         [...] 
         if( tableau[i2][j2]==groupe_old)
         tableau[i2][j2]=groupe;
         [...]
    }
    Le compilateur dit que la variable "i2" est utilisée sans être initialisée...

    C'est normal ?

  3. #3
    Membre émérite

    Profil pro
    Inscrit en
    Août 2003
    Messages
    878
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 878
    Par défaut
    Citation Envoyé par crocodilex
    Le compilateur dit que la variable "i2" est utilisée sans être initialisée...

    C'est normal ?
    A priori, c'est normal qu'il le dise.

  4. #4
    Membre émérite Avatar de crocodilex
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    697
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 697
    Par défaut
    Citation Envoyé par David.Schris
    A priori, c'est normal qu'il le dise.
    Le "c'est normal ?" était purement ironique....

  5. #5
    Membre émérite

    Profil pro
    Inscrit en
    Août 2003
    Messages
    878
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 878
    Par défaut
    Citation Envoyé par crocodilex
    Le "c'est normal ?" était purement ironique....
    Ah bon ?

  6. #6
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par Jean|Jean[FR]
    Tel quel, le code est compilé sans pb mais il ya une
    erreur a l'execution (Voulez vous envoyer le rapport
    d'erreur... blablabla...).
    Du moins chez moi.

    Comment débugger ?

    1 - Compiler en mode sévère

    http://emmanuel-delahaye.developpez....tm#cfg_compilo

    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
     
    Project   : Forums
    Compiler  : GNU GCC Compiler (called directly)
    Directory : C:\dev\forums2\
    --------------------------------------------------------------------------------
    Switching to target: default
    Compiling: main.c
    main.c:70: warning: return type defaults to `int'
    main.c:70: warning: function declaration isn't a prototype
    main.c: In function `main':
    main.c:70: warning: unused variable `i'
    main.c: In function `clustering':
    main.c:15: warning: 'i2' might be used uninitialized in this function
    main.c: In function `main':
    main.c:85: warning: control reaches end of non-void function
    Linking console executable: console.exe
    Process terminated with status 0 (0 minutes, 6 seconds)
    0 errors, 5 warnings
    Déjà, ça (ligne 15), c'est pas net. Il faudrait le corriger d'urgence...

    2 - Rendre le code lisible

    http://emmanuel-delahaye.developpez....tm#indentation

    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
     
    #include <stdio.h>
     
    void affiche (int tableau[10][10])
    {
       int i = 0, j = 0;
       printf ("\n");
       for (i = 0; i < 10; i++)
       {
          for (j = 0; j < 10; j++)
             printf ("%4d", tableau[i][j]);
          printf ("\n");
       }
       printf ("\n");
    }
     
    void clustering (int tableau[10][10])
    {
       int groupe_actuel = 2, i = 0, j = 0, groupe, groupe_old, i2, j2, temp;
     
       for (i = 0; i < 10; i++)
       {
          for (j = 0; j < 10; j++)
             if (tableau[i][j] == 1)
             {
                groupe = 0;
                if ((j > 0) && (i > 0) && (tableau[i][j - 1] > 1)
                    && (tableau[i - 1][j] > 1)
                    && (tableau[i - 1][j] != tableau[i][j - 1]))
                {
                   groupe_old = tableau[i][j - 1];
                   groupe = tableau[i - 1][j];
     
                   for (j2 = 0; j2 < 10; j2++)
                      if (tableau[i2][j2] == groupe_old)
                         tableau[i2][j2] = groupe;
                }
     
                if (i > 0)
                {
                   if (tableau[i - 1][j] > 0)
                      groupe = tableau[i][j - 1];
                }
     
                if (j > 0)
                {
                   if (tableau[i][j - 1] > 0)
                      groupe = tableau[i][j - 1];
                }
     
                if (groupe == 0)
                {
                   groupe = groupe_actuel;
                   groupe_actuel++;
                }
     
                tableau[i][j] = groupe;
             }
     
       }
     
       i2 = 2;
       j2 = 1;
       temp = 0;
     
       while (i2 < groupe_actuel)
       {
          for (i = 0; i < 10; i++)
          {
             for (j = 0; j < 10; j++)
                if (tableau[i][j] != 0)
                {
                   if (tableau[i][j] == i2)
                   {
                      tableau[i][j] = j2;
                      temp++;
                   }
                }
     
          }
          if (temp != 0)
          {
             j2++;
             temp = 0;
          }
          i2++;
       }
     
    }
     
    main ()
    {
       int i;
       int t[10][10] = 
       { 
       {0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
       {0, 1, 0, 1, 0, 0, 1, 1, 1, 0},
       {0, 1, 1, 1, 0, 0, 1, 1, 1, 0},
       {0, 0, 0, 0, 0, 1, 1, 1, 1, 0},
       {0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
       {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
       {1, 1, 1, 0, 0, 0, 0, 1, 0, 0},
       {1, 1, 1, 0, 0, 0, 1, 1, 1, 0},
       {0, 1, 0, 1, 1, 0, 0, 1, 0, 0},
       {0, 0, 0, 1, 1, 0, 0, 0, 0, 0}
       };
       affiche (t);
       printf ("--------------------------------\n");
       clustering (t);
       affiche (t);
    }
    3 - Poser des pièges.

    Ici, il y a des tableaux et des indices, d'où un risque permanent de débordement.

    Utiliser la macro assert() (<assert.h>) qui permet de vérifier si le comportement est correct à l'exécution, notamment en vérifiant le domaine des indices.

    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
     
    #include <stdio.h>
    #include <assert.h>
     
    void affiche (int tableau[10][10])
    {
       int i = 0, j = 0;
       printf ("\n");
       for (i = 0; i < 10; i++)
       {
          for (j = 0; j < 10; j++)
             printf ("%4d", tableau[i][j]);
          printf ("\n");
       }
       printf ("\n");
    }
     
    void clustering (int tableau[10][10])
    {
       int groupe_actuel = 2, i = 0, j = 0, groupe, groupe_old, i2, j2, temp;
     
       for (i = 0; i < 10; i++)
       {
          for (j = 0; j < 10; j++)
             if (tableau[i][j] == 1)
             {
                groupe = 0;
     
                assert (i - 1 >= 0);
                assert (j - 1 >= 0);
     
                assert (i < 10);
                assert (j < 10);
     
                if ((j > 0) && (i > 0) && (tableau[i][j - 1] > 1)
                    && (tableau[i - 1][j] > 1)
                    && (tableau[i - 1][j] != tableau[i][j - 1]))
                {
                   groupe_old = tableau[i][j - 1];
                   groupe = tableau[i - 1][j];
     
                   for (j2 = 0; j2 < 10; j2++)
                   {
                      if (tableau[i2][j2] == groupe_old)
                      {
                         tableau[i2][j2] = groupe;
                      }
                   }
                }
     
                if (i > 0)
                {
                   if (tableau[i - 1][j] > 0)
                      groupe = tableau[i][j - 1];
                }
     
                if (j > 0)
                {
                   if (tableau[i][j - 1] > 0)
                      groupe = tableau[i][j - 1];
                }
     
                if (groupe == 0)
                {
                   groupe = groupe_actuel;
                   groupe_actuel++;
                }
     
                tableau[i][j] = groupe;
             }
     
       }
     
       i2 = 2;
       j2 = 1;
       temp = 0;
     
       while (i2 < groupe_actuel)
       {
          for (i = 0; i < 10; i++)
          {
             for (j = 0; j < 10; j++)
                if (tableau[i][j] != 0)
                {
                   if (tableau[i][j] == i2)
                   {
                      tableau[i][j] = j2;
                      temp++;
                   }
                }
     
          }
          if (temp != 0)
          {
             j2++;
             temp = 0;
          }
          i2++;
       }
     
    }
     
    int main (void)
    {
       int t[10][10] = {
          {0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
          {0, 1, 0, 1, 0, 0, 1, 1, 1, 0},
          {0, 1, 1, 1, 0, 0, 1, 1, 1, 0},
          {0, 0, 0, 0, 0, 1, 1, 1, 1, 0},
          {0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
          {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
          {1, 1, 1, 0, 0, 0, 0, 1, 0, 0},
          {1, 1, 1, 0, 0, 0, 1, 1, 1, 0},
          {0, 1, 0, 1, 1, 0, 0, 1, 0, 0},
          {0, 0, 0, 1, 1, 0, 0, 0, 0, 0}
       };
       affiche (t);
       printf ("--------------------------------\n");
       clustering (t);
       affiche (t);
     
       return 0;
    }
    Ca n'a pas trainé...
    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
     
     
       0   1   0   1   0   0   0   0   0   0
       0   1   0   1   0   0   1   1   1   0
       0   1   1   1   0   0   1   1   1   0
       0   0   0   0   0   1   1   1   1   0
       0   0   0   0   1   1   1   0   0   0
       0   0   0   0   0   1   0   0   0   0
       1   1   1   0   0   0   0   1   0   0
       1   1   1   0   0   0   1   1   1   0
       0   1   0   1   1   0   0   1   0   0
       0   0   0   1   1   0   0   0   0   0
     
    --------------------------------
    Assertion failed: i-1 >= 0, file main.c, line 28
     
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
     
    Press ENTER to continue.

Discussions similaires

  1. quel langage de programmation pour traitement d'image
    Par nano8308 dans le forum Traitement d'images
    Réponses: 8
    Dernier message: 02/02/2011, 09h44
  2. Quel langage pour le traitement d'image ?
    Par _gabor dans le forum Traitement d'images
    Réponses: 33
    Dernier message: 24/03/2010, 14h16
  3. Quel langage/outil libre pour du traitement d'image?
    Par Miss Ti dans le forum Langages de programmation
    Réponses: 16
    Dernier message: 04/08/2008, 14h02
  4. Quel langage pour du traitement d'image ?
    Par shawty dans le forum Langages de programmation
    Réponses: 11
    Dernier message: 03/12/2007, 11h43
  5. Traitement d'images : quel langage?
    Par belasri dans le forum Langages de programmation
    Réponses: 19
    Dernier message: 07/10/2005, 09h59

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