root / src / lsm_contour.cpp @ 30
Historique | Voir | Annoter | Télécharger (7,39 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 |
|
60 |
float tailleVoxel[3] = {0};// resolution initialisation |
61 |
|
62 |
if(filename.compare(filename.size()-4,4,".inr")==0) |
63 |
{ |
64 |
img_prev.get_load_inr(filename.c_str(),tailleVoxel); // reads resolution
|
65 |
} |
66 |
else if(filename.compare(filename.size()-7,7,".inr.gz")==0) |
67 |
{ |
68 |
string oldname = filename;
|
69 |
filename.erase(filename.size()-3);
|
70 |
string zip="gunzip -c "+oldname+" > "+filename; |
71 |
if(system(zip.c_str())); // decompress image file |
72 |
img_prev.load(filename.c_str()); //read image
|
73 |
img_prev.get_load_inr(filename.c_str(),tailleVoxel); // read resolution
|
74 |
zip="rm "+filename;
|
75 |
if(system(zip.c_str())); //removes decompressed image |
76 |
|
77 |
|
78 |
} |
79 |
else
|
80 |
{cout<<"!! wrong file extension : "<<filename<<endl;
|
81 |
return 0;} |
82 |
CImg<float> img=img_prev;
|
83 |
img_prev.assign(); |
84 |
cout<<"original image : "<<filename<<endl;
|
85 |
|
86 |
//--------------------------------------------Parameters
|
87 |
//model parameters
|
88 |
int lam=10; |
89 |
int alf=atoi(argv[4]); |
90 |
int beta=atoi(argv[5]); |
91 |
|
92 |
//numerical parameters
|
93 |
float epsilon=1.5; |
94 |
int dt=100; |
95 |
float mu=0.1/dt; |
96 |
int timestep_max=2000; |
97 |
|
98 |
//linear threshold
|
99 |
int t_up=atoi(argv[2]); |
100 |
int t_down=atoi(argv[3]); |
101 |
|
102 |
float smooth=atof(argv[6]); |
103 |
|
104 |
float perUp=atof(argv[7]); |
105 |
float perDown=atof(argv[8]); |
106 |
|
107 |
cout<<"Voxel size : ("<<tailleVoxel[0]<<","<<tailleVoxel[1]<<","<<tailleVoxel[2]<<")"<<endl; |
108 |
|
109 |
|
110 |
//-------------------------------------------Names and directories
|
111 |
//new name with arguments
|
112 |
string ar2=argv[2]; |
113 |
string ar3=argv[3]; |
114 |
string ar4=argv[4]; |
115 |
string ar5=argv[5]; |
116 |
string ar6=argv[6]; |
117 |
string insert="_LSMcont"+ar2+"-"+ar3+"a"+ar4+"b"+ar5+"s"+ar6; |
118 |
filename.insert(filename.size()-4,insert);
|
119 |
|
120 |
//create directories and update names
|
121 |
size_t test=filename.rfind("/");
|
122 |
if(test!=filename.npos)
|
123 |
{filename.erase(0,test+1);} |
124 |
string outputdir=filename;
|
125 |
outputdir.erase(filename.size()-4);
|
126 |
string mkdir="mkdir -p "+outputdir; |
127 |
if(system(mkdir.c_str()));
|
128 |
|
129 |
string filename_txt=outputdir+"/"+filename; |
130 |
filename_txt.erase(filename_txt.size()-4);
|
131 |
filename=outputdir+"/"+filename;
|
132 |
string result_name=filename;
|
133 |
|
134 |
//txt files
|
135 |
ofstream file; |
136 |
string txt_name=filename_txt+".txt"; |
137 |
file.open(txt_name.c_str()); |
138 |
file<<argv[0]<<endl;
|
139 |
time_t t; |
140 |
struct tm * timeinfo;
|
141 |
time(&t); |
142 |
timeinfo=localtime(&t); |
143 |
file<<asctime(timeinfo); |
144 |
file<<"image : "<<argv[1]<<endl; |
145 |
file<<"_________________________________"<<endl;
|
146 |
file<<"Parameters"<<endl;
|
147 |
file<<"lambda : "<<lam<<endl;
|
148 |
file<<"alpha : "<<alf<<endl;
|
149 |
file<<"epsilon : "<<epsilon<<endl;
|
150 |
file<<"dt : "<<dt<<endl;
|
151 |
file<<"mu : "<<mu<<endl;
|
152 |
file<<"timestep_max : "<<timestep_max<<endl;
|
153 |
file<<"\nthreshold up : "<<t_up<<endl;
|
154 |
file<<"threshold down : "<<t_down<<endl;
|
155 |
file<<"beta : "<<beta<<endl;
|
156 |
file<<"perUp : "<<perUp<<endl;
|
157 |
file<<"perDown : "<<perDown<<endl;
|
158 |
|
159 |
ofstream bg_file; |
160 |
string bg_name=filename_txt+"_BGgrowth.txt"; |
161 |
bg_file.open(bg_name.c_str()); |
162 |
bg_file<<"it\tbg_growth"<<endl;
|
163 |
|
164 |
//-----------------------------------------Image Pre-processing
|
165 |
//add slices
|
166 |
img=add_side_slices(img,3);
|
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 |
//Save result
|
235 |
if((((it%50)==0)and(it!=0))or(contour_evolves==false)or(it==timestep_max-1)) |
236 |
{ |
237 |
CImg<unsigned char>segSave=remove_side_slices(segmented,3); |
238 |
segSave.save_inr(result_name.c_str(),tailleVoxel); |
239 |
segSave.assign(); |
240 |
string zip="gzip -f "+result_name; |
241 |
if(system(zip.c_str()));
|
242 |
} |
243 |
it+=1;
|
244 |
} |
245 |
|
246 |
clock_t end=clock(); |
247 |
double time=double(end-begin)/CLOCKS_PER_SEC; |
248 |
cout <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl; |
249 |
file <<"last iteration : "<<it-1<<endl; |
250 |
file <<"elapsed time : "<<time<<" sec ( ~ "<<time/60<<" mn ~ "<<time/60/60<<" h)"<<endl; |
251 |
|
252 |
file<<"width "<<img.width()<<endl;
|
253 |
file<<"height "<<img.height()<<endl;
|
254 |
file<<"depth "<<img.depth()<<endl;
|
255 |
|
256 |
file<<"number of pixel "<<img._width*img._height*img._depth<<endl;
|
257 |
|
258 |
file.close(); |
259 |
return 0; |
260 |
} |