Statistiques
| Révision :

root / src / lsm_contour_init.cpp @ 7

Historique | Voir | Annoter | Télécharger (6,87 ko)

1
/*
2
Level-set Method to detect tissue contour (exterior shape)
3
- Sequential -
4

5
      Copyright 2016 ENS de Lyon
6

7
       File author(s):
8
           Typhaine Moreau, Annamaria Kiss <annamaria.kiss@ens-lyon.fr.fr>
9
       See accompanying file LICENSE.txt
10

11
To compile
12
 g++ -o lsm_contour_init lsm_contour_init.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11 -l:libtiff.so.5
13
 Need CImg.h and lsm_lib.h
14
 
15
To execute
16
 ./lsm_contour_init img initial_contour a b smooth perUp perDown
17

18
 img : grayscale image of cells (.inr or .inr.gz)
19
 initial_contour : 8bit image, with values 0=tissue, 1=background (.inr or .inr.gz)
20
 a : area term (float) --> 0.5, 1
21
 b : curvature term (float)
22
 smooth : amount of gaussian blur to apply to the image
23
 perUp, perDown : the algorithm stops when 10 succesive iteration are between perUp and perDown (in % of background growth)
24
*/
25

    
26
#include <iostream>
27
#include <math.h>
28
#include <sstream>
29
#include <vector>
30
#include <fstream>
31

    
32
#include "lsm_lib.h"
33

    
34
using namespace cimg_library;
35
using namespace std;
36

    
37
//------------------------------------------------------------------------------
38
//Main
39
//------------------------------------------------------------------------------
40
int main (int argc, char* argv[])
41
{
42
  clock_t begin=clock();
43

    
44
  if(argc!=8)
45
    {
46
      cout<<"!! wrong number of arguments"<<endl;
47
      cout<<"Usage : lsm_contour_init img initial_contour a b smooth perUp perDown"<<endl;
48
      cout<<"Examples for parameter values:"<<endl;
49
      cout<<"------------------------------"<<endl;
50
      cout<<"img : grayscale image of cells, (.inr or .inr.gz)"<<endl;
51
      cout<<"initial_contour : 8bit image, with values 0=tissue, 1=background (.inr or .inr.gz)"<<endl;
52
      cout<<"Area term : a = 0 (0.5, 1)"<<endl;
53
      cout<<"Curvature term : b = 0 (1)"<<endl;
54
      cout<<"Gaussian filter : smooth = 1 (0, if image already filtered)"<<endl;
55
      cout<<"Stop criteria : the contour evolution is in [perDown,perUp] for 10 consecutive iterations"<<endl;
56
      cout<<"     perUp = 0.002, perDown = -0.002"<<endl;
57
      return 0;
58
    }
59

    
60
  //check filename and read image
61
  //-----------------------------
62
  string filename=argv[1];
63
  float tailleVoxel[3] = {0};// resolution initialisation  
64
  CImg<float> img=imread(filename,tailleVoxel);
65

    
66
 //check filename and read the initial contour image
67
 //-----------------------------
68
  string filename_init=argv[2];
69
  float tailleVoxel_init[3] = {0};// resolution initialisation
70
  CImg<float> img_init=imread(filename_init,tailleVoxel_init);
71

    
72
  //--------------------------------------------Parameters
73
  //model parameters
74
  int lam=10;
75
  int alf=atoi(argv[3]);
76
  int beta=atoi(argv[4]);
77

    
78
  //numerical parameters
79
  float epsilon=1.5;
80
  int dt=100;
81
  float mu=0.1/dt;
82
  int timestep_max=2000;
83

    
84
  //smoothing and stop criteria
85
  float smooth=atof(argv[5]);
86
  float perUp=atof(argv[6]);
87
  float perDown=atof(argv[7]);
88

    
89
  cout<<"Voxel size : ("<<tailleVoxel[0]<<","<<tailleVoxel[1]<<","<<tailleVoxel[2]<<")"<<endl;
90
  cout<<"Voxel size in contour : ("<<tailleVoxel_init[0]<<","<<tailleVoxel_init[1]<<","<<tailleVoxel_init[2]<<")"<<endl;
91

    
92
  //-------------------------------------------Names and directories
93
  //new name with arguments
94
  string ar2=argv[2];
95
  string ar3=argv[3];
96
  string ar4=argv[4];
97
  string ar5=argv[5];
98
  string insert="_LSMcont_a"+ar3+"b"+ar4+"s"+ar5;
99
  filename.insert(filename.size()-4,insert);
100

    
101
  //create directories and update names
102
  size_t test=filename.rfind("/");
103
  if(test!=filename.npos)
104
    {filename.erase(0,test+1);}
105
  string outputdir=filename;
106
  outputdir.erase(filename.size()-4);
107
  string mkdir="mkdir -p "+outputdir;
108
  if(system(mkdir.c_str())); 
109

    
110
  string filename_txt=outputdir+"/"+filename;
111
  filename_txt.erase(filename_txt.size()-4);
112
  filename=outputdir+"/"+filename;
113
  string result_name=filename;
114

    
115
  //txt files 
116
  ofstream file;
117
  string txt_name=filename_txt+".txt";
118
  file.open(txt_name.c_str());
119
  file<<argv[0]<<endl;
120
  time_t t;
121
  struct tm * timeinfo;
122
  time(&t);
123
  timeinfo=localtime(&t);
124
  file<<asctime(timeinfo);
125
  file<<"image : "<<argv[1]<<endl;
126
  file<<"initial contour : "<<argv[2]<<endl;
127
  file<<"_________________________________"<<endl;
128
  file<<"Parameters"<<endl;
129
  file<<"lambda : "<<lam<<endl;
130
  file<<"alpha : "<<alf<<endl;
131
  file<<"epsilon : "<<epsilon<<endl;
132
  file<<"dt : "<<dt<<endl;
133
  file<<"mu : "<<mu<<endl;
134
  file<<"timestep_max : "<<timestep_max<<endl;
135
  file<<"beta : "<<beta<<endl;
136
  file<<"perUp : "<<perUp<<endl;
137
  file<<"perDown : "<<perDown<<endl;
138

    
139
  ofstream bg_file;
140
  string bg_name=filename_txt+"_BGgrowth.txt";
141
  bg_file.open(bg_name.c_str());
142
  bg_file<<"it\tbg_growth"<<endl;
143

    
144
  //-----------------------------------------Image Pre-processing
145
  //add slices
146
  img=add_side_slices(img,3);
147
  img_init=add_side_slices(img_init,3);
148
  CImg<unsigned char> segmented = img_init;
149

    
150
  //smooth image
151
  file<<"smooth : "<<smooth<<endl;
152
  img.blur(smooth);
153

    
154
  //-------------------------------------------Initialization
155
  //compute fixed terms
156
  CImg<float> g=edge_indicator(gradient(img));
157
  CImgList<float> gg=gradient(g);
158
 
159
  //initialize level-set
160
  int c0=-4;
161
  CImg<float> psi=lsm_contour_init(segmented,c0);
162

    
163
  int it=0;
164
  int it_stop=0;
165
  bool contour_evolves=true;
166
  int nb_pix=img.width()*img.height()*img.depth();
167
  double prev_backsegm=segmented.sum();
168

    
169
  //-------------------------------------------Time iterations
170
  while( (it<timestep_max) and (contour_evolves==true) )
171
    {
172
      //LSM
173
      psi=evolution_AK2_contour(psi,g,gg,g,lam,mu,alf,beta,epsilon,dt);
174

    
175
      //Update segmentation
176
      double backsegm=0;
177
      cimg_forXYZ(segmented,x,y,z)
178
        {
179
          if(psi(x,y,z)>0)
180
            {
181
              segmented(x,y,z)=1;
182
              backsegm+=1;
183
            }
184
          else
185
            {segmented(x,y,z)=0;}
186
        }        
187

    
188
      //Background evolution
189
      double bg_evolution=backsegm-prev_backsegm;
190
      double bg100=(bg_evolution*1.0/nb_pix)*100;
191
      prev_backsegm=backsegm;
192

    
193
      cout<<"----------------------------------- it : "<<it<<endl;
194
      cout<<"bg growth : "<<bg_evolution<<endl;
195
      cout<<"% of bg growth : "<<bg100<<endl;
196
      bg_file<<it<<"\t"<<bg_evolution<<endl;
197
   
198

    
199
      //Stop criteria 
200
      if((it>10) and (bg100<perUp) and (bg100>perDown))
201
        {
202
          it_stop+=1;
203
          if(it_stop>9)
204
            {contour_evolves=false;}
205
        }
206
      else
207
        {
208
          it_stop=0;
209
        }
210

    
211
    //Save result
212
      if((((it%50)==0)and(it!=0))or(contour_evolves==false)or(it==timestep_max-1))
213
        {          
214
          CImg<unsigned char>segSave=remove_side_slices(segmented,3);
215
          int sv=imsave(result_name, tailleVoxel, segSave);
216
        }
217
      it+=1;
218
    }
219

    
220
  clock_t end=clock();
221
  double time=double(end-begin)/CLOCKS_PER_SEC;
222
  cout <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl;
223
  file <<"last iteration : "<<it-1<<endl;
224
  file <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl;
225

    
226
  file<<"width "<<img.width()<<endl;
227
  file<<"height "<<img.height()<<endl;
228
  file<<"depth "<<img.depth()<<endl;
229

    
230
  file<<"number of pixel "<<img._width*img._height*img._depth<<endl;
231

    
232
  file.close();
233
  return 0;
234
}