00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00030 
00031 #include <cstdlib>
00032 #include <cmath>
00033 
00034 #include <utility>
00035 #include <iostream>
00036 
00037 #include <mln/core/image/complex_image.hh>
00038 #include <mln/core/image/complex_neighborhoods.hh>
00039 
00040 #include <mln/morpho/closing/area.hh>
00041 #include <mln/morpho/meyer_wst.hh>
00042 
00043 #include <mln/literal/white.hh>
00044 
00045 #include <mln/io/off/load.hh>
00046 #include <mln/io/off/save.hh>
00047 
00048 
00049 
00050 int main(int argc, char* argv[])
00051 {
00052   if (argc != 4)
00053     {
00054       std::cerr << "usage: " << argv[0] << " input.off lambda output.off"
00055                 << std::endl;
00056       std::exit(1);
00057     }
00058 
00059   std::string input_filename = argv[1];
00060   unsigned lambda = atoi(argv[2]);
00061   std::string output_filename = argv[3];
00062 
00063   
00064 
00065 
00066 
00067   
00068   typedef mln::float_2complex_image3df ima_t;
00069   
00070   static const unsigned D = ima_t::dim;
00071   
00072   typedef mln_geom_(ima_t) G;
00073 
00074   ima_t input;
00075   mln::io::off::load(input, input_filename);
00076 
00077   
00078   mln::p_n_faces_fwd_piter<D, G> e(input.domain(), 1);
00079   typedef mln::complex_higher_neighborhood<D, G> adj_polygons_nbh_t;
00080   adj_polygons_nbh_t adj_polygons_nbh;
00081   mln_niter_(adj_polygons_nbh_t) adj_p(adj_polygons_nbh, e);
00082   
00083   for_all(e)
00084   {
00085     float s = 0.0f;
00086     unsigned n = 0;
00087     for_all(adj_p)
00088     {
00089       s += input(adj_p);
00090       ++n;
00091     }
00092     input(e) = s / n;
00093     
00094     mln_invariant(n <= 2);
00095   }
00096 
00097   
00098 
00099 
00100 
00102   typedef
00103     mln::complex_higher_dim_connected_n_face_neighborhood<D, G>
00104     adj_edges_nbh_t;
00105   adj_edges_nbh_t adj_edges_nbh;
00106 
00107   ima_t closed_input = mln::morpho::closing::area(input, adj_edges_nbh, lambda);
00108 
00109   
00110 
00111 
00112 
00113   
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121   
00122   typedef unsigned wst_val_t;
00123   wst_val_t nbasins;
00124   typedef mln::unsigned_2complex_image3df wst_ima_t;
00125   wst_ima_t wshed =
00126     mln::morpho::meyer_wst(closed_input, adj_edges_nbh, nbasins);
00127   std::cout << "nbasins = " << nbasins << std::endl;
00128 
00129   
00130   for_all(e)
00131     if (wshed(e) != 0)
00132       for_all(adj_p)
00133         wshed(adj_p) = wshed(e);
00134 
00135   
00136 
00137 
00138 
00139   mln::rgb8_2complex_image3df output(wshed.domain());
00140   mln::data::fill(output, mln::literal::white);
00141 
00142   
00143   
00144   std::vector<mln::value::rgb8> basin_color (nbasins + 1);
00145   for (unsigned i = 0; i <= nbasins; ++i)
00146     basin_color[i] = mln::value::rgb8(random() % 256,
00147                                       random() % 256,
00148                                       random() % 256);
00149   mln_piter_(ima_t) f(wshed.domain());
00150   for_all(f)
00151     output(f) = basin_color[wshed(f)];
00152 
00153   mln::io::off::save(output, output_filename);
00154 }