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