lconv16to8.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 */
00040 #include "image.h"
00041 #include "proto2D.h"
00042 #include "radar.h" 
00043 #include <stdlib.h>
00044 #include <fcntl.h>
00045 #include <math.h>
00046 #include <limits.h> /* pour  USHRT_MAX ... */
00047 
00048 
00049 /* **********************  LECTURE DE PARAMETRES   *************************/
00063 param * conv16to8_lect(conv16to8_t *des, param *ptp, char *debq){
00064   char question[500];
00065 
00066   sprintf(question, "%s methode de conversion : 1=[min,moy+K*sigma], 2=[n(K%%), n(100-k%%)], 3=[0, K]", 
00067           debq); 
00068   lec_param(question, ptp);
00069   des->methode = atoi(ptp->rep);
00070   ptp = ptp->next; 
00071   if(  des->methode < 1 || des->methode > 3 ){
00072       printf("\n>> ERREUR conv16to8_lect : methode %d inconnue \n", des->methode);
00073       exit(1);
00074   }
00075 
00076   sprintf(question, "%s valeur du parametre K", debq); 
00077   lec_param(question, ptp);
00078   des->K = (float)atof(ptp->rep);
00079   ptp = ptp->next;
00080 
00081   return(ptp);
00082 }
00083 
00084 
00085 /* **********************  INITIALISATION  *****************************/
00097 /* initialisation (16 bit)*/
00098 int conv16to8_init(conv16to8_t *des, imau2 im0, imau1 *imres){
00099   int n;
00100 
00101   /* image resultat */ 
00102   imres->nc = im0.nc;
00103   imres->nr = im0.nr;
00104   sprintf(imres->nom, "%s(conv16to8m%d)", im0.nom, des->methode);
00105   alloc_imau1(imres);
00106 
00107   if(des->methode == 1 || des->methode == 2){
00108       for(n=0; n<65536; n++)
00109           des->his[n] = 0;
00110   }
00111 
00112   return(0);  
00113 }
00114 
00115 /* *************************  CALCUL  ***************************/
00126 /* calcul (16 bit) */
00127 int conv16to8_calc(conv16to8_t *des, imau2 im0, imau1 *imres){
00128   int i, j, n, nbp, min,  val, minlut, maxlut;
00129   float som, som2, moy, sigma, sigma2, a, b, tmpfl;
00130 
00131   if(des->methode == 1 || des->methode == 2){  
00132       min = im0.p[0][0];
00133       for(j=0; j<im0.nr; j++)
00134           for(i=0; i<im0.nc; i++){
00135               val = (int)im0.p[j][i];
00136               des->his[val]++;
00137               if(val < min)
00138                   min = val;
00139           }
00140   }
00141   nbp = im0.nc * im0.nr;
00142   switch(des->methode){
00143   case 1 :
00144       som = 0;
00145       som2 =0;
00146       for(n=1; n<65536; n++){
00147           som += (float)n*(float)des->his[n];
00148           som2 += (float)n*(float)n*(float)des->his[n];
00149       }
00150       moy = som/nbp;
00151       sigma2 = (som2 - nbp*moy*moy)/(nbp-1);
00152       sigma = (float)sqrt((double)sigma2);
00153       minlut = min;
00154       tmpfl = moy + des->K * sigma + 0.5;
00155       maxlut = (int)tmpfl;
00156       sprintf(des->legende, "conversion lineaire sur [min:%d, moy(%.2f) + %.2f * sigma(%.2f) = %d]\n", 
00157              minlut, moy, des->K, sigma, maxlut);
00158       break;
00159   case 2 :
00160       tmpfl = des->K * nbp / 100. + 0.5;
00161       n=0; 
00162       som = 0;
00163       do{
00164           som += des->his[n];
00165           n++;
00166       }while(som < tmpfl);
00167       minlut = n;
00168       n=65535; 
00169       som = 0;
00170       do{
00171           som += des->his[n];
00172           n--;
00173       }while(som < tmpfl);
00174       maxlut = n;
00175       sprintf(des->legende, "conversion lineaire sur [n(%.2f%%):%d, n(100-%.2f%%):%d]\n", 
00176              des->K, minlut, des->K, maxlut);
00177       break;
00178   case 3 :
00179       minlut = 0;
00180       maxlut = (int)des->K;
00181       sprintf(des->legende, "conversion lineaire sur [%d, %d]\n",  minlut, maxlut);
00182       break;
00183   }
00184   printf("\n>>conv16to8 : %s\n",  des->legende);
00185 
00186   /* lut */    
00187   a = 255.9999/(double)(maxlut-minlut);
00188   b = -a*(double)minlut;
00189   for(n=0; n<65536; n++)
00190           if( n <= minlut )
00191               des->lut[n] = 0;
00192           else if( n >= maxlut )
00193               des->lut[n] = 255;
00194           else{
00195               tmpfl = a*(float)n + b + 0.5;
00196               des->lut[n] = (pixu1)tmpfl;
00197           }
00198   /* conversion */    
00199   for(j=0; j<im0.nr; j++)
00200       for(i=0; i<im0.nc; i++)
00201           imres->p[j][i] = des->lut[im0.p[j][i]];
00202 
00203 
00204   return(0);
00205 }
00206 
00207 
00208 

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