Statistiques
| Révision :

root / src / lsm_detect_contour.cpp @ 24

Historique | Voir | Annoter | Télécharger (7,93 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 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 : lsm_detect_contour img t_up t_down a b smooth perUp perDown"<<endl;
43
      cout<<"Examples for parameter values:"<<endl;
44
      cout<<"------------------------------"<<endl;
45
      cout<<"img : grayscale image of cells, (.inr or .inr.gz)"<<endl;
46
      cout<<"Upper threshold : t_up = 20"<<endl;
47
      cout<<"Down threshold : t_down = 5"<<endl;
48
      cout<<"Area term : a = 0 (0.5, 1)"<<endl;
49
      cout<<"Curvature term : b = 0 (1)"<<endl;
50
      cout<<"Gaussian filter : smooth = 1 (0, if image already filtered)"<<endl;
51
      cout<<"Stop criteria : the contour evolution is in [perDown,perUp] for 10 consecutive iterations"<<endl;
52
      cout<<"     perUp = 0.002, perDown = -0.002"<<endl;
53
      return 0;
54
    }
55

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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