Jerome Piovano

Research in Medical Imaging and Computer Vision

2D_SoftShapePrior.C

Use of a 2D soft shape prior

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         // initialization of the segmentations objects
00024         FastMarchingInitializer<2,float> init;                         // Fast Marching Initializer
00025         LevelSet<2,float>                phi(shape, (double) 2.0);     // Level Set of band thickness = 2
00026         Segmentation<2,float>            segm(&phi, &init);            // Segmentation  
00027 
00028         LevelSet<2,float>::Index c(150, 150);
00029         phi.init_sphere(c, 70);
00030  
00031         // initialization of the modules 
00032         RegularizationModule<2,float>     regul(1.f);             // parameters : weight
00033         BalloonModule<2,float>            balloon(1.f);           // parameters : weight
00034 
00035         BaseImage<2,float> weight(shape);
00036         weight = 0;
00037 
00038         // bbox
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         // launch the segmentation 
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         //std::ifstream f("image/test_circularite.inr");
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         // initialization of the segmentations objects
00086         FastMarchingInitializer<2,float> init;                         // Fast Marching Initializer
00087         LevelSet<2,float>                phi(shape, (double) 3.0);     // Level Set of band thickness = 2
00088         Segmentation<2,float>            segm(&phi, &init, &image);    // Segmentation  
00089 
00090         // initalization of the level set
00091         //phi.init_border(40, init);
00092         phi.init_small_spheres(20, 50, init);
00093         //LevelSet<2,float>::Index tl(50, 50);
00094         //LevelSet<2,float>::Index br(250, 250);
00095         //phi.init_rectangle(tl, br, init, true);
00096         
00097         // initialization of the modules 
00098         RegularizationModule<2,float>        regul(1.f);                   // parameters : weight
00099         CircularityModule<2,float>           circular(0.00005f, true);        // parameters : weight
00100         //BalloonModule<2,float>               balloon(0.2f);                // parameters : weight
00101         GaussianRegionModule<2,float,float>  gauss(&image, 0.113f, true);  // parameters : image, weight
00102 
00103         segm.add_module(regul);
00104         segm.add_module(circular);
00105         //segm.add_module(balloon);
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         // initialization of the segmentations objects
00143         FastMarchingInitializer<2,float> init;                         // Fast Marching Initializer
00144         LevelSet<2,float>                phi(shape, (double) 2.0);     // Level Set of band thickness = 2
00145         Segmentation<2,float>            segm(&phi, &init);            // Segmentation  
00146 
00147         // initalization of the level set
00148         //phi.init_small_spheres(15, 40);    // parameters : radius, windows 
00149         //phi.init_small_spheres(10, 60);
00150         //LevelSet<2,float>::Index tl(100,100);
00151         //LevelSet<2,float>::Index br(140,140);
00152         //phi.init_rectangle(tl, br);
00153         LevelSet<2,float>::Index c(150, 150);
00154         phi.init_sphere(c, 70);
00155         //BaseImage<2, float> image;
00156         //std::ifstream f("soft_shape_2D_phi_001000.inr");
00157         //f >> format("inrimage") >> image;
00158         //phi.init_image(image, init);
00159  
00160         // initialization of the modules 
00161         RegularizationModule<2,float>     regul(1.f);             // parameters : weight
00162         SoftShapeModule<2,float>          soft_shape(1.f);        // parameters : weight, radius of tubular shape
00163         BalloonModule<2,float>            balloon(1.f);           // parameters : weight
00164         //BalloonModule<2,float>            balloon(0.75f);           // parameters : weight
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         // launch the segmentation 
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         // initialization of the segmentations objects
00234         FastMarchingInitializer<3,float> init;                         // Fast Marching Initializer
00235         LevelSet<3,float>                phi(shape, (double) 2.0);     // Level Set of band thickness = 2
00236         Segmentation<3,float>            segm(&phi, &init);            // Segmentation
00237 
00238         // initalization of the level set
00239         //LevelSet<3,float>::Index c(150, 150, 150);
00240         //phi.init_sphere(c, 60);
00241         BaseImage<3, float> image;
00242         std::ifstream f("a.inr");
00243         f >> format("inrimage") >> image;
00244         phi.init_image(image, init);
00245 
00246         // initialization of the modules
00247         RegularizationModule<3,float>     regul(1.f);             // parameters : weight
00248         SoftShapeModule<3,float>          soft_shape(1.f);  // parameters : weight, radius of tubular shape
00249         BalloonModule<3,float>            balloon(1.f);         // parameters : weight
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         //float ampl3   = 5;
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         //float width  = 20;
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                 //if ( fabs(dist) < width)
00281                 //       weight(ind) = (width - fabs(dist))/width;
00282                 //else  
00283                 //      weight(ind) = 0;
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         // launch the segmentation
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         // initialization of the segmentations objects
00313         FastMarchingInitializer<2,float> init;                    // Fast Marching Initializer
00314         LevelSet<2,float>                phi;                     // Level Set of band thickness = 2
00315         Segmentation<2,float>            segm(&phi, &init);       // Segmentation
00316 
00317         phi.thickness() = 2;
00318         // binary image
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         // initialization of the modules 
00325         RegularizationModule<2,float>     regul(1.f);             // parameters : weight
00326         SoftShapeModule<2,float>          soft_shape(1.f, 5.0f);  // parameters : weight, radius of tubular shape
00327         BalloonModule<2,float>            balloon(0.75f);         // parameters : weight
00328 
00329         soft_shape.set_sigma(5.0f); 
00330         soft_shape.p() = 2;
00331 
00332         segm.add_module(regul);
00333         //segm.add_module(soft_shape); 
00334         //segm.add_module(balloon);
00335 
00336         timeval _time;
00337         Globals::initChrono(_time); 
00338 
00339         // d aborsd une fois pour virer le bruit
00340         // launch the segmentation 
00341         segm.segment(10, "soft_shape_2D_");
00342 
00343         segm.add_module(soft_shape); 
00344         segm.add_module(balloon);
00345         
00346         // vraie segmentation
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 }

For further information, please contact Jerome Piovano - Last update 2008-02-08