2D_FastMarching.C
2D Fast Marching
00001 #include <Image.H>
00002 #include <FastMarching.h>
00003 #include <Globals.h>
00004 #include <Schemes.h>
00005
00006 #include <cmath>
00007 #include <sstream>
00008
00011 int main(int argc, char* argv[])
00012 {
00013 using namespace Images;
00014 using namespace levelset;
00015
00016 #if 1 // random seeds/forbidde circles
00017
00018 const Dimension nx = 500;
00019 const Dimension ny = 500;
00020 const Dimension dims[] = {nx, ny};
00021 Shape<2> shape(dims);
00022
00023 BaseImage<2,float> image(shape);
00024 Eikonal<2,float> march(&image);
00025
00026
00027 image = 0.0;
00028
00029 srand(10000);
00030
00031
00032 for (int k = 0 ; k < 20 ; k++){
00033
00034 int x = static_cast<int>(nx * (double)rand() / ((double)(RAND_MAX)+(double)(1)));
00035 int y = static_cast<int>(ny * (double)rand() / ((double)(RAND_MAX)+(double)(1)));
00036 BaseImage<2,float>::Index ind(x, y);
00037
00038 march.add_trial_point(ind);
00039 }
00040
00041
00042 for (int k = 0 ; k < 20 ; k++){
00043
00044 int x = static_cast<int>(30 + (nx-60) * (double)rand() / ((double)(RAND_MAX)+(double)(1)));
00045 int y = static_cast<int>(30 + (ny-60) * (double)rand() / ((double)(RAND_MAX)+(double)(1)));
00046 int r = static_cast<int>( 30 * (double)rand() / ((double)(RAND_MAX)+(double)(1)));
00047
00048 for (int i = x-r ; i < x+r ; i++)
00049 for (int j = y-r ; j< y+r ; j++)
00050 if(sqrt( (double) Maths::sqr(i - x) + Maths::sqr( j - y)) < r){
00051 BaseImage<2,float>::Index ind(i, j);
00052 march.add_forbidden_point(ind);
00053 }
00054 }
00055
00056 timeval _time;
00057 Globals::initChrono(_time);
00058
00059
00060 march.run();
00061
00062 Globals::printChrono("\n => FastMarching : ", _time);
00063 Globals::initChrono(_time);
00064
00065 std::ofstream f("2D_FastMarching.inr");
00066 f << format("inrimage") << image;
00067 f.close();
00068
00069 return 0;
00070 #endif
00071
00072 #if 0 // maze + backtrack
00073
00074 BaseImage<2, float> maze;
00075 std::ifstream f("image/maze.inr");
00076 f >> format("inrimage") >> maze;
00077
00078 BaseImage<2,float> image(maze.shape());
00079 Eikonal<2,float> march(&image);
00080
00081 image = 0.0;
00082
00083
00084 for (BaseImage<2, float>::iterator<domain> ind=maze.begin() ; ind!=maze.end() ; ++ind)
00085 if (maze(ind) > 0) march.add_forbidden_point(ind.position());
00086
00087
00088 BaseImage<2,float>::Index ind(25, 5);
00089 march.add_trial_point(ind);
00090 march.run();
00091
00092 std::ofstream f_fm("2D_FastMarching.inr");
00093 f_fm<< format("inrimage") << image;
00094 f_fm.close();
00095
00096 #if 1
00097
00098 BaseImage<2,float> backtrack = maze.copy();
00099 Globals::normalize<2,float>(backtrack, 0, 200);
00100
00101 BaseImage<2,float>::Index ind2(240, 495);
00102 BaseImage<2,float>::Index min;
00103 int inf_coords[2];
00104 int sup_coords[2];
00105 while (ind2 != ind){
00106 backtrack(ind2) = 255;
00107 Schemes<2,float>::find_neighbours(image, ind2, inf_coords, sup_coords);
00108 min = ind2;
00109 for (int i=0 ; i<2 ; i++){
00110 BaseImage<2,float>::Index inf = ind2;
00111 BaseImage<2,float>::Index sup = ind2;
00112 inf(i+1) = inf_coords[i];
00113 sup(i+1) = sup_coords[i];
00114
00115 if (march(inf) != Eikonal<2,float>::eForbidden && image(inf)<image(min)) min = inf;
00116 if (march(sup) != Eikonal<2,float>::eForbidden && image(sup)<image(min)) min = sup;
00117 }
00118 ind2 = min;
00119 }
00120
00121 std::ofstream f_backtrack("backtrack.inr");
00122 f_backtrack << format("inrimage") << backtrack;
00123 f_backtrack.close();
00124 #endif
00125
00126 return 0;
00127 #endif
00128 }
00129