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
00029
00030
00031 #include <iostream>
00032
00033 #include <mln/core/image/image2d.hh>
00034 #include <mln/core/alias/neighb2d.hh>
00035 #include <mln/core/alias/window2d.hh>
00036 #include <mln/core/image/dmorph/image_if.hh>
00037 #include <mln/core/var.hh>
00038
00039 #include <mln/accu/center.hh>
00040 #include <mln/accu/stat/mean.hh>
00041
00042 #include <mln/io/ppm/save.hh>
00043 #include <mln/io/ppm/load.hh>
00044 #include <mln/io/pgm/load.hh>
00045 #include <mln/io/pgm/save.hh>
00046
00047 #include <mln/value/rgb8.hh>
00048 #include <mln/value/int_u8.hh>
00049 #include <mln/value/label_16.hh>
00050
00051 #include <mln/data/transform.hh>
00052
00053 #include <mln/labeling/wrap.hh>
00054 #include <mln/labeling/mean_values.hh>
00055
00056 #include <mln/convert/to_fun.hh>
00057
00058 #include <mln/morpho/gradient.hh>
00059 #include <mln/morpho/closing/area.hh>
00060 #include <mln/morpho/watershed/flooding.hh>
00061 #include <mln/morpho/elementary/dilation.hh>
00062
00063 #include <mln/make/vertex_image.hh>
00064 #include <mln/make/edge_image.hh>
00065 #include <mln/make/region_adjacency_graph.hh>
00066
00067 #include <mln/math/diff_abs.hh>
00068
00069 #include <mln/debug/draw_graph.hh>
00070
00071 namespace mln
00072 {
00073
00075 struct dist : Function_vv2v< dist >
00076 {
00077
00078 typedef value::int_u8 result;
00079
00080 result operator()(const value::rgb8& c1, const value::rgb8& c2) const
00081 {
00082 unsigned d = math::diff_abs(c1.red(), c2.red());
00083 unsigned d_;
00084 d_ = math::diff_abs(c1.green(), c2.green());
00085
00086 if (d_ > d)
00087 d = d_;
00088
00089 d_ = math::diff_abs(c1.blue(), c2.blue());
00090
00091 if (d_ > d)
00092 d = d_;
00093 return d;
00094 }
00095
00096 };
00097
00099 template <typename I, typename V>
00100 struct edge_to_color : Function_v2v< edge_to_color<I,V> >
00101 {
00102 typedef V result;
00103
00104 edge_to_color(const I& ima)
00105 : ima_(ima)
00106 {
00107 }
00108
00109 V
00110 operator()(const unsigned& e) const
00111 {
00112 return convert::to<V>(ima_(e));
00113 }
00114
00115 I ima_;
00116 };
00117
00118
00120 template <typename I, typename V, typename E>
00121 inline
00122 image2d<mln_value(I)>
00123 make_debug_graph_image(const I& input,
00124 const V& ima_v, const E& ima_e,
00125 const value::rgb8& bg)
00126 {
00127 image2d<mln_value(I)> ima;
00128 initialize(ima, input);
00129
00130 data::fill(ima, bg);
00131 debug::draw_graph(ima, ima_v.domain(),
00132 pw::cst(mln_value(I)(literal::green)),
00133 edge_to_color<E, mln_value(I)>(ima_e));
00134
00136 dpoint2d tl(-3,-3);
00137 dpoint2d br(3,3);
00138 mln_piter(V) p(ima_v.domain());
00139 for_all(p)
00140 if (p.id() != 0)
00141 {
00142 box2d b(p + tl, p + br);
00143 b.crop_wrt(ima.domain());
00144 data::fill((ima | b).rw(), convert::to<mln_value(I)>(ima_v(p)));
00145 }
00146
00147 return ima;
00148 }
00149
00150 }
00151
00152 int main()
00153 {
00154 using namespace mln;
00155 using value::int_u8;
00156 using value::rgb8;
00157 using value::label_16;
00158
00159 image2d<int_u8> input_pgm;
00160 io::pgm::load(input_pgm, "house.pgm");
00161
00162 image2d<rgb8> input_ppm;
00163 io::ppm::load(input_ppm, "house.ppm");
00164
00165
00167 image2d<int_u8> grad = morpho::gradient(input_pgm, win_c4p());
00168 io::pgm::save(grad, "tmp_grad_c4p.pgm");
00169
00171 image2d<int_u8> clo = morpho::closing::area(grad, c4(), 25);
00172 io::pgm::save(clo, "tmp_clo_a100.pgm");
00173
00175 label_16 nbasins;
00176 image2d<label_16> wshed = morpho::watershed::flooding(clo, c4(), nbasins);
00177
00180 io::ppm::save(labeling::mean_values(input_ppm, wshed, nbasins),
00181 "tmp_wshed_mean_colors.ppm");
00182
00183
00185 data::fill((input_ppm | (pw::value(wshed) == 0u)).rw(), literal::yellow);
00186 io::ppm::save(input_ppm, "tmp_wshed_color.ppm");
00187
00188
00190 util::graph g = make::region_adjacency_graph(wshed, c4(), nbasins);
00191
00192
00195 util::array<point2d>
00196 basin_centers = labeling::compute(accu::center<point2d, point2d>(),
00197 wshed, nbasins);
00198
00201 typedef accu::stat::mean<rgb8, mln_sum_(rgb8), rgb8>
00202 accu_mean_t;
00203 util::array<rgb8>
00204 mean_values = labeling::compute(accu_mean_t(), input_ppm, wshed, nbasins);
00205
00207 mln_VAR(ima_v, make::vertex_image(g, basin_centers, mean_values));
00208
00210 mln_VAR(ima_e, make::edge_image(ima_v, dist()));
00211
00212 io::ppm::save(make_debug_graph_image(input_ppm, ima_v, ima_e, literal::white),
00213 "tmp_wst_rag_graph_image_white.ppm");
00214 }