00001 #include <Segmentation.h>
00002 #include <SegmentationModule/RegularizationModule.h>
00003 #include <SegmentationModule/SoftShapeModule.h>
00004 #include <SegmentationModule/BalloonModule.h>
00005 #include <SegmentationModule/GaussianRegionModule.h>
00006 #include <SegmentationModule/CircularityModule.h>
00007
00010 int main(int argc, char* argv[])
00011 {
00012 using namespace Images;
00013 using namespace levelset;
00014 using namespace segmentation;
00015
00016 #if 0 // creation sulci
00017
00018 const Dimension nx = 300;
00019 const Dimension ny = 300;
00020 const Dimension dims[] = {nx, ny};
00021 Shape<2> shape(dims);
00022
00023
00024 FastMarchingInitializer<2,float> init;
00025 LevelSet<2,float> phi(shape, (double) 2.0);
00026 Segmentation<2,float> segm(&phi, &init);
00027
00028 LevelSet<2,float>::Index c(150, 150);
00029 phi.init_sphere(c, 70);
00030
00031
00032 RegularizationModule<2,float> regul(1.f);
00033 BalloonModule<2,float> balloon(1.f);
00034
00035 BaseImage<2,float> weight(shape);
00036 weight = 0;
00037
00038
00039 int height = 110;
00040 int width = 30;
00041 int up = 130;
00042 int down = 240;
00043 int left = 135;
00044 int right = 165;
00045
00046 for (BaseImage<2, float>::iterator<domain> ind=weight.begin() ; ind!=weight.end() ; ++ind){
00047 if (ind(1)>left && ind(1)<right && ind(2)>up && ind(2)<down){
00048 int x = ind(1) - left;
00049 int y = ind(2) - up;
00050 if( fabs(x - width/2) < y*(width/2)/height)
00051 weight(ind) = -1.f;
00052 }
00053 }
00054
00055 balloon.set_image_weight(&weight);
00056
00057 segm.add_module(regul);
00058 segm.add_module(balloon);
00059
00060 timeval _time;
00061 Globals::initChrono(_time);
00062
00063
00064 segm.segment(1000, "result_softshapeprior/2D_brain_sulci/soft_shape_2D_");
00065
00066 Globals::printChrono("\n => Soft Shape Prior : ", _time);
00067 Globals::initChrono(_time);
00068
00069 std::cout << "OK" << std::endl;
00070
00071 system("./convert_cimg.sh png result_softshapeprior/2D_brain_sulci/soft_shape_2D_*ppm");
00072
00073
00074 return 0;
00075 #endif
00076
00077 #if 1 // circle creation
00078
00079 BaseImage<2, float> image;
00080
00081 std::ifstream f("image/sylvain/S_test_G_1841_ROIs/S_test_G_1841_ROI1.inr");
00082 f >> format("inrimage") >> image;
00083 Shape<2> shape = image.shape();
00084
00085
00086 FastMarchingInitializer<2,float> init;
00087 LevelSet<2,float> phi(shape, (double) 3.0);
00088 Segmentation<2,float> segm(&phi, &init, &image);
00089
00090
00091
00092 phi.init_small_spheres(20, 50, init);
00093
00094
00095
00096
00097
00098 RegularizationModule<2,float> regul(1.f);
00099 CircularityModule<2,float> circular(0.00005f, true);
00100
00101 GaussianRegionModule<2,float,float> gauss(&image, 0.113f, true);
00102
00103 segm.add_module(regul);
00104 segm.add_module(circular);
00105
00106 segm.add_module(gauss);
00107
00108 timeval _time;
00109 Globals::initChrono(_time);
00110
00111 Segmentation<2,float>::RGBPixel neg(215,152,0);
00112 Segmentation<2,float>::RGBPixel pos(255,252,0);
00113
00114 std::ostringstream name;
00115 name.str("");
00116 name << "mkdir result_softshapeprior/circle/";
00117
00118 name.str("");
00119 name << "result_softshapeprior/circle/";
00120
00121 segm.segment(3000, name.str().c_str(), neg, pos);
00122
00123 name.str("");
00124 name << "convert_cimg.sh png result_softshapeprior/circle/*ppm";
00125 system(name.str().c_str());
00126
00127 Globals::printChrono("\n => Circular : ", _time);
00128 Globals::initChrono(_time);
00129
00130 std::cout << "OK" << std::endl;
00131
00132 return 0;
00133 #endif
00134
00135 #if 0 // 2D version
00136
00137 const Dimension nx = 300;
00138 const Dimension ny = 300;
00139 const Dimension dims[] = {nx, ny};
00140 Shape<2> shape(dims);
00141
00142
00143 FastMarchingInitializer<2,float> init;
00144 LevelSet<2,float> phi(shape, (double) 2.0);
00145 Segmentation<2,float> segm(&phi, &init);
00146
00147
00148
00149
00150
00151
00152
00153 LevelSet<2,float>::Index c(150, 150);
00154 phi.init_sphere(c, 70);
00155
00156
00157
00158
00159
00160
00161 RegularizationModule<2,float> regul(1.f);
00162 SoftShapeModule<2,float> soft_shape(1.f);
00163 BalloonModule<2,float> balloon(1.f);
00164
00165
00167 #if 0
00168 float radius1 = 80;
00169 float ampl1 = 5;
00170 float freq1 = 8;
00171 float radius2 = 45;
00172 float ampl2 = 5;
00173 float freq2 = 5;
00174
00175 BaseImage<2,bool> mask(shape);
00176 for (BaseImage<2, float>::iterator<domain> ind=mask.begin() ; ind!=mask.end() ; ++ind){
00177 double pol = atan2(ind(1) - 150, ind(2) - 150);
00178 if ( (sqrt( (double) Maths::sqr(ind(1) - 150) + Maths::sqr( ind(2) - 150)) < ampl1*sin(freq1*pol) + radius1 + 3*sin(10*pol + 2))
00179 && (sqrt( (double) Maths::sqr(ind(1) - 150) + Maths::sqr( ind(2) - 150)) > ampl2*sin(freq2*pol) + radius2 + 3*sin(10*pol + 2)) )
00180 mask(ind) = true;
00181 else
00182 mask(ind) = false;
00183 }
00184 segm.set_mask(mask);
00185 #endif
00186 float radius = 65;
00187 float width = 20;
00188 BaseImage<2,float> weight(shape);
00189 for (BaseImage<2, float>::iterator<domain> ind=weight.begin() ; ind!=weight.end() ; ++ind){
00190 float dist = sqrt( (double) Maths::sqr(ind(1) - 150) + Maths::sqr( ind(2) - 150)) - radius;
00191 if ( fabs(dist) < width)
00192 weight(ind) = (width - fabs(dist))/width;
00193 else
00194 weight(ind) = 0;
00195 }
00196 soft_shape.set_image_weight(&weight);
00197 balloon.set_image_weight(&weight);
00199
00200 soft_shape.set_sigma(7.0f);
00201 soft_shape.p() = 2;
00202
00203 segm.add_module(regul);
00204 segm.add_module(soft_shape);
00205 segm.add_module(balloon);
00206
00207 timeval _time;
00208 Globals::initChrono(_time);
00209
00210
00211 segm.segment(1000, "result_softshapeprior/2D_brain_weight/soft_shape_2D_");
00212
00213 Globals::printChrono("\n => Soft Shape Prior : ", _time);
00214 Globals::initChrono(_time);
00215
00216 std::cout << "OK" << std::endl;
00217
00218 system("./convert_cimg.sh png result_softshapeprior/2D_brain_weight/soft_shape_2D_*ppm");
00219
00220
00221 return 0;
00222 #endif
00223
00224 #if 0 // 3D version
00225
00226
00227 const Dimension nx = 300;
00228 const Dimension ny = 300;
00229 const Dimension nz = 300;
00230 const Dimension dims[] = {nx, ny, nz};
00231 Shape<3> shape(dims);
00232
00233
00234 FastMarchingInitializer<3,float> init;
00235 LevelSet<3,float> phi(shape, (double) 2.0);
00236 Segmentation<3,float> segm(&phi, &init);
00237
00238
00239
00240
00241 BaseImage<3, float> image;
00242 std::ifstream f("a.inr");
00243 f >> format("inrimage") >> image;
00244 phi.init_image(image, init);
00245
00246
00247 RegularizationModule<3,float> regul(1.f);
00248 SoftShapeModule<3,float> soft_shape(1.f);
00249 BalloonModule<3,float> balloon(1.f);
00250
00252 #if 0
00253 float radius1 = 80;
00254 float ampl1 = 5;
00255 float freq1 = 8;
00256 float radius2 = 45;
00257 float ampl2 = 5;
00258 float freq2 = 5;
00259 float radius3 = 45;
00260
00261 float freq3 = 5;
00262 BaseImage<3,bool> mask(shape);
00263 for (BaseImage<3, float>::iterator<domain> ind=mask.begin() ; ind!=mask.end() ; ++ind){
00264 double pol1 = atan2(ind(1) - 150, ind(2) - 150);
00265 double pol2 = atan2(ind(1) - 150, ind(3) - 150);
00266 if ( (sqrt( (double) Maths::sqr(ind(1) - 150) + Maths::sqr( ind(2) - 150) + Maths::sqr( ind(3) - 150)) < radius1 + ampl1*sin(freq1*pol1)*sin(freq3*pol2))
00267 && (sqrt( (double) Maths::sqr(ind(1) - 150) + Maths::sqr( ind(2) - 150) + Maths::sqr( ind(3) - 150)) > radius2 + ampl2*sin(freq2*pol1)*sin(freq3*pol2)) )
00268 mask(ind) = true;
00269 else
00270 mask(ind) = false;
00271 }
00272 segm.set_mask(mask);
00273 #endif
00274 float radius = 65;
00275
00276 BaseImage<3,float> weight(shape);
00277 for (BaseImage<3, float>::iterator<domain> ind=weight.begin() ; ind!=weight.end() ; ++ind){
00278 float dist = sqrt((double) Maths::sqr(ind(1) - 150) + Maths::sqr( ind(2) - 150) + Maths::sqr( ind(3) - 150)) - radius;
00279 weight(ind) = exp(-0.5f*Maths::sqr(dist)/64);
00280
00281
00282
00283
00284 }
00285 soft_shape.set_image_weight(&weight);
00286 balloon.set_image_weight(&weight);
00287
00288 std::ofstream fw("weight.inr");
00289 fw << format("inrimage") << weight;
00290 fw.close();
00292
00293 soft_shape.set_sigma(7.f);
00294 soft_shape.p() = 2;
00295
00296 segm.add_module(regul);
00297 segm.add_module(soft_shape);
00298 segm.add_module(balloon);
00299
00300
00301 segm.segment(1000, "result_softshapeprior/3D_brain_weight/soft_shape_3D_");
00302
00303 std::cout << "OK" << std::endl;
00304
00305
00306 return 0;
00307
00308 #endif
00309
00310 #if 0 // test changement topologie
00311
00312
00313 FastMarchingInitializer<2,float> init;
00314 LevelSet<2,float> phi;
00315 Segmentation<2,float> segm(&phi, &init);
00316
00317 phi.thickness() = 2;
00318
00319 BaseImage<2, float> image;
00320 std::ifstream f("phi_000925.inr");
00321 f >> format("inrimage") >> image;
00322 phi.init_image(image, init);
00323
00324
00325 RegularizationModule<2,float> regul(1.f);
00326 SoftShapeModule<2,float> soft_shape(1.f, 5.0f);
00327 BalloonModule<2,float> balloon(0.75f);
00328
00329 soft_shape.set_sigma(5.0f);
00330 soft_shape.p() = 2;
00331
00332 segm.add_module(regul);
00333
00334
00335
00336 timeval _time;
00337 Globals::initChrono(_time);
00338
00339
00340
00341 segm.segment(10, "soft_shape_2D_");
00342
00343 segm.add_module(soft_shape);
00344 segm.add_module(balloon);
00345
00346
00347 segm.segment(300, "soft_shape_2D_");
00348
00349 Globals::printChrono("\n => Soft Shape Prior : ", _time);
00350 Globals::initChrono(_time);
00351
00352 std::cout << "OK" << std::endl;
00353
00354
00355 return 0;
00356
00357 #endif
00358
00359
00360 }