Statistiques
| Révision :

root / src / lsm_detect_contour_full.cpp @ 21

Historique | Voir | Annoter | Télécharger (7,86 ko)

1
/*
2
Level-set Method to detect contour (exterior shape)
3
Sequential
4
Stop criteria parameters in command lines
5

6
To compile
7
 g++ -o lsm_detect_contour_full.exe lsm_detect_contour_full.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11 -l:libtiff.so.5
8
 Need CImg.h and lsm_lib.h
9
 
10
To execute
11
 ./lsm_detect_contour_ful.exe img t_up t_down a b smooth perUp perDown
12

13
 img : grayscale image of cells, .inr or .inr.gz
14
 t_up,t_down : linear threshold value (inr)
15
 a : area term (float) --> 0.5, 1
16
 b : curvature term (float)
17
 smooth : amount of gaussian blur to apply to the image
18
 perUp, perDown : the algorithm stops when 10 succesive iteration are between perUp and perDown (in % of background growth)
19
*/
20

    
21
#include <iostream>
22
#include <math.h>
23
#include <sstream>
24
#include <vector>
25
#include <fstream>
26

    
27
#include "lsm_lib.h"
28

    
29
using namespace cimg_library;
30
using namespace std;
31

    
32
//------------------------------------------------------------------------------
33
//Main
34
//------------------------------------------------------------------------------
35
int main (int argc, char* argv[])
36
{
37
  clock_t begin=clock();
38

    
39
  if(argc!=9)
40
    {
41
      cout<<"!! wrong number of arguments"<<endl;
42
      cout<<"Usage : ./xxxx.exe img t_up t_down a b smooth perUp perDown"<<endl;
43
      cout<<"Examples for parameter values:"<<endl;
44
      cout<<"------------------------------"<<endl;
45
      cout<<"Upper threshold : t_up = 20"<<endl;
46
      cout<<"Down threshold : t_down = 5"<<endl;
47
      cout<<"Area term : a = 0 (0.5, 1)"<<endl;
48
      cout<<"Curvature term : b = 0 (1)"<<endl;
49
      cout<<"Gaussian filter : smooth = 1 (0, if image already filtered)"<<endl;
50
      cout<<"Stop criteria : the contour evolution is in [perDown,perUp] for 10 consecutive iterations"<<endl;
51
      cout<<"     perUp = 0.002, perDown = -0.002"<<endl;
52
      return 0;
53
    }
54

    
55
  //ckeck filename and read image
56
  string filename=argv[1];
57
  CImg<unsigned char> img_prev;
58
  if(filename.compare(filename.size()-4,4,".inr")==0)
59
    {
60
      img_prev.load(filename.c_str());
61
    }
62
  else if(filename.compare(filename.size()-7,7,".inr.gz")==0)
63
    {
64
      img_prev.load_gzip_external(filename.c_str());
65
      filename.erase(filename.size()-3);
66
    }
67
  else
68
    {cout<<"!! wrong file extension : "<<filename<<endl;
69
      return 0;}
70
  CImg<float> img=img_prev;
71
  img_prev.assign();
72
  cout<<"original image : "<<filename<<endl;
73

    
74
  //--------------------------------------------Parameters
75
  //model parameters
76
  int lam=10;
77
  int alf=atoi(argv[4]);
78
  int beta=atoi(argv[5]);
79

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

    
86
  //linear threshold
87
  int t_up=atoi(argv[2]);
88
  int t_down=atoi(argv[3]);
89

    
90
  float smooth=atof(argv[6]);
91

    
92
  float perUp=atof(argv[7]);
93
  float perDown=atof(argv[8]);
94

    
95
  float tailleVoxel[3] = {0.195177,0.195177,0.195177};
96
  
97
  //other paremeters
98
  int systout;
99

    
100
  //-------------------------------------------Names and directories
101
  //new name with arguments
102
  string ar2=argv[2];
103
  string ar3=argv[3];
104
  string ar4=argv[4];
105
  string ar5=argv[5];
106
  string ar6=argv[6];
107
  string insert="_LSMcont"+ar2+"-"+ar3+"a"+ar4+"b"+ar5+"s"+ar6;
108
  filename.insert(filename.size()-4,insert);
109

    
110
  //create directories and update names
111
  size_t test=filename.rfind("/");
112
  if(test!=filename.npos)
113
    {filename.erase(0,test+1);}
114
  string outputdir=filename;
115
  outputdir.erase(filename.size()-4);
116
  string mkdir="mkdir -p "+outputdir;
117
  systout=system(mkdir.c_str()); 
118
  mkdir="mkdir -p "+outputdir+"/evolution";
119
  systout=system(mkdir.c_str());
120

    
121
  string filename_cut=outputdir+"/evolution/"+filename;
122
  filename_cut.erase(filename_cut.size()-4);
123
  filename=outputdir+"/"+filename;
124
  string result_name=filename;
125

    
126
  //txt files 
127
  ofstream file;
128
  string txt_name=filename_cut+".txt";
129
  file.open(txt_name.c_str());
130
  file<<argv[0]<<endl;
131
  time_t t;
132
  struct tm * timeinfo;
133
  time(&t);
134
  timeinfo=localtime(&t);
135
  file<<asctime(timeinfo);
136
  file<<"image : "<<argv[1]<<endl;
137
  file<<"_________________________________"<<endl;
138
  file<<"Parameters"<<endl;
139
  file<<"lambda : "<<lam<<endl;
140
  file<<"alpha : "<<alf<<endl;
141
  file<<"epsilon : "<<epsilon<<endl;
142
  file<<"dt : "<<dt<<endl;
143
  file<<"mu : "<<mu<<endl;
144
  file<<"timestep_max : "<<timestep_max<<endl;
145
  file<<"\nthreshold up : "<<t_up<<endl;
146
  file<<"threshold down : "<<t_down<<endl;
147
  file<<"beta : "<<beta<<endl;
148
  file<<"perUp : "<<perUp<<endl;
149
  file<<"perDown : "<<perDown<<endl;
150

    
151
  ofstream bg_file;
152
  string bg_name=filename_cut+"_BGgrowth.txt";
153
  bg_file.open(bg_name.c_str());
154
  bg_file<<"it\tbg_growth"<<endl;
155

    
156
  //-----------------------------------------Image Pre-processing
157
  //add slices
158
  img=add_side_slices(img,3);
159

    
160
  //define and save cut
161
  int cut=img._depth/2;
162
  cout <<"cut z= "<<cut<<endl;
163
  file <<"\ncut z= "<<cut<<endl;
164
  CImg<float> imgCut=img.get_crop(0,0,cut,0,img._width,img._height,cut,0);
165
  imgCut.save((filename_cut+".png").c_str());
166

    
167
  //smooth image
168
  file<<"smooth : "<<smooth<<endl;
169
  img.blur(smooth);
170

    
171
  //-------------------------------------------Initialization
172
  //compute fixed terms
173
  CImg<float> g=edge_indicator(gradient(img));
174
  CImgList<float> gg=gradient(g);
175
 
176
  //initialize level-set
177
  int c0=-4;
178
  CImg<unsigned char> segmented=threshold_linear_alongZ(img,t_up,t_down);
179
  string segmentedName=filename+".gz";
180
  segmentedName.insert(filename.size()-4,"_initial");
181
  segmented.save_gzip_external(segmentedName.c_str());
182

    
183
  CImg<float> psi=lsm_contour_init(segmented,c0);
184

    
185
  int it=0;
186
  int it_stop=0;
187
  bool contour_evolves=true;
188
  int nb_pix=img.width()*img.height()*img.depth();
189
  double prev_backsegm=segmented.sum();
190

    
191
  //-------------------------------------------Time iterations
192
  while( (it<timestep_max) and (contour_evolves==true) )
193
    {
194
      //LSM
195
      psi=evolution_AK2_contour(psi,g,gg,g,lam,mu,alf,beta,epsilon,dt);
196

    
197
      //Update segmentation
198
      double backsegm=0;
199
      cimg_forXYZ(segmented,x,y,z)
200
        {
201
          if(psi(x,y,z)>0)
202
            {
203
              segmented(x,y,z)=1;
204
              backsegm+=1;
205
            }
206
          else
207
            {segmented(x,y,z)=0;}
208
        }        
209

    
210
      //Background evolution
211
      double bg_evolution=backsegm-prev_backsegm;
212
      double bg100=(bg_evolution*1.0/nb_pix)*100;
213
      prev_backsegm=backsegm;
214

    
215
      cout<<"----------------------------------- it : "<<it<<endl;
216
      cout<<"bg growth : "<<bg_evolution<<endl;
217
      cout<<"% of bg growth : "<<bg100<<endl;
218
      bg_file<<it<<"\t"<<bg_evolution<<endl;
219
   
220

    
221
      //Stop criteria 
222
      if((it>10) and (bg100<perUp) and (bg100>perDown))
223
        {
224
          it_stop+=1;
225
          if(it_stop>9)
226
            {contour_evolves=false;}
227
        }
228
      else
229
        {
230
          it_stop=0;
231
        }
232

    
233
      //Graphics to follow evolution
234
      if((it%50==0)or(contour_evolves==false)or(it==timestep_max-1))
235
        {
236
          ostringstream oss;
237
          oss << it;
238
          string iterstring="";
239
          if(it<10) {iterstring="00"+oss.str();}
240
          else if (it<100) {iterstring="0"+oss.str();}
241
          else {iterstring=oss.str();}
242
          cout<<"-----------------------------------"<<endl;
243
          cout<<" *** time step for graphics : "<<iterstring<<endl;
244
          //Save cut
245
          CImg<float>segCut=segmented.get_crop(0,0,cut,0,img._width,img._height,cut,0);
246
          string temp_name=filename_cut+"it"+iterstring+".png";
247
          segCut.normalize(0,255).save(temp_name.c_str());
248
        }
249
      if((((it%50)==0)and(it!=0))or(contour_evolves==false)or(it==timestep_max-1))
250
        {
251
          //Save result
252
          CImg<unsigned char>segSave=remove_side_slices(segmented,3);
253
          segSave.save_inr(result_name.c_str(),tailleVoxel);
254
          segSave.assign();
255
          string zip="gzip -f "+result_name;
256
          systout=system(zip.c_str());
257
        }
258
      it+=1;
259
    }
260

    
261
  clock_t end=clock();
262
  double time=double(end-begin)/CLOCKS_PER_SEC;
263
  cout <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl;
264
  file <<"last iteration : "<<it-1<<endl;
265
  file <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl;
266

    
267
  file<<"width "<<img.width()<<endl;
268
  file<<"height "<<img.height()<<endl;
269
  file<<"depth "<<img.depth()<<endl;
270

    
271
  file<<"number of pixel "<<img._width*img._height*img._depth<<endl;
272

    
273
  file.close();
274
  return 0;
275
}