Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 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 }