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