Révision 6

lsm3d_compile.sh (revision 6)
29 29
# ==========
30 30
cd src
31 31
echo '------------------------------------'
32

  
33

  
34
echo 'compiling contour_init...'
35
$CCPP -o $bin_directory/contour_init contour_init.cpp $compiling_options
36

  
37
echo 'compiling lsm_contour_init...'
38
$CCPP -o $bin_directory/lsm_contour_init lsm_contour_init.cpp $compiling_options
39

  
32 40
echo 'compiling lsm_contour...'
33 41
$CCPP -o $bin_directory/lsm_contour lsm_contour.cpp $compiling_options
34 42

  
35 43
echo 'compiling lsm_cells...'
36 44
$CCPP -o $bin_directory/lsm_cells lsm_cells.cpp $compiling_options -fopenmp 
37 45

  
38
#echo 'compiling tiff2inr...'
39
#$CCPP -o $bin_directory/tiff2inr tiff2inr.cpp $compiling_options -l:libtiff.so.5 
46
echo 'compiling tiff2inr...'
47
$CCPP -o $bin_directory/tiff2inr tiff2inr.cpp $compiling_options -l:libtiff.so.5 
40 48

  
41 49

  
42 50
echo 'Done!'
src/contour_init.cpp (revision 6)
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 contour_init 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
 ./contour_init img t_up t_down smooth
17

  
18
 img : grayscale image of cells, .inr or .inr.gz
19
 t_up,t_down : linear threshold value (inr)
20
 smooth : amount of gaussian blur to apply to the image
21
*/
22

  
23
#include <iostream>
24
#include <math.h>
25
#include <sstream>
26
#include <vector>
27
#include <fstream>
28

  
29
#include "lsm_lib.h"
30

  
31
using namespace cimg_library;
32
using namespace std;
33

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

  
41
  if(argc!=5)
42
    {
43
      cout<<"!! wrong number of arguments"<<endl;
44
      cout<<"Usage : contour_init img t_up t_down smooth"<<endl;
45
      cout<<"Examples for parameter values:"<<endl;
46
      cout<<"------------------------------"<<endl;
47
      cout<<"img : grayscale image of cells, (.inr or .inr.gz)"<<endl;
48
      cout<<"Upper threshold : t_up = 20"<<endl;
49
      cout<<"Down threshold : t_down = 5"<<endl;
50
      cout<<"Gaussian filter : smooth = 1 (0, if image already filtered)"<<endl;
51
      return 0;
52
    }
53

  
54
  //ckeck filename and read image
55
  string filename=argv[1];
56
  float tailleVoxel[3] = {0};// resolution initialisation
57
  CImg<float> img=imread(filename, tailleVoxel); 
58

  
59
  //--------------------------------------------Parameters
60
  //linear threshold and smoothing
61
  int t_up=atoi(argv[2]);
62
  int t_down=atoi(argv[3]);
63
  float smooth=atof(argv[4]);
64

  
65
  //-------------------------------------------Names and directories
66
  //new name with arguments
67
  string ar2=argv[2];
68
  string ar3=argv[3];
69
  string ar4=argv[4];
70
  string insert="_initial"+ar2+"-"+ar3+"s"+ar4;
71
  filename.insert(filename.size()-4,insert);
72

  
73
  //-----------------------------------------Image Pre-processing
74
  //smooth image
75
  img.blur(smooth);
76

  
77
  //-------------------------------------------Initialization
78
  //initialize level-set
79
  CImg<unsigned char> segmented=threshold_linear_alongZ(img,t_up,t_down);
80
  int s=imsave(filename, tailleVoxel, segmented);
81
  return 0;
82
}
src/lsm_lib.h (revision 6)
22 22

  
23 23
//**************************************** GLOBAL IMAGE *************************************************************************
24 24

  
25
//Write an image image
26
//----------------------------------------------------------------------------
27
int imsave(string filename, float tailleVoxel[3], CImg<unsigned char> const & img)
28
{
29
	  img.save_inr(filename.c_str(),tailleVoxel);
30
	  string zip="gzip -f "+filename;
31
	  if(system(zip.c_str()));
32
  cout<<"Image saved in file :"<<filename<<".gz"<<endl;
33
  cout<<" - Voxel size : ("<<tailleVoxel[0]<<","<<tailleVoxel[1]<<","<<tailleVoxel[2]<<")"<<endl;
34
  cout<<" - Image size : ("<<img.width()<<","<<img.height()<<","<<img.depth()<<")"<<endl;
35
  return 1;
36
}
37

  
38
//Read an image image
39
//----------------------------------------------------------------------------
40
CImg<unsigned char> imread(string &filename, float (&tailleVoxel)[3])
41
{
42
	CImg<unsigned char> img_prev;
43
	cout<<"Image read : "<<filename<<endl;
44
    if(filename.compare(filename.size()-4,4,".inr")==0)
45
    {
46
      img_prev.get_load_inr(filename.c_str(),tailleVoxel); // reads resolution
47
    }
48
  else if(filename.compare(filename.size()-7,7,".inr.gz")==0)
49
    {
50
      string oldname = filename;
51
      filename.erase(filename.size()-3);
52
      string zip="gunzip -c "+oldname+" > "+filename;
53
      if(system(zip.c_str())); // decompress image file
54
      img_prev.load(filename.c_str()); //read image
55
      img_prev.get_load_inr(filename.c_str(),tailleVoxel); // read resolution
56
      zip="rm "+filename; 
57
      if(system(zip.c_str())); //removes decompressed image    
58
    }
59
  else
60
    {cout<<"!! wrong file extension : "<<filename<<endl;
61
	}
62
  cout<<" - Voxel size : ("<<tailleVoxel[0]<<","<<tailleVoxel[1]<<","<<tailleVoxel[2]<<")"<<endl;
63
  cout<<" - Image size : ("<<img_prev.width()<<","<<img_prev.height()<<","<<img_prev.depth()<<")"<<endl;
64
  return img_prev;
65
}
66

  
25 67
//Invert the image
26 68
//----------------------------------------------------------------------------
27 69
CImg<unsigned char> invert_image(CImg<unsigned char> const & img)
src/lsm_contour_init.cpp (revision 6)
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
}

Formats disponibles : Unified diff