root / src / lsm_contour.cpp @ 6
Historique | Voir | Annoter | Télécharger (7,53 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 lsm_contour.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 img t_up t_down a b smooth perUp perDown
|
17 |
|
18 |
img : grayscale image of cells, .inr or .inr.gz
|
19 |
t_up,t_down : linear threshold value (inr)
|
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!=9) |
45 |
{ |
46 |
cout<<"!! wrong number of arguments"<<endl;
|
47 |
cout<<"Usage : lsm_contour img t_up t_down 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<<"Upper threshold : t_up = 20"<<endl;
|
52 |
cout<<"Down threshold : t_down = 5"<<endl;
|
53 |
cout<<"Area term : a = 0 (0.5, 1)"<<endl;
|
54 |
cout<<"Curvature term : b = 0 (1)"<<endl;
|
55 |
cout<<"Gaussian filter : smooth = 1 (0, if image already filtered)"<<endl;
|
56 |
cout<<"Stop criteria : the contour evolution is in [perDown,perUp] for 10 consecutive iterations"<<endl;
|
57 |
cout<<" perUp = 0.002, perDown = -0.002"<<endl;
|
58 |
return 0; |
59 |
} |
60 |
|
61 |
//ckeck filename and read image
|
62 |
string filename=argv[1]; |
63 |
CImg<unsigned char> img_prev; |
64 |
|
65 |
float tailleVoxel[3] = {0};// resolution initialisation |
66 |
|
67 |
if(filename.compare(filename.size()-4,4,".inr")==0) |
68 |
{ |
69 |
img_prev.get_load_inr(filename.c_str(),tailleVoxel); // reads resolution
|
70 |
} |
71 |
else if(filename.compare(filename.size()-7,7,".inr.gz")==0) |
72 |
{ |
73 |
string oldname = filename;
|
74 |
filename.erase(filename.size()-3);
|
75 |
string zip="gunzip -c "+oldname+" > "+filename; |
76 |
if(system(zip.c_str())); // decompress image file |
77 |
img_prev.load(filename.c_str()); //read image
|
78 |
img_prev.get_load_inr(filename.c_str(),tailleVoxel); // read resolution
|
79 |
zip="rm "+filename;
|
80 |
if(system(zip.c_str())); //removes decompressed image |
81 |
|
82 |
|
83 |
} |
84 |
else
|
85 |
{cout<<"!! wrong file extension : "<<filename<<endl;
|
86 |
return 0;} |
87 |
CImg<float> img=img_prev;
|
88 |
img_prev.assign(); |
89 |
cout<<"original image : "<<filename<<endl;
|
90 |
|
91 |
//--------------------------------------------Parameters
|
92 |
//model parameters
|
93 |
int lam=10; |
94 |
int alf=atoi(argv[4]); |
95 |
int beta=atoi(argv[5]); |
96 |
|
97 |
//numerical parameters
|
98 |
float epsilon=1.5; |
99 |
int dt=100; |
100 |
float mu=0.1/dt; |
101 |
int timestep_max=2000; |
102 |
|
103 |
//linear threshold
|
104 |
int t_up=atoi(argv[2]); |
105 |
int t_down=atoi(argv[3]); |
106 |
|
107 |
float smooth=atof(argv[6]); |
108 |
|
109 |
float perUp=atof(argv[7]); |
110 |
float perDown=atof(argv[8]); |
111 |
|
112 |
cout<<"Voxel size : ("<<tailleVoxel[0]<<","<<tailleVoxel[1]<<","<<tailleVoxel[2]<<")"<<endl; |
113 |
|
114 |
|
115 |
//-------------------------------------------Names and directories
|
116 |
//new name with arguments
|
117 |
string ar2=argv[2]; |
118 |
string ar3=argv[3]; |
119 |
string ar4=argv[4]; |
120 |
string ar5=argv[5]; |
121 |
string ar6=argv[6]; |
122 |
string insert="_LSMcont"+ar2+"-"+ar3+"a"+ar4+"b"+ar5+"s"+ar6; |
123 |
filename.insert(filename.size()-4,insert);
|
124 |
|
125 |
//create directories and update names
|
126 |
size_t test=filename.rfind("/");
|
127 |
if(test!=filename.npos)
|
128 |
{filename.erase(0,test+1);} |
129 |
string outputdir=filename;
|
130 |
outputdir.erase(filename.size()-4);
|
131 |
string mkdir="mkdir -p "+outputdir; |
132 |
if(system(mkdir.c_str()));
|
133 |
|
134 |
string filename_txt=outputdir+"/"+filename; |
135 |
filename_txt.erase(filename_txt.size()-4);
|
136 |
filename=outputdir+"/"+filename;
|
137 |
string result_name=filename;
|
138 |
|
139 |
//txt files
|
140 |
ofstream file; |
141 |
string txt_name=filename_txt+".txt"; |
142 |
file.open(txt_name.c_str()); |
143 |
file<<argv[0]<<endl;
|
144 |
time_t t; |
145 |
struct tm * timeinfo;
|
146 |
time(&t); |
147 |
timeinfo=localtime(&t); |
148 |
file<<asctime(timeinfo); |
149 |
file<<"image : "<<argv[1]<<endl; |
150 |
file<<"_________________________________"<<endl;
|
151 |
file<<"Parameters"<<endl;
|
152 |
file<<"lambda : "<<lam<<endl;
|
153 |
file<<"alpha : "<<alf<<endl;
|
154 |
file<<"epsilon : "<<epsilon<<endl;
|
155 |
file<<"dt : "<<dt<<endl;
|
156 |
file<<"mu : "<<mu<<endl;
|
157 |
file<<"timestep_max : "<<timestep_max<<endl;
|
158 |
file<<"\nthreshold up : "<<t_up<<endl;
|
159 |
file<<"threshold down : "<<t_down<<endl;
|
160 |
file<<"beta : "<<beta<<endl;
|
161 |
file<<"perUp : "<<perUp<<endl;
|
162 |
file<<"perDown : "<<perDown<<endl;
|
163 |
|
164 |
ofstream bg_file; |
165 |
string bg_name=filename_txt+"_BGgrowth.txt"; |
166 |
bg_file.open(bg_name.c_str()); |
167 |
bg_file<<"it\tbg_growth"<<endl;
|
168 |
|
169 |
//-----------------------------------------Image Pre-processing
|
170 |
//add slices
|
171 |
img=add_side_slices(img,3);
|
172 |
|
173 |
//smooth image
|
174 |
file<<"smooth : "<<smooth<<endl;
|
175 |
img.blur(smooth); |
176 |
|
177 |
//-------------------------------------------Initialization
|
178 |
//compute fixed terms
|
179 |
CImg<float> g=edge_indicator(gradient(img));
|
180 |
CImgList<float> gg=gradient(g);
|
181 |
|
182 |
//initialize level-set
|
183 |
int c0=-4; |
184 |
CImg<unsigned char> segmented=threshold_linear_alongZ(img,t_up,t_down); |
185 |
string segmentedName=filename+".gz"; |
186 |
segmentedName.insert(filename.size()-4,"_initial"); |
187 |
segmented.save_gzip_external(segmentedName.c_str()); |
188 |
|
189 |
CImg<float> psi=lsm_contour_init(segmented,c0);
|
190 |
|
191 |
int it=0; |
192 |
int it_stop=0; |
193 |
bool contour_evolves=true; |
194 |
int nb_pix=img.width()*img.height()*img.depth();
|
195 |
double prev_backsegm=segmented.sum();
|
196 |
|
197 |
//-------------------------------------------Time iterations
|
198 |
while( (it<timestep_max) and (contour_evolves==true) ) |
199 |
{ |
200 |
//LSM
|
201 |
psi=evolution_AK2_contour(psi,g,gg,g,lam,mu,alf,beta,epsilon,dt); |
202 |
|
203 |
//Update segmentation
|
204 |
double backsegm=0; |
205 |
cimg_forXYZ(segmented,x,y,z) |
206 |
{ |
207 |
if(psi(x,y,z)>0) |
208 |
{ |
209 |
segmented(x,y,z)=1;
|
210 |
backsegm+=1;
|
211 |
} |
212 |
else
|
213 |
{segmented(x,y,z)=0;}
|
214 |
} |
215 |
|
216 |
//Background evolution
|
217 |
double bg_evolution=backsegm-prev_backsegm;
|
218 |
double bg100=(bg_evolution*1.0/nb_pix)*100; |
219 |
prev_backsegm=backsegm; |
220 |
|
221 |
cout<<"----------------------------------- it : "<<it<<endl;
|
222 |
cout<<"bg growth : "<<bg_evolution<<endl;
|
223 |
cout<<"% of bg growth : "<<bg100<<endl;
|
224 |
bg_file<<it<<"\t"<<bg_evolution<<endl;
|
225 |
|
226 |
|
227 |
//Stop criteria
|
228 |
if((it>10) and (bg100<perUp) and (bg100>perDown)) |
229 |
{ |
230 |
it_stop+=1;
|
231 |
if(it_stop>9) |
232 |
{contour_evolves=false;}
|
233 |
} |
234 |
else
|
235 |
{ |
236 |
it_stop=0;
|
237 |
} |
238 |
|
239 |
//Save result
|
240 |
if((((it%50)==0)and(it!=0))or(contour_evolves==false)or(it==timestep_max-1)) |
241 |
{ |
242 |
CImg<unsigned char>segSave=remove_side_slices(segmented,3); |
243 |
segSave.save_inr(result_name.c_str(),tailleVoxel); |
244 |
segSave.assign(); |
245 |
string zip="gzip -f "+result_name; |
246 |
if(system(zip.c_str()));
|
247 |
} |
248 |
it+=1;
|
249 |
} |
250 |
|
251 |
clock_t end=clock(); |
252 |
double time=double(end-begin)/CLOCKS_PER_SEC; |
253 |
cout <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl; |
254 |
file <<"last iteration : "<<it-1<<endl; |
255 |
file <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl; |
256 |
|
257 |
file<<"width "<<img.width()<<endl;
|
258 |
file<<"height "<<img.height()<<endl;
|
259 |
file<<"depth "<<img.depth()<<endl;
|
260 |
|
261 |
file<<"number of pixel "<<img._width*img._height*img._depth<<endl;
|
262 |
|
263 |
file.close(); |
264 |
return 0; |
265 |
} |