root / src / lsm_contour.cpp @ 28
Historique | Voir | Annoter | Télécharger (6,98 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_contour lsm_contour.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_contour 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_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 |
|
120 |
string filename_txt=outputdir+"/"+filename; |
121 |
filename_txt.erase(filename_txt.size()-4);
|
122 |
filename=outputdir+"/"+filename;
|
123 |
string result_name=filename;
|
124 |
|
125 |
//txt files
|
126 |
ofstream file; |
127 |
string txt_name=filename_txt+".txt"; |
128 |
file.open(txt_name.c_str()); |
129 |
file<<argv[0]<<endl;
|
130 |
time_t t; |
131 |
struct tm * timeinfo;
|
132 |
time(&t); |
133 |
timeinfo=localtime(&t); |
134 |
file<<asctime(timeinfo); |
135 |
file<<"image : "<<argv[1]<<endl; |
136 |
file<<"_________________________________"<<endl;
|
137 |
file<<"Parameters"<<endl;
|
138 |
file<<"lambda : "<<lam<<endl;
|
139 |
file<<"alpha : "<<alf<<endl;
|
140 |
file<<"epsilon : "<<epsilon<<endl;
|
141 |
file<<"dt : "<<dt<<endl;
|
142 |
file<<"mu : "<<mu<<endl;
|
143 |
file<<"timestep_max : "<<timestep_max<<endl;
|
144 |
file<<"\nthreshold up : "<<t_up<<endl;
|
145 |
file<<"threshold down : "<<t_down<<endl;
|
146 |
file<<"beta : "<<beta<<endl;
|
147 |
file<<"perUp : "<<perUp<<endl;
|
148 |
file<<"perDown : "<<perDown<<endl;
|
149 |
|
150 |
ofstream bg_file; |
151 |
string bg_name=filename_txt+"_BGgrowth.txt"; |
152 |
bg_file.open(bg_name.c_str()); |
153 |
bg_file<<"it\tbg_growth"<<endl;
|
154 |
|
155 |
//-----------------------------------------Image Pre-processing
|
156 |
//add slices
|
157 |
img=add_side_slices(img,3);
|
158 |
|
159 |
//smooth image
|
160 |
file<<"smooth : "<<smooth<<endl;
|
161 |
img.blur(smooth); |
162 |
|
163 |
//-------------------------------------------Initialization
|
164 |
//compute fixed terms
|
165 |
CImg<float> g=edge_indicator(gradient(img));
|
166 |
CImgList<float> gg=gradient(g);
|
167 |
|
168 |
//initialize level-set
|
169 |
int c0=-4; |
170 |
CImg<unsigned char> segmented=threshold_linear_alongZ(img,t_up,t_down); |
171 |
string segmentedName=filename+".gz"; |
172 |
segmentedName.insert(filename.size()-4,"_initial"); |
173 |
segmented.save_gzip_external(segmentedName.c_str()); |
174 |
|
175 |
CImg<float> psi=lsm_contour_init(segmented,c0);
|
176 |
|
177 |
int it=0; |
178 |
int it_stop=0; |
179 |
bool contour_evolves=true; |
180 |
int nb_pix=img.width()*img.height()*img.depth();
|
181 |
double prev_backsegm=segmented.sum();
|
182 |
|
183 |
//-------------------------------------------Time iterations
|
184 |
while( (it<timestep_max) and (contour_evolves==true) ) |
185 |
{ |
186 |
//LSM
|
187 |
psi=evolution_AK2_contour(psi,g,gg,g,lam,mu,alf,beta,epsilon,dt); |
188 |
|
189 |
//Update segmentation
|
190 |
double backsegm=0; |
191 |
cimg_forXYZ(segmented,x,y,z) |
192 |
{ |
193 |
if(psi(x,y,z)>0) |
194 |
{ |
195 |
segmented(x,y,z)=1;
|
196 |
backsegm+=1;
|
197 |
} |
198 |
else
|
199 |
{segmented(x,y,z)=0;}
|
200 |
} |
201 |
|
202 |
//Background evolution
|
203 |
double bg_evolution=backsegm-prev_backsegm;
|
204 |
double bg100=(bg_evolution*1.0/nb_pix)*100; |
205 |
prev_backsegm=backsegm; |
206 |
|
207 |
cout<<"----------------------------------- it : "<<it<<endl;
|
208 |
cout<<"bg growth : "<<bg_evolution<<endl;
|
209 |
cout<<"% of bg growth : "<<bg100<<endl;
|
210 |
bg_file<<it<<"\t"<<bg_evolution<<endl;
|
211 |
|
212 |
|
213 |
//Stop criteria
|
214 |
if((it>10) and (bg100<perUp) and (bg100>perDown)) |
215 |
{ |
216 |
it_stop+=1;
|
217 |
if(it_stop>9) |
218 |
{contour_evolves=false;}
|
219 |
} |
220 |
else
|
221 |
{ |
222 |
it_stop=0;
|
223 |
} |
224 |
|
225 |
//Save result
|
226 |
if((((it%50)==0)and(it!=0))or(contour_evolves==false)or(it==timestep_max-1)) |
227 |
{ |
228 |
CImg<unsigned char>segSave=remove_side_slices(segmented,3); |
229 |
segSave.save_inr(result_name.c_str(),tailleVoxel); |
230 |
segSave.assign(); |
231 |
string zip="gzip -f "+result_name; |
232 |
systout=system(zip.c_str()); |
233 |
} |
234 |
it+=1;
|
235 |
} |
236 |
|
237 |
clock_t end=clock(); |
238 |
double time=double(end-begin)/CLOCKS_PER_SEC; |
239 |
cout <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl; |
240 |
file <<"last iteration : "<<it-1<<endl; |
241 |
file <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl; |
242 |
|
243 |
file<<"width "<<img.width()<<endl;
|
244 |
file<<"height "<<img.height()<<endl;
|
245 |
file<<"depth "<<img.depth()<<endl;
|
246 |
|
247 |
file<<"number of pixel "<<img._width*img._height*img._depth<<endl;
|
248 |
|
249 |
file.close(); |
250 |
return 0; |
251 |
} |