vect_ima.c

Go to the documentation of this file.
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 
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include "image.h"
00023 #include "proto2D.h"
00024 #include "volume.h"
00025 #include "vect_ima.h"
00026 #include <string.h>
00027 
00028 
00029 
00095 int getev(allEigenvectors * result,char *filename)
00096 {
00097   FILE * fattr;
00098   dimensions * dim;  
00099   if ((fattr = fopen(filename,"r"))==NULL){
00100    printf("file %s not found\n",filename);
00101    exit(3);
00102   }   
00103   result->dim=(dimensions *) malloc(sizeof(dimensions));
00104   fread(result->dim ,sizeof(dimensions),1,fattr);
00105   result->data=(eigenvector *)malloc(result->dim->dimx * result->dim->dimy* result->dim->dimz * sizeof(eigenvector));
00106   fread(result->data ,sizeof(eigenvector),result->dim->dimx * result->dim->dimy* result->dim->dimz,fattr);
00107   close(fattr);
00108   return(0);  
00109 }
00110 
00111 short round_perso(float in)
00112 {
00113   float min, max;
00114   max=floor(in);
00115   min=ceil(in);
00116   if (in > ((min + max)/2))
00117     return (short) min;
00118   else
00119     return (short) max;
00120   
00121 }
00122 short getDiff(float * vect,short size,short * diffX,short * diffY,short *diffZ)
00123      /* returns the differnet cooridinates between the endpoint and the strarting point of the vector in the parameters the return value is the value with the largest difference posible values: 0,1,2 */
00124 
00125 {
00126   float fakt;
00127   short val;
00128  
00129   if (vect[0]==vect[1]&&vect[1]==vect[2]&&vect[0]==(float) 0)
00130     return 99;
00131   if ((fabs(vect[0])>=fabs(vect[1]))&&(fabs(vect[0])>=fabs(vect[2]))){
00132     fakt=size/fabs(vect[0]);
00133     val=0;   
00134   }
00135   else if ((fabs(vect[1])>=fabs(vect[0]))&&(fabs(vect[1])>=fabs(vect[2]))){
00136     fakt=size/fabs(vect[1]);
00137     val =1;
00138   }
00139   else if ((fabs(vect[2])>=fabs(vect[0]))&&(fabs(vect[2])>=fabs(vect[1]))) {
00140     fakt=size/fabs(vect[2]);
00141     val=2;
00142   }
00143   vect[0]*=fakt;
00144   vect[1]*=fakt;
00145   vect[2]*=fakt;
00146   
00147   *diffX=round_perso(vect[0]);
00148   *diffY=round_perso(vect[1]);
00149   *diffZ=round_perso(vect[2]); 
00150   return val;
00151 }
00152 
00153 
00154 short operatoru1print(ima3Du1* image,short x, short y, short z,short diffX,short diffY,short diffZ,short flag)
00155      /* flag: 0: xyz (the correct order)
00156               1: yxz
00157               2: zxy */
00158      /* traces the vector in the Original image */
00159         
00160 {
00161   short switch1=2*fabs(diffY)-fabs(diffX),switch2=2*fabs(diffZ)-fabs(diffX);
00162   short inx1=2*fabs(diffY), inx2=2*fabs(diffZ), inxy=2*(fabs(diffY)-fabs(diffX)), inxz=2*(fabs(diffZ)-fabs(diffX));
00163   short  min=255 ,max=-255, stopX=x+diffX, stopY=y+diffY, stopZ=z+diffZ,data,pox=x,poy=y,poz=z,count=0;
00164   int sum=0;
00165  
00166   /* wenn der Ausganspunkt in der Mitte liegt wird erst in eine Rtg gegangen und dann in die entgegengesetzte, deshalb gibt es neben den eigentlichen x,y,z Zaehlern noch entgegengesetze */
00167   if (flag==0){     
00168        image->data[z][y][x]=0; image->data[poz][poy][pox]=0;
00169        }
00170   else if (flag==1){       
00171        image->data[z][x][y]=0; image->data[poz][pox][poy]=0; 
00172        }
00173   else {      
00174        image->data[x][z][y]=0; image->data[pox][poz][poy]=0; 
00175        } 
00176   while  (x!=stopX)  {         
00177      sum+=data;
00178      if (diffX<0){
00179        x--;
00180        pox++;
00181      }
00182      else{
00183        x++;
00184        pox--;
00185      }
00186      if (switch1>0){
00187        if (diffY<0){
00188          poy++;
00189          y--;
00190        }
00191        else{
00192          y++;
00193          poy--;
00194        }
00195        switch1+=inxy;  
00196      }
00197      else
00198        switch1+=inx1;
00199      if (switch2>0){
00200        if (diffZ<0){
00201         z--;
00202         poz++;
00203        }
00204        else{
00205          z++;
00206          poz--;
00207        }
00208        switch2+=inxz;  
00209      }
00210      else
00211        switch2+=inx2;
00212      if (flag==0){     
00213        image->data[z][y][x]=0; image->data[poz][poy][pox]=0;
00214        }
00215      else if (flag==1){       
00216          image->data[z][x][y]=0; image->data[poz][pox][poy]=0; 
00217          }
00218      else        {      
00219          image->data[x][z][y]=0; image->data[pox][poz][poy]=0; 
00220          }
00221          }
00222   return sum/fabs(diffX+1);
00223 }
00224 
00225 short operators2print(ima3Ds2 * image,short x, short y, short z,short diffX,short diffY,short diffZ,short flag,short greylevel)
00226      /* flag: 0: xyz (the correct order)
00227               1: yxz
00228               2: zxy */
00229      /* traces the vector in the Original image */
00230         
00231 {
00232   short switch1=2*fabs(diffY)-fabs(diffX),switch2=2*fabs(diffZ)-fabs(diffX);
00233   short inx1=2*fabs(diffY), inx2=2*fabs(diffZ), inxy=2*(fabs(diffY)-fabs(diffX)), inxz=2*(fabs(diffZ)-fabs(diffX));
00234   short  min=255 ,max=-255, stopX=x+diffX, stopY=y+diffY, stopZ=z+diffZ,data,count=0,pox=x,poy=y,poz=z;
00235   int sum=0;
00236   /* takes the input point as middle of the window and traces a line through this piont in every direction */
00237   /* wenn der Ausganspunkt in der Mitte liegt wird erst in eine Rtg gegangen und dann in die entgegengesetzte, deshalb gibt es neben den eigentlichen x,y,z Zaehlern noch entgegengesetze */
00238    if (flag==0){     
00239        image->data[z][y][x]=greylevel;
00240        }
00241    else if (flag==1){       
00242          image->data[z][x][y]=greylevel;
00243          }
00244    else  {      
00245          image->data[x][z][y]=greylevel;
00246          }
00247   while  (x!=stopX)  {         
00248     
00249      sum+=data;
00250      if (diffX<0){
00251        x--;
00252        pox++;
00253      }
00254      else{
00255        x++;
00256        pox--;
00257      }
00258      if (switch1>0){
00259        if (diffY<0){
00260          poy++;
00261          y--;
00262        }
00263        else{
00264          y++;
00265          poy--;
00266        }
00267        switch1+=inxy;  
00268      }
00269      else
00270        switch1+=inx1;
00271      if (switch2>0){
00272        if (diffZ<0){
00273         z--;
00274         poz++;
00275        }
00276        else{
00277          z++;
00278          poz--;
00279        }
00280        switch2+=inxz;  
00281      }
00282      else
00283        switch2+=inx2;
00284      if (flag==0){     
00285        image->data[z][y][x]=greylevel;image->data[poz][poy][pox]=greylevel;
00286        }
00287      else if (flag==1){       
00288          image->data[z][x][y]=greylevel;  image->data[poz][pox][poy]=greylevel; 
00289          }
00290      else        {      
00291          image->data[x][z][y]=greylevel;  image->data[pox][poz][poy]=greylevel;
00292          }
00293          } 
00294   if (flag==0){     
00295        image->data[z+diffZ][y+diffY][x+diffX]=greylevel;
00296        }
00297    else if (flag==1){       
00298          image->data[z+diffZ][x+diffX][y+diffY]=greylevel;
00299          }
00300    else  {      
00301          image->data[x+diffX][z+diffZ][y+diffY]=greylevel;
00302          }
00303   return 0;
00304 }
00305 short means2(ima3Ds2 * image,short x, short y, short z,short diffX,short diffY,short diffZ,short flag)
00306      /* flag: 0: xyz (the correct order)
00307               1: yxz
00308               2: zxy */
00309      /*calculates the mean of all points on the line of the vector */
00310         
00311 {
00312   short switch1=2*fabs(diffY)-fabs(diffX),switch2=2*fabs(diffZ)-fabs(diffX);
00313   short inx1=2*fabs(diffY), inx2=2*fabs(diffZ), inxy=2*(fabs(diffY)-fabs(diffX)), inxz=2*(fabs(diffZ)-fabs(diffX));
00314   short  min=255 ,max=-255, stopX=x+diffX, stopY=y+diffY, stopZ=z+diffZ,data,count=0,pox=x,poy=y,poz=z;
00315   int sum=0;
00316  
00317   /* wenn der Ausganspunkt in der Mitte liegt wird erst in eine Rtg gegangen und dann in die entgegengesetzte, deshalb gibt es neben den eigentlichen x,y,z Zaehlern noch entgegengesetze */
00318   if (flag==0){     
00319          sum=image->data[z][y][x];
00320                }
00321   else if (flag==1){       
00322          sum=image->data[z][x][y]; 
00323   }
00324   else   {      
00325          sum=image->data[x][z][y]; 
00326   }
00327   while  (x!=stopX)  {                 
00328      if (diffX<0){
00329        x--;
00330        pox++;
00331      }
00332      else{
00333        x++;
00334        pox--;
00335      }
00336      if (switch1>0){
00337        if (diffY<0){
00338          poy++;
00339          y--;
00340        }
00341        else{
00342          y++;
00343          poy--;
00344        }
00345        switch1+=inxy;  
00346      }
00347      else
00348        switch1+=inx1;
00349      if (switch2>0){
00350        if (diffZ<0){
00351         z--;
00352         poz++;
00353        }
00354        else{
00355          z++;
00356          poz--;
00357        }
00358        switch2+=inxz;  
00359      }
00360      else
00361        switch2+=inx2;
00362      if (flag==0){     
00363          sum+=(image->data[z][y][x]+image->data[poz][poy][pox]);
00364      }
00365      else if (flag==1){       
00366          sum+=(image->data[z][x][y]+image->data[poz][pox][poy]); 
00367      }  
00368      else        {      
00369          sum+=(image->data[x][z][y]+image->data[pox][poz][poy]); 
00370      }
00371  }  
00372   /*if (((sum/(2*fabs(diffX)+1))>127.0)||((sum/(2*fabs(diffX)+1))<-128.0)){
00373     printf("sumfl %f, sum %d\n", (sum/(2*fabs(diffX)+1)),(short) (sum/(2*fabs(diffX))+1));
00374     printf("x: %d ,y: %d,z: %d flag: %d\n",x-diffX,y-diffY,z-diffZ,flag);
00375   }*/
00376  
00377   return (short) sum/(2*fabs(diffX)+1);
00378 }
00379 /* *************************  INITIALISATION  ***************************/
00380 /* \ingroup vector
00381     Initialisation. 
00382 
00383     @param des  descriptor pointer
00384   
00385     @author C. MAIER
00386     @version 1.0 (03/04/00); Include : vect_ima.h, filelib.h , volume.h
00387 
00388    
00389   
00390 */
00391 int vector_init(vector_t *des){
00392   return(0);
00393 }
00394 
00395 
00412 param *vector_lect(vector_t *des, param *ptp, char *debq){
00413 
00414   char question[500];
00415  
00416   sprintf(question, "%s nom complet du fichier image a lire",debq); 
00417   lec_param(question, ptp); 
00418   des->nom_in = ptp->rep;
00419   ptp = ptp->next;
00420 
00421   sprintf(question, "%s nom complet du fichier avec les vecteurs a lire", debq); 
00422   lec_param(question, ptp);  
00423   des->nom_vect = ptp->rep;
00424   ptp = ptp->next;
00425  
00426   sprintf(question, "%s nom complet du fichier image a ecrire", debq); 
00427   lec_param(question, ptp);
00428   des->nom_out = ptp->rep;
00429   ptp = ptp->next;
00430  
00431   sprintf(question, " rayon de la fenetre d'analyse"); 
00432   lec_param(question, ptp);
00433   des->size = atoi(ptp->rep);
00434   ptp = ptp->next; 
00435 
00436   sprintf(question, " modus (filtrage directionelle: 0, ou tracer les vectuers: 1)"); 
00437   lec_param(question, ptp); 
00438   des->dir = atoi(ptp->rep);
00439   ptp = ptp->next;
00440 
00441   sprintf(question, " intensity de la ligne (uniquement quand modus=1)"); 
00442   lec_param(question, ptp); 
00443   des->intensity = atoi(ptp->rep);
00444   ptp = ptp->next;
00445   
00446   return(ptp);
00447 }
00448 
00449 
00459 int vector_calc(vector_t *des)
00460  { 
00461   ima3Ds2 ima; 
00462   ima3Ds2 ima2; 
00463   ima3Ds2 *image=&ima;
00464   ima3Ds2 *imaout=&ima2;
00465   allEigenvectors  * result; 
00466   short dummy;
00467   short z,y,x,size=des->size;
00468   short diffX, diffY,diffZ,flag;
00469   long j;
00470   float vect[3];
00471   if ((des->dir!=0)&&(des->dir!=1)){
00472     printf("illegal Modus %d\n",des->dir);
00473     exit(3);
00474   }
00475   result=(allEigenvectors *)malloc( sizeof( allEigenvectors)); 
00476   lect_ima3Ds2(des->nom_in,image,ALL); 
00477   imaout->dimx=image->dimx;
00478   imaout->dimy=image->dimy;
00479   imaout->dimz=image->dimz; 
00480   allouer_ima3Ds2(imaout);
00481   getev(result,des->nom_vect); 
00482   if ((result->dim->dimz!=image->dimz)||(result->dim->dimy!=image->dimy)||(result->dim->dimx!=image->dimx)){
00483     printf("The X,Y or Z diminsions of the input data blocs are not cooresponding to each other!\n");
00484     exit(3);
00485   }
00486   z=-1;
00487   for (j=0;j<(result->dim->dimx*result->dim->dimy*result->dim->dimz);j++){
00488     if ((j%result->dim->dimx)==0){ 
00489       if ((j%(result->dim->dimx*result->dim->dimy))==0){
00490         z++;
00491         y=-1;   
00492       }
00493       y++;
00494       x=-1;     
00495     }
00496     x++;
00497     if ((x>size-1)&&(x<result->dim->dimx-size)&&(y>size-1)&&(y<result->dim->dimy-size)&&(z>size-1)&&(z<result->dim->dimz-size)) {  
00498      if (des->dir==1){
00499       /*tracing a line along the given vector */         
00500       if (((z%(2*size+1))==0)&&((y%(2*size+1))==0)&&((x%(2*size+1))==0)) {  
00501          vect[0]=result->data[j].element1;
00502          vect[1]=result->data[j].element2;
00503          vect[2]=result->data[j].element3;
00504          flag=getDiff(vect, size, &diffX, &diffY, &diffZ);    
00505          if (flag==0)
00506             dummy=operators2print(imaout,x,y,z,diffX,diffY,diffZ,flag,des->intensity);
00507          else if (flag==1)         
00508             dummy=operators2print(imaout,y,x,z,diffY,diffX,diffZ,flag,des->intensity); 
00509          else if (flag==2)
00510             dummy=operators2print(imaout,z,x,y,diffZ,diffX,diffY,flag,des->intensity);
00511       }
00512       else
00513            imaout->data[z][y][x]=image->data[z][y][x];          
00514       }
00515      else{
00516         vect[0]=result->data[j].element1;
00517         vect[1]=result->data[j].element2;
00518         vect[2]=result->data[j].element3;
00519         flag=getDiff(vect, size, &diffX, &diffY, &diffZ);    
00520         if (flag==0)
00521             imaout->data[z][y][x]=means2(image,x,y,z,diffX,diffY,diffZ,flag);
00522         else if (flag==1)          
00523             imaout->data[z][y][x]=means2(image,y,x,z,diffY,diffX,diffZ,flag); 
00524         else if (flag==2)
00525             imaout->data[z][y][x]=means2(image,z,x,y,diffZ,diffX,diffY,flag);
00526         else 
00527             imaout->data[z][y][x]=image->data[z][y][x];
00528       } 
00529      }
00530     else
00531            imaout->data[z][y][x]=image->data[z][y][x];   
00532     }        
00533                       
00534   ecr_ima3Ds2(des->nom_out,imaout); 
00535   liberer_ima3Ds2(imaout); 
00536   liberer_ima3Ds2(image);   
00537  }
00538 
00539 

Generated on Tue Apr 22 13:31:06 2008 for volume by  doxygen 1.5.3