00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00038 #include "format_learning_set_from_imau1.h"
00039 #include "image.h"
00040 #include "proto2D.h"
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043
00044
00045
00046
00073
00074
00075
00076
00077
00078
00096 param* format_learning_set_from_imau1_lect(
00097 format_learning_set_from_imau1_t *desc_format,
00098 param *ptp,
00099 char *debq
00100 )
00101 {
00102 char question[500];
00103 int i;
00104
00105
00106 sprintf(question, "%s Nombre d'attributs ", debq);
00107 lec_param(question, ptp);
00108 desc_format->nb_att = atoi(ptp->rep);
00109 ptp = ptp->next;
00110
00111 if(desc_format->nb_att == 0)
00112 {
00113 printf("Nb Att = 0!!\n\n");
00114 exit(1);
00115 }
00116
00117
00118 if((desc_format->att_rea = (read_ima_t*)malloc(desc_format->nb_att*sizeof(read_ima_t)))==NULL)
00119 {
00120 printf("Alloc structures %d images imp.",desc_format->nb_att);
00121 exit(1);
00122 }
00123
00124 for(i=0;i<desc_format->nb_att;i++)
00125 {
00126 printf("\nChemin de l'attributs %d/%d :",i+1,desc_format->nb_att);
00127 ptp = read_ima_lect(&desc_format->att_rea[i], ptp, ">> Chemin de l'attribut (.ras) ");
00128 }
00129
00130
00131 ptp = format_imau1_classifier_input_lect(&desc_format->format_des_input, ptp, ">> Format attributs->data input lecture : ");
00132
00133
00134 ptp = read_ima_lect(&desc_format->masque_rea, ptp, ">> Format learning set : chemin du masque (.ras) ");
00135
00136
00137 ptp = format_imau1_classifier_crisp_output_lect(&desc_format->format_des_output, ptp, ">> Format data output lecture : ");
00138
00139 return ptp;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00176 int format_learning_set_from_imau1_init(
00177 format_learning_set_from_imau1_t *desc_format,
00178 const int LABEL_FOND,
00179 learning_set *learn
00180 )
00181 {
00182
00183 int i,j,k;
00184 int stop;
00185 int l;
00186 int indice_c_max;
00187
00188 printf("\nInitialisation de learning set...\n\n");
00189
00190
00191
00192
00193
00194
00195 printf("Traitement des attributs...\n");
00196
00197
00198 if((desc_format->att_ima = (imau1*)malloc(desc_format->nb_att*sizeof(imau1))) == NULL)
00199 {
00200 printf("\nAlloc images entrées impossible\n");
00201 exit(1);
00202 }
00203
00204
00205 for(i=0;i<desc_format->nb_att;i++)
00206 read_imau1_init(desc_format->att_rea+i,desc_format->att_ima+i);
00207
00208
00209 for(i=0;i<desc_format->nb_att;i++)
00210 format_imau1_classifier_input_init(&desc_format->format_des_input, desc_format->att_ima[i], &desc_format->dataIattributs);
00211
00212
00213 read_imau1_init(&desc_format->masque_rea,&desc_format->masque_ima);
00214 format_imau1_classifier_crisp_output_init(&desc_format->format_des_output,desc_format->masque_ima,&desc_format->dataOmasque);
00215
00216
00217 for(i=0;i<desc_format->nb_att;i++)
00218 format_imau1_classifier_input_calc(&desc_format->format_des_input, desc_format->att_ima[i], &desc_format->dataIattributs);
00219
00220
00221 format_imau1_classifier_crisp_output_calc(&desc_format->format_des_output, desc_format->masque_ima, &desc_format->dataOmasque);
00222
00223
00224 printf("\nLiberation des images et des structures associees...");fflush(stdout);
00225 for(i=0;i<desc_format->nb_att;i++)
00226 free_imau1(&desc_format->att_ima[i]);
00227 free(desc_format->att_ima);
00228 free(desc_format->att_rea);
00229
00230 printf("Ok\n");
00231
00232
00233
00234
00235
00236
00237
00238
00239 if(desc_format->dataIattributs.nb_pts < 2 || desc_format->dataOmasque.nb_pts < 2)
00240 {
00241 printf("\n\nERREUR learning set : Pas assez de points attributs = %d et masque = %d (<2?)\n",
00242 desc_format->dataIattributs.nb_pts,desc_format->dataOmasque.nb_pts);
00243 exit(1);
00244 }
00245
00246
00247 if(desc_format->dataIattributs.nb_pts != desc_format->dataOmasque.nb_pts)
00248 {
00249 printf("\n\nERREUR : le masque et les attributs doivent comporter le meme nombre de points.\n\n");
00250 printf("desc_format->dataIattributs.nb_pts = %d - desc_format->dataOmasque.nb_pts = %d\n",desc_format->dataIattributs.nb_pts,desc_format->dataOmasque.nb_pts);
00251 printf("desc_format->dataIattributs.equ_nb_rows = %d - desc_format->dataOmasque.equ_nb_rows = %d\n",desc_format->dataIattributs.equ_nb_rows,desc_format->dataOmasque.equ_nb_rows);
00252 printf("desc_format->dataIattributs.equ_nb_cols = %d - desc_format->dataOmasque.equ_nb_cols = %d\n",desc_format->dataIattributs.equ_nb_cols,desc_format->dataOmasque.equ_nb_cols);
00253 exit(1);
00254 }
00255
00256
00257
00258
00259
00260
00261
00262 learn->output_ref.nb_class = 1;
00263
00264
00265 if((desc_format->classes = (une_classe *)malloc(1*sizeof(une_classe))) == NULL)
00266 {
00267 printf ("\n\nERREUR learning set : allocation classes learn impossible\n");
00268 exit(1);
00269 }
00270
00271
00272 desc_format->classes[0].label = desc_format->dataOmasque.classes.crisp[0];
00273 desc_format->classes->nb_pts = 1;
00274
00275
00276 j = 1;
00277
00278
00279
00280
00281 for(i=1;i<desc_format->dataIattributs.nb_pts;i++)
00282 {
00283
00284
00285 k=0;
00286 while(k< learn->output_ref.nb_class)
00287 {
00288
00289 if(desc_format->classes[k].label == desc_format->dataOmasque.classes.crisp[i])
00290 {
00291 desc_format->classes[k].nb_pts++;
00292
00293
00294 break;
00295 }
00296 else
00297 {
00298 k++;
00299
00300 if(k == learn->output_ref.nb_class)
00301 {
00302
00303 learn->output_ref.nb_class++;
00304 if((desc_format->classes =
00305 (une_classe *)realloc(desc_format->classes, (learn->output_ref.nb_class)*(sizeof(une_classe)))) == NULL)
00306 {
00307 printf ("\n\nERREUR learning set : reallocation classes impossible\n");
00308 exit(1);
00309 }
00310
00311 desc_format->classes[k].nb_pts = 1;
00312 desc_format->classes[k].label = desc_format->dataOmasque.classes.crisp[i];
00313 break;
00314 }
00315 }
00316 }
00317 }
00318
00319
00320
00321 if(learn->output_ref.nb_class == 1)
00322 {
00323 printf("\n\nERREUR learning set : Il n'y a visiblement qu'une seule classe, \n");
00324 printf("donc tous les points sont dans la meme classe. Pas d'interet.\n");
00325 exit(1);
00326 }
00327
00328
00329
00330
00331 indice_c_max = 0;
00332 desc_format->c_max = desc_format->classes[indice_c_max].label;
00333
00334
00335 for(i=1;i<learn->output_ref.nb_class;i++)
00336 {
00337 if(desc_format->classes[i].nb_pts > desc_format->classes[indice_c_max].nb_pts)
00338 {
00339 desc_format->c_max = desc_format->classes[i].label;
00340 indice_c_max = i;
00341 }
00342 }
00343
00344
00345 if(desc_format->c_max != LABEL_FOND)
00346 {
00347 printf("\n---------------------------------------------------------------------\n");
00348 printf("\nATTENTION \nLe label du fond specifie en parametre de la fonction init \n");
00349 printf("de l'operateur format_learning_set_from_imau1 est different\n");
00350 printf("de celui detecte par la fonction.");
00351
00352
00353
00354
00355
00356
00357
00358 printf("\n\nINFO : La classe avec le label '%d' est prise comme le fond et ne sera pas\n",
00359 desc_format->classes[indice_c_max].label);
00360 printf("prise dans les donnees d'apprentissage vu qu'elle est celle \n");
00361 printf("avec le plus grand nombre de points (%d points sur un total de %d). \n",
00362 desc_format->classes[indice_c_max].nb_pts,desc_format->dataIattributs.nb_pts);
00363 printf("Seulement les autres classes seront incluses dans le learning set.\n");
00364 printf("\nAcceptez (1 + <enter>) ou changez le label a exclure (0 + <enter>) ? : ");
00365 scanf("%d",&l);
00366
00367 stop = 0;
00368 while(stop == 0)
00369 {
00370 switch(l)
00371 {
00372 case 0 :
00373 while(stop == 0)
00374 {
00375 printf("\nLes differents labels sont : \n");
00376
00377 for(i=0;i<learn->output_ref.nb_class;i++)
00378 printf(" label %d (%d points) \n",desc_format->classes[i].label,desc_format->classes[i].nb_pts);
00379
00380 printf("\nNumero du label a exclure ? : ");
00381 scanf("%d",&desc_format->c_max);
00382
00383
00384 i=0;
00385 while(i<learn->output_ref.nb_class)
00386 {
00387 if(desc_format->classes[i].label == desc_format->c_max)
00388 {
00389 stop = 1;
00390 indice_c_max = 0;
00391 while(desc_format->classes[indice_c_max].label != desc_format->c_max)
00392 indice_c_max ++;
00393 break;
00394 }
00395
00396 if(i+1==learn->output_ref.nb_class)
00397 {
00398 printf("\nCe label n'existe pas.\n");
00399 break;
00400 }
00401
00402 i++;
00403 }
00404 break;
00405
00406 case 1:
00407 stop = 1;
00408 break;
00409
00410 default :
00411 printf("\nTapez 0 (pour modifier le label de la classe de fond) ou 1 (pour continuer) : ");
00412 scanf("%d",&l);
00413 break;
00414 }
00415 }
00416 }
00417 }
00418
00419
00420
00421
00422
00423
00424
00425 printf("\nInit champs de la structure..."); fflush(stdout);
00426
00427
00428
00429
00430
00431 learn->input_ref.nb_pts = desc_format->dataIattributs.nb_pts - desc_format->classes[indice_c_max].nb_pts;
00432 learn->output_ref.nb_pts = learn->input_ref.nb_pts;
00433
00434
00435 learn->input_ref.nb_attr = desc_format->dataIattributs.nb_attr;
00436
00437
00438 desc_format->dataOmasque.nb_class = learn->output_ref.nb_class;
00439
00440
00441 learn->output_ref.nb_class -= 1;
00442
00443
00444 learn->output_ref.type = CRISP_CHOICE;
00445
00446 printf("Ok\n");
00447
00448
00449
00450
00451
00452
00453 printf("Allocations datainput et dataoutput...");fflush(stdout);
00454
00455
00456 alloc_classifier_data_input_completely(&learn->input_ref);
00457
00458
00459 alloc_classifier_data_output(&learn->output_ref);
00460
00461
00462 printf("\n\n--------------- RAPPORT DES LEARNING DATA ------------------- \n");
00463
00464
00465 printf("\nDonnees initiales : %d points et %d attributs\n",
00466 desc_format->dataIattributs.nb_pts,desc_format->dataIattributs.nb_attr);
00467
00468
00469 printf("\n%d classes ont ete detectees :\n",learn->output_ref.nb_class);
00470
00471
00472
00473 for(i=0;i<learn->output_ref.nb_class + 1;i++)
00474 printf("Le label \t %d \t contient \t %d points.\n",
00475 desc_format->classes[i].label,desc_format->classes[i].nb_pts);
00476
00477
00478 printf("\nLa classe avec le label %d a ete supprimee des donnees (fond).\n",
00479 desc_format->classes[indice_c_max].label );
00480
00481
00482 printf("\nIl y a %d points d'apprentissage soit %2.2f/100 des points initiaux\n\n",
00483 learn->input_ref.nb_pts,
00484 100.0*(float)learn->input_ref.nb_pts/ (float)(desc_format->dataIattributs.nb_pts));
00485
00486 printf("------------------------------------------------------------------------------------- \n");
00487
00488 return NO_ERROR_FORMAT_LSFI1;
00489 }
00490
00491
00492
00493
00494
00495
00506 int format_learning_set_from_imau1_calc(
00507 format_learning_set_from_imau1_t *desc_format,
00508 learning_set *learn
00509 )
00510 {
00511
00512 int i,j,k;
00513
00514
00515
00516
00517
00518
00519
00520
00521 printf("\nRemplissage de la structure learning set avec les donnees...");fflush(stdout);
00522
00523
00524
00525
00526 j=0;
00527 for(i=0;i<desc_format->dataIattributs.nb_pts;i++)
00528 {
00529 if(desc_format->dataOmasque.classes.crisp[i] != desc_format->c_max)
00530 {
00531
00532
00533 learn->output_ref.classes.crisp[j] = desc_format->dataOmasque.classes.crisp[i];
00534
00535 for(k=0;k<learn->input_ref.nb_attr;k++)
00536
00537 learn->input_ref.attributes[k][j] = desc_format->dataIattributs.attributes[k][i];
00538
00539 j++;
00540 }
00541 }
00542
00543
00544 learn->input_ref.equ_nb_rows = 0;
00545 learn->input_ref.equ_nb_cols = 0;
00546 learn->output_ref.equ_nb_rows = 0;
00547 learn->output_ref.equ_nb_cols = 0;
00548 learn->input_ref.begin = 0;
00549 learn->input_ref.end = 0;
00550
00551 printf("ok\n");
00552
00553 return NO_ERROR_FORMAT_LSFI1;
00554
00555 }
00556
00557
00558
00559
00560 int format_learning_set_from_imau1_ferm(
00561 format_learning_set_from_imau1_t *desc_format,
00562 learning_set learn
00563 )
00564 {
00565 printf("\nLiberation de la memoire allouee pour le descripteur de learning set...");
00566 free(desc_format->classes);
00567 free_data_input(&desc_format->dataIattributs);
00568 free_data_output(&desc_format->dataOmasque);
00569 printf("ok\n");
00570
00571 printf("Liberation de la memoire allouee pour la structure de learning set...");
00572 free_data_input(&learn.input_ref);
00573 free_data_output(&learn.output_ref);
00574 printf("ok\n");
00575
00576 return NO_ERROR_FORMAT_LSFI1;
00577 }
00578
00579
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00626
00627
00628
00629
00630
00648 param *format_imau1_classifier_crisp_output_lect(
00649 format_imau1_classifier_crisp_output_t *desc,
00650 param *ptp,
00651 char *debq
00652 )
00653 {
00654 char question[500];
00655 int choix;
00656
00657 sprintf(question, "%s format output : prendre image entiere (0) ou partielle (1)", debq);
00658 lec_param(question, ptp);
00659 desc->choix = atoi(ptp->rep);
00660 ptp = ptp->next;
00661
00662
00663 switch(desc->choix)
00664 {
00665
00666 case IMAGE_PARTIELLE :
00667
00668 sprintf(question, "%s Coordonnées X du premier point (colonne : 0 a nb colonnes - 1)", debq);
00669 lec_param(question, ptp);
00670 desc->premier_point.Xcol = atoi(ptp->rep);
00671 ptp = ptp->next;
00672
00673 sprintf(question, "%s Coordonnées Y du premier point (ligne : 0 a nb lignes - 1)", debq);
00674 lec_param(question, ptp);
00675 desc->premier_point.Yrow = atoi(ptp->rep);
00676 ptp = ptp->next;
00677
00678 sprintf(question, "%s Coordonnées X deuxieme point (colonne : 0 a nb colonnes - 1)", debq);
00679 lec_param(question, ptp);
00680 desc->deuxieme_point.Xcol = atoi(ptp->rep);
00681 ptp = ptp->next;
00682
00683 sprintf(question, "%s Coordonnées Y deuxieme point (ligne : 0 a nb lignes - 1) ", debq);
00684 lec_param(question, ptp);
00685 desc->deuxieme_point.Yrow = atoi(ptp->rep);
00686 ptp = ptp->next;
00687
00688 break;
00689
00690
00691
00692 case IMAGE_ENTIERE : break;
00693
00694
00695 default :
00696 printf("\nMauvaise reponse\n");
00697 exit(1);
00698
00699 }
00700
00701 return ptp;
00702 }
00703
00704
00705
00706
00707
00728 int format_imau1_classifier_crisp_output_init(
00729 format_imau1_classifier_crisp_output_t *desc,
00730 imau1 image_u1,
00731 data_output *dataO
00732 )
00733 {
00734 printf("\n\nInit format crisp output...");
00735 switch(desc->choix)
00736 {
00737
00738 case IMAGE_ENTIERE :
00739 desc->premier_point.Xcol = 0;
00740 desc->premier_point.Yrow = 0;
00741 desc->deuxieme_point.Xcol = image_u1.nc - 1;
00742 desc->deuxieme_point.Yrow = image_u1.nr - 1;
00743 break;
00744
00745
00746 case IMAGE_PARTIELLE :
00747 if(!(desc->premier_point.Xcol >= 0 && desc->premier_point.Xcol < image_u1.nc &&
00748 desc->premier_point.Yrow >= 0 && desc->premier_point.Yrow < image_u1.nr &&
00749 desc->deuxieme_point.Xcol >= 0 && desc->deuxieme_point.Xcol < image_u1.nc &&
00750 desc->deuxieme_point.Yrow >= 0 && desc->deuxieme_point.Yrow < image_u1.nr &&
00751 desc->deuxieme_point.Xcol - desc->premier_point.Xcol >= 0 &&
00752 desc->deuxieme_point.Yrow - desc->premier_point.Yrow >= 0))
00753 {
00754 printf("\nErreur dimension - coordonnees points invalides\n");
00755 printf("desc->premier_point.Xcol =%d \n",desc->premier_point.Xcol);
00756 printf("desc->premier_point.Yrow =%d\n",desc->premier_point.Yrow);
00757 printf("image_u1.nr =%d\n",image_u1.nr);
00758 printf("image_u1.nc =%d\n",image_u1.nc);
00759 printf("desc->deuxieme_point.Xcol = %d\n",desc->deuxieme_point.Xcol);
00760 printf("desc->deuxieme_point.Yrow =%d\n ",desc->deuxieme_point.Yrow );
00761 exit(1);
00762 }
00763
00764 break;
00765
00766 default :
00767 printf("\nErreur dans init : choix non valide\n");
00768 exit(1);
00769 }
00770
00771
00772 dataO->type = CRISP_CHOICE;
00773
00774
00775 dataO->equ_nb_cols = desc->deuxieme_point.Xcol - desc->premier_point.Xcol + 1;
00776 dataO->equ_nb_rows = desc->deuxieme_point.Yrow - desc->premier_point.Yrow + 1;
00777
00778
00779
00780 dataO->nb_pts = dataO->equ_nb_cols*dataO->equ_nb_rows;
00781
00782
00783 dataO->classes.crisp = NULL;
00784
00785 printf("OK.\n\n");
00786 return 0;
00787 }
00788
00789
00790
00791
00792
00807 int format_imau1_classifier_crisp_output_calc(
00808 format_imau1_classifier_crisp_output_t *desc,
00809 imau1 image_u1,
00810 data_output *dataO
00811 )
00812 {
00813 int i,j,k;
00814 printf("\nCalcul format crisp_output...");
00815
00816
00817 alloc_classifier_data_output(dataO);
00818
00819
00820 k=0;
00821 for(i=0;i<dataO->equ_nb_rows;i++)
00822 for(j=0;j<dataO->equ_nb_cols;j++)
00823 {
00824 dataO->classes.crisp[k] =
00825 (int)(image_u1.p[desc->premier_point.Yrow+i][desc->premier_point.Xcol+j]);
00826 k++;
00827 }
00828
00829 printf("OK\n");
00830
00831 return 0;
00832 }
00833
00835
00836
00837
00838
00839
00840