lhisto.c

00001 /*
00002 *    Copyright (c) 2007. The BATI team. All right reserved.
00003 *
00004 *    This file is part of BATI library.
00005 *
00006 *    BATI library is free software: you can redistribute it and/or modify
00007 *    it under the terms of the GNU General Public License as published by
00008 *    the Free Software Foundation, either version 3 of the License, or
00009 *    (at your option) any later version.
00010 *
00011 *    BATI library  is distributed in the hope that it will be useful,
00012 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *    GNU General Public License for more details.
00015 *
00016 *    You should have received a copy of the GNU General Public License
00017 *    along with BATI library.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 /* **************************** lhisto.c ******************************/
00020 /*                                                                    */
00021 /* ********************************************************************/
00022 
00061 #include "image.h"
00062 #include "proto2D.h"
00063 #include "histo.h"
00064 #include <stdlib.h>
00065 #include <limits.h>
00066 #include <string.h>
00067 
00068 /* *************************  LECTURE  *******************************/
00079 param *histo_lect(histo_t *des, param *ptp, char *debq){
00080   char question[500];
00081   int l, j;
00082 
00083   sprintf(question, "%s nom complet du fichier ASCII resultat", debq);
00084   lec_param(question, ptp);
00085   strcpy(des->nomhist, ptp->rep);
00086   ptp = ptp->next;
00087 
00088   sprintf(question, "%s valeur minimale ([0, 0] : auto)", debq);
00089   lec_param(question, ptp);
00090   des->min = atoi(ptp->rep);
00091   ptp = ptp->next;
00092 
00093   sprintf(question, "%s valeur maximale ([0, 0] : auto)", debq);
00094   lec_param(question, ptp);
00095   des->max = atoi(ptp->rep);
00096   ptp = ptp->next; 
00097 
00098   sprintf(question, "%s nombre de cases (0 : auto)", debq);
00099   lec_param(question, ptp);
00100   des->bins = atoi(ptp->rep);
00101   ptp = ptp->next;  
00102 
00103   sprintf(question, "%s niveau de gris de la region definie dans un masque (-1 => toute l'image)", debq);
00104   lec_param(question, ptp);
00105   des->ngm = atoi(ptp->rep);
00106   ptp = ptp->next;  
00107 
00108   return(ptp);
00109 }
00110 
00111 /* *************************  INITIALISATION  ***************************/
00123 int histofl_init(histo_t *des, imafl im0, imau1 mas){
00124   int i, j;
00125   float min, max, step;
00126   pixu1 ngmu1;
00127 
00128   if( (des->pfd=fopen(des->nomhist,"w")) == NULL ){
00129       printf("\n>> ERREUR histofl_init: creation fichier %s impossible\n",des->nomhist);
00130       exit(1);
00131   }
00132 
00133   if( des->bins == 0)
00134       des->bins = 1000;
00135   if(  (des->h = (unsigned int *)malloc(des->bins*sizeof(unsigned int))) == NULL){
00136       printf ("\n ERREUR histofl_init : allocation histogramme impossible\n");  
00137       exit(1); 
00138   }  
00139   for(j = 0; j < des->bins; j++)
00140       des->h[j] = 0;
00141 
00142   if((des->max == 0.0) && (des->min == 0.0)){
00143       ngmu1 = (pixu1)des->ngm;
00144       des->max = im0.p[0][0];
00145       des->min = im0.p[0][0];
00146 
00147       if(des->ngm < 0){
00148           for(j = 0; j<im0.nr; j++)
00149               for(i = 0; i<im0.nc; i++){
00150                   if( im0.p[j][i] >= des->max )
00151                       des->max = im0.p[j][i];
00152                   if( im0.p[j][i] <= des->min )
00153                       des->min = im0.p[j][i];
00154               }
00155       }
00156       else{
00157           for(j = 0; j<im0.nr; j++)
00158               for(i = 0; i<im0.nc; i++){
00159                   if( mas.p[j][i] == ngmu1 ){
00160                       if( im0.p[j][i] >= des->max )
00161                           des->max = im0.p[j][i];
00162                       if( im0.p[j][i] <= des->min )
00163                           des->min = im0.p[j][i];
00164                   }
00165               }
00166           }
00167   }
00168   printf("\n histofl_init : histogramme sur [%f,%f], %d cases\n", 
00169          des->min, des->max, des->bins);
00170 
00171   des->step = (des->max - des->min)/(des->bins);
00172 
00173   return(0);      
00174 }
00175 /* *************************  CALCUL  ***************************/
00186 int histofl_calc(histo_t *des, imafl im0, imau1 mas){ 
00187   int i, j;
00188   int tmp;
00189   float tmpfl;
00190   pixu1 ngmu1;
00191 
00192   if(des->ngm < 0){
00193       for(j=0; j<im0.nr; j++)
00194           for(i=0; i<im0.nc; i++){
00195               tmpfl = (im0.p[j][i]-des->min)/des->step;
00196               tmp = (int) tmpfl;
00197               if ((tmp < des->bins) && (tmp >= 0))
00198                   des->h[tmp]++;
00199               else if (tmp >= des->bins) 
00200                   des->h[des->bins-1]++;
00201               else
00202                   des->h[0]++;
00203           }
00204   }
00205   else{
00206       ngmu1 = (pixu1)des->ngm;
00207       for(j=0; j<im0.nr; j++)
00208           for(i=0; i<im0.nc; i++)
00209               if(mas.p[j][i] == ngmu1){
00210                   tmpfl = (im0.p[j][i]-des->min)/des->step;
00211                   tmp = (int) tmpfl;
00212                   if ((tmp < des->bins) && (tmp >= 0))
00213                       des->h[tmp]++;
00214                   else if (tmp >= des->bins) 
00215                       des->h[des->bins-1]++;
00216                   else
00217                       des->h[0]++;
00218           }
00219 
00220   }
00221 }
00222 
00223 /* *************************  INITIALISATION  ***************************/
00224 /* XX = u1 : initialisation de l'operateur histogramme pour images 8 bit.\\
00225     @name histou1_init 
00226     @param des  pointeur descripteur
00227     @param im0  image initiale (taille connue)
00228     @author F. Bujor
00229     @version 1.0 (26/04/00); Include : image.h, proto2D.h, histo.h
00230 */
00231 
00232 int histou1_init(histo_t *des, imau1 im0, imau1 mas){
00233   int i, j;
00234   float min, max, step;
00235 
00236   if( (des->pfd=fopen(des->nomhist,"w")) == NULL ){
00237       printf("\n>> ERREUR histou1_init: creation fichier %s impossible\n",des->nomhist);
00238       exit(1);
00239   }
00240 
00241   if( des->bins == 0)
00242       des->bins = 256;
00243   if(  (des->h = (unsigned int *)malloc(des->bins*sizeof(unsigned int))) == NULL)  
00244         {printf ("\n ERREUR histou1_init : allocation histogramme impossible\n");  exit(1); }  
00245   for(j = 0; j < des->bins; j++)
00246       des->h[j] = 0;
00247 
00248   if((des->max == 0.0) && (des->min == 0.0)){
00249       des->max = 256;
00250       des->min = 0;
00251   }
00252   else{
00253       if(des->max > 256){
00254           printf("\n ATTENTION histou1_init : valeur maximale corrigee a 256 \n");
00255           des->max = 256;
00256       }
00257   
00258       if(des->min < 0){
00259           printf("\n ATTENTION histou1_init : valeur minimale corrigee a 0 \n");
00260           des->min =0;
00261       }
00262   }
00263   printf("\n histou1_init : histogramme sur [%f,%f], %d cases\n", 
00264          des->min, des->max, des->bins);
00265 
00266   des->step = (des->max-des->min)/(des->bins);
00267 
00268   return(0);      
00269 }
00270 /* *************************  CALCUL  ***************************/
00271 /* XX = u1 : calcul de l'operateur histogramme pour images 8 bit.\\
00272     @name histou1_calc  
00273     @param des  pointeur descripteur
00274     @param im0  image initiale 
00275     @author F. Bujor
00276     @version 1.0 (26/04/00); Include : image.h, proto2D.h, histo.h
00277 */
00278 
00279 int histou1_calc(histo_t *des, imau1 im0, imau1 mas){ 
00280   int i, j;
00281   int tmp; 
00282   float tmpfl;
00283   pixu1 ngmu1;
00284 
00285   if(des->ngm < 0){
00286       for(j=0; j<im0.nr; j++)
00287           for(i=0; i<im0.nc; i++){
00288               tmpfl = (im0.p[j][i]-des->min)/des->step;
00289               tmp = (int) tmpfl;
00290               if ((tmp < des->bins) && (tmp >= 0))
00291                   des->h[tmp]++;
00292               else if (tmp >= des->bins) 
00293                   des->h[des->bins-1]++;
00294               else
00295                   des->h[0]++;
00296           }
00297   }
00298   else{
00299       ngmu1 = (pixu1)des->ngm;
00300       for(j=0; j<im0.nr; j++)
00301           for(i=0; i<im0.nc; i++)
00302               if(mas.p[j][i] == ngmu1){
00303                   tmpfl = (im0.p[j][i]-des->min)/des->step;
00304                   tmp = (int) tmpfl;
00305                   if ((tmp < des->bins) && (tmp >= 0))
00306                       des->h[tmp]++;
00307                   else if (tmp >= des->bins) 
00308                       des->h[des->bins-1]++;
00309                   else
00310                       des->h[0]++;
00311           }
00312   }
00313 }
00314 
00315 /* *************************  INITIALISATION  ***************************/
00316 /* 
00317    XX = u2 : initialisation de l'operateur histogramme pour images 16 bit.\\
00318     @name histou2_init 
00319     @param des  pointeur descripteur
00320     @param im0  image initiale (taille connue)
00321     @author F. Bujor
00322     @version 1.0 (26/04/00); Include : image.h, proto2D.h, histo.h
00323 */
00324 int histou2_init(histo_t *des, imau2 im0, imau1 mas){
00325   int i, j;
00326   float min, max, step;
00327 
00328   if( (des->pfd=fopen(des->nomhist,"w")) == NULL ){
00329       printf("\n>> ERREUR histou2_init: creation fichier %s impossible\n",des->nomhist);
00330       exit(1);
00331   }
00332 
00333   if( des->bins == 0)
00334       des->bins = 65536;
00335   if( (des->h = (unsigned int *)malloc(des->bins*sizeof(unsigned int))) == NULL ){
00336       printf("\n ERREUR histou2_init : allocation histogramme impossible\n");
00337       exit(1); 
00338   }  
00339   for(j = 0; j < des->bins; j++)
00340       des->h[j] = 0;
00341 
00342   if((des->max == 0.0) && (des->min == 0.0)){
00343       des->max = 65536;
00344       des->min = 0;
00345   }
00346   else{
00347       if(des->max > 65536){
00348           printf("\n ATTENTION histou2_init : valeur maximale corrigee a 65536 \n");
00349           des->max = 65536;
00350       }
00351 
00352       if(des->min < 0){
00353           printf("\n ATTENTION histou2_init : valeur minimale corrigee a 0 \n"); 
00354           des->min =0;
00355       }
00356   }
00357   printf("\n histou2_init : histogramme sur [%f,%f], %d cases\n", 
00358          des->min, des->max, des->bins);
00359   des->step = (des->max-des->min)/(des->bins);
00360 
00361   return(0);      
00362 }
00363 /* *************************  CALCUL  ***************************/
00364 /* XX = u2 : calcul de l'operateur histogramme pour images 16 bit.\\
00365     @name histou2_calc  
00366     @param des  pointeur descripteur
00367     @param im0  image initiale 
00368     @author F. Bujor
00369     @version 1.0 (26/04/00); Include : image.h, proto2D.h, histo.h
00370 */
00371 
00372 int histou2_calc(histo_t *des, imau2 im0, imau1 mas){ 
00373   int i, j;
00374   int tmp; 
00375   float tmpfl;
00376   pixu1 ngmu1;
00377 
00378   if(des->ngm < 0){
00379       for(j=0; j<im0.nr; j++)
00380           for(i=0; i<im0.nc; i++){
00381               tmpfl = (im0.p[j][i]-des->min)/des->step;
00382               tmp = (int) tmpfl;
00383               if ((tmp < des->bins) && (tmp >= 0))
00384                   des->h[tmp]++;
00385               else if (tmp >= des->bins) 
00386                   des->h[des->bins-1]++;
00387               else
00388                   des->h[0]++;
00389           }
00390   }
00391   else{
00392       ngmu1 = (pixu1)des->ngm;
00393       for(j=0; j<im0.nr; j++)
00394           for(i=0; i<im0.nc; i++)
00395               if(mas.p[j][i] == ngmu1){
00396                   tmpfl = (im0.p[j][i]-des->min)/des->step;
00397                   tmp = (int) tmpfl;
00398                   if ((tmp < des->bins) && (tmp >= 0))
00399                       des->h[tmp]++;
00400                   else if (tmp >= des->bins) 
00401                       des->h[des->bins-1]++;
00402                   else
00403                       des->h[0]++;
00404               }
00405   }
00406 }
00407 
00408 /* *************************  FERMETURE  ***************************/
00415 int histo_ferm(histo_t *des){
00416      int j;
00417      
00418      for(j = 0; j < des->bins; j++)
00419           if( fprintf(des->pfd, "%f \t %12d\n", des->min+j*des->step, des->h[j]) == 0){
00420              printf("\n>> ERREUR histo_ferm : ecriture de fichier %s\n", des->nomhist);
00421              exit(1);
00422          }
00423      close(des->pfd); 
00424      return(0);
00425 }
00426   

Generated on Tue Apr 22 13:31:04 2008 for ima2D by  doxygen 1.5.3