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 <iostream>
00032 #include <fstream>
00033 #include <sstream>
00034 #include <iomanip>
00035
00036 #include <mln/value/int_u8.hh>
00037 #include <mln/value/rgb8.hh>
00038 #include <mln/literal/black.hh>
00039 #include <mln/literal/white.hh>
00040
00041 #include <mln/core/concept/function.hh>
00042 #include <mln/core/alias/point2d.hh>
00043 #include <mln/core/site_set/p_faces.hh>
00044 #include <mln/core/image/complex_image.hh>
00045
00046 #include <mln/core/image/complex_neighborhoods.hh>
00047 #include <mln/core/image/complex_neighborhood_piter.hh>
00048
00049 #include <mln/data/fill.hh>
00050
00051 #include <mln/norm/l2.hh>
00052
00053 #include <mln/morpho/closing/area.hh>
00054 #include <mln/morpho/watershed/flooding.hh>
00055
00056 #include <mln/convert/to.hh>
00057
00058 #include <mln/debug/iota.hh>
00059
00060
00061 struct colorize : mln::Function_v2v< colorize >
00062 {
00063 typedef mln::value::rgb8 result;
00064 colorize(unsigned max)
00065 : lut(max + 1)
00066 {
00067 lut[0] = mln::literal::black;
00068 for (unsigned i = 1; i <= max; ++i)
00069 lut[i] = result(100 + std::rand() % 150,
00070 100 + std::rand() % 150,
00071 100 + std::rand() % 150);
00072 }
00073 result operator()(unsigned i) const
00074 {
00075 return lut[i];
00076 }
00077 std::vector<result> lut;
00078 };
00079
00080
00081
00082 int main()
00083 {
00084 using namespace mln;
00085 using mln::value::int_u8;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 const unsigned D = 1;
00111
00112 topo::complex<D> c;
00113
00114 typedef point2d P;
00115 typedef geom::complex_geometry<D, P> G;
00116 G geom;
00117
00118
00119 typedef topo::n_face<0, D> vertex;
00120 typedef topo::n_face<1, D> edge;
00121
00122
00123 vertex v0 = c.add_face(); geom.add_location(point2d(0,1));
00124 vertex v1 = c.add_face(); geom.add_location(point2d(2,0));
00125 vertex v2 = c.add_face(); geom.add_location(point2d(2,2));
00126 vertex v3 = c.add_face(); geom.add_location(point2d(0,3));
00127
00128
00129 edge e0 = c.add_face(-v1 + v0);
00130 edge e1 = c.add_face(-v2 + v0);
00131 edge e2 = c.add_face(-v2 + v1);
00132 edge e3 = c.add_face(-v0 + v3);
00133 edge e4 = c.add_face(-v3 + v2);
00134
00135
00136
00137
00138
00139 p_complex<D, G> pc(c, geom);
00140
00141
00142
00143
00144
00145
00146
00147 typedef complex_image<D, G, unsigned> dist_ima_t;
00148
00149
00150 dist_ima_t dist_ima(pc);
00151 data::fill(dist_ima, 0u);
00152
00153
00154
00155
00156
00157
00158
00159 p_n_faces_fwd_piter<D, G> e(dist_ima.domain(), 1);
00160 typedef complex_lower_neighborhood<D, G> v_nbh_t;
00161 v_nbh_t v_nbh;
00162 mln_niter_(v_nbh_t) v(v_nbh, e);
00163 for_all(e)
00164 {
00165 v.start();
00166 point2d p1 = v.to_site().front();
00167 v.next();
00168 point2d p2 = v.to_site().front();
00169 v.next();
00170 mln_invariant(!v.is_valid());
00171
00172 dist_ima(e) = convert::to<unsigned>(10 * norm::l2_distance(p1.to_vec(), p2.to_vec()));
00173 }
00174
00175
00176 p_n_faces_fwd_piter<D, G> v_(dist_ima.domain(), 0);
00177 for_all(v_)
00178 dist_ima(v_) = mln_max(mln_value_(dist_ima_t));
00179
00180
00181
00182 typedef complex_lower_dim_connected_n_face_neighborhood<D, G> nbh_t;
00183 nbh_t nbh;
00184
00185 mln_niter_(nbh_t) ne(nbh, e);
00186 for_all(e)
00187 {
00188 std::cout << "dist_ima(" << e << ") = " << dist_ima(e)
00189 << " -- adjacent edges :" << std::endl;
00190 for_all(ne)
00191 std::cout << " " << ne << std::endl;
00192 }
00193
00194
00195
00196
00197
00198
00199 dist_ima_t closed_dist_ima = morpho::closing::area(dist_ima, nbh, 1);
00200
00201
00202
00203
00204
00205
00206 typedef unsigned wst_val_t;
00207 wst_val_t nbasins;
00208 typedef complex_image<D, G, wst_val_t> wst_ima_t;
00209 wst_ima_t wshed = morpho::watershed::flooding(closed_dist_ima, nbh, nbasins);
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 wst_val_t actual_nbasins = nbasins - c.nfaces_of_static_dim<0>();
00237 std::cout << "nbasins = " << actual_nbasins << std::endl;
00238
00239
00240 colorize color(nbasins);
00241
00242 std::ofstream g("wst.neato");
00243 g << "graph wst" << std::endl
00244 << "{" << std::endl
00245 << " graph [bgcolor = \"#000000\"]" << std::endl
00246 << " edge [color = \"#FFFFFF\"]" << std::endl
00247 << " node [color = \"#FFFFFF\", fontcolor = \"#FFFFFF\" ]" << std::endl;
00248
00249
00250 typedef complex_higher_neighborhood<D, G> e_nbh_t;
00251 e_nbh_t e_nbh;
00252 mln_niter_(e_nbh_t) v_e(e_nbh, v_);
00253 for_all(v_)
00254 {
00255
00256 value::rgb8 basin_color = literal::white;
00257 for_all(v_e)
00258 if (wshed(v_e) != 0)
00259 {
00260 basin_color = color(wshed(v_e));
00261 break;
00262 }
00263 std::ostringstream basin_color_str;
00264 basin_color_str << '#'
00265 << std::hex
00266 << std::setfill('0')
00267 << std::setw(2) << basin_color.red()
00268 << std::setw(2) << basin_color.green()
00269 << std::setw(2) << basin_color.blue()
00270 << std::dec;
00271
00272 g << " v" << v_.unproxy_().face_id()
00273 << " [pos = \""
00274 << std::fixed << std::setprecision(1)
00275 << (float)v_.to_site().front()[1] << ", "
00276 << -(float)v_.to_site().front()[0]
00277 << "\", color = \"" << basin_color_str.str()
00278 << "\", fillcolor = \"" << basin_color_str.str()
00279 << "\", pin = \"true\", style=\"filled,setlinewidth(3)\"];"
00280 << std::endl;
00281 }
00282
00283 for_all(e)
00284 {
00285 value::rgb8 basin_color = color(wshed(e));
00286 std::ostringstream basin_color_str;
00287 basin_color_str << '#'
00288 << std::hex
00289 << std::setfill('0')
00290 << std::setw(2) << basin_color.red()
00291 << std::setw(2) << basin_color.green()
00292 << std::setw(2) << basin_color.blue()
00293 << std::dec;
00294
00295
00296 v.start();
00297 topo::face<1> v1 = v.unproxy_().face();
00298 point2d p1 = v.to_site().front();
00299 v.next();
00300 topo::face<1> v2 = v.unproxy_().face();
00301 point2d p2 = v.to_site().front();
00302 v.next();
00303 mln_invariant(!v.is_valid());
00304
00305
00306 g << " v" << v1.face_id() << " -- v" << v2.face_id() << " ";
00307 if (wshed(e) == 0)
00308 g << "[color = \"#FFFFFF\"];" << std::endl;
00309 else
00310 g << "[color = \"" << basin_color_str.str()
00311 << "\", style=\"setlinewidth(3)\"];" << std::endl;
00312 }
00313
00314 g << "}" << std::endl;
00315 g.close();
00316 }