• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

complex_image_morpho.cc

00001 // Copyright (C) 2008, 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 #include <iostream>
00031 
00032 #include <mln/value/int_u8.hh>
00033 #include <mln/core/alias/point2d.hh>
00034 
00035 #include <mln/core/site_set/p_faces.hh>
00036 #include <mln/core/image/complex_image.hh>
00037 
00038 // FIXME: Include these elsewhere? (In complex_image.hh?)
00039 #include <mln/core/image/complex_windows.hh>
00040 #include <mln/core/image/complex_window_piter.hh>
00041 
00042 #include <mln/debug/iota.hh>
00043 
00044 #include <mln/morpho/erosion.hh>
00045 #include <mln/morpho/dilation.hh>
00046 
00047 /* FIXME: Factor common parts with
00048    milena/tests/core/image/complex_image.cc  */
00049 
00050 // Forward declaration.
00051 template <typename I, typename W>
00052 void test_morpho(const mln::Image<I>& ima, const mln::Window<W> win,
00053                  const std::string& comment);
00054 
00055 
00056 int main()
00057 {
00058   using namespace mln;
00059 
00060   /*----------.
00061   | Complex.  |
00062   `----------*/
00063 
00064   /* A 2-d (simplicial) complex and its adjacency graph.
00065 
00066        c   0     1     2     3
00067      r .------------------------
00068        |        v0      e3     v3
00069      0 |         o-----------o                v0----e3----v3      
00070        |        / \ ,-----. /                / \    |    /   
00071        |       / . \ \ t1/ /                /   \   t1  /    
00072      1 |   e0 / / \ e1\ / / e4             e0.  ,e1ยด  `e4  
00073        |     / /t0 \ \ ' /                /   t0  \   /      
00074        |    / `-----' \ /                /    |    \ /       
00075      2 |   o-----------o                v1----e2----v2
00076        | v1     e2      v2
00077 
00078        v = vertex
00079        e = edge
00080        t = triangle
00081   */
00082 
00083 
00084   const unsigned D = 2;
00085 
00086   topo::complex<D> c;
00087 
00088   // 0-faces (points).
00089   topo::n_face<0, D> v0 = c.add_face();
00090   topo::n_face<0, D> v1 = c.add_face();
00091   topo::n_face<0, D> v2 = c.add_face();
00092   topo::n_face<0, D> v3 = c.add_face();
00093 
00094   // 1-faces (segments).
00095   topo::n_face<1, D> e0 = c.add_face(v0 + v1);
00096   topo::n_face<1, D> e1 = c.add_face(v0 + v2);
00097   topo::n_face<1, D> e2 = c.add_face(v1 + v2);
00098   topo::n_face<1, D> e3 = c.add_face(v0 + v3);
00099   topo::n_face<1, D> e4 = c.add_face(v2 + v3);
00100 
00101   // 2-faces (triangles).
00102   topo::n_face<2, D> t0 = c.add_face(e0 + e1 + e2);
00103   topo::n_face<2, D> t1 = c.add_face(e1 + e3 + e4);
00104 
00105 
00106   /*-------------------------.
00107   | Complex-based site set.  |
00108   `-------------------------*/
00109 
00110   typedef point2d P;
00111   typedef geom::complex_geometry<D, P> G;
00112   G geom;
00113   geom.add_location(point2d(0,1)); // 0-face #0.
00114   geom.add_location(point2d(2,0)); // 0-face #1.
00115   geom.add_location(point2d(2,2)); // 0-face #2.
00116   geom.add_location(point2d(0,3)); // 0-face #3. 
00117   p_complex<D, G> pc(c, geom);
00118 
00119 
00120   /*----------------------.
00121   | Complex-based image.  |
00122   `----------------------*/
00123 
00124   using mln::value::int_u8;
00125 
00126   // An image type built on a 2-complex with mln::int_u8 values on
00127   // each face.
00128   typedef complex_image<D, G, int_u8> ima_t;
00129   ima_t ima(pc);
00130   // Initialize values.
00131   debug::iota(ima);
00132 
00133   /* Values of IMA.
00134 
00135               v0      e3     v3             1      8      4
00136                 o-----------o                o-----------o
00137                / \ ,-----. /                / \ ,-----. /
00138               / . \ \ t1/ /                / . \ \ 11/ /
00139           e0 / / \ e1\ / / e4           5 / / \ 6 \ / / 9
00140             / /t0 \ \ ' /                / /10 \ \ ' /
00141            / `-----' \ /                / `-----' \ /
00142           o-----------o                o-----------o
00143        v1      e2      v2            2      7       3
00144 
00145   */
00146 
00147   // Manual iteration over the domain of IMA.
00148   mln_piter_(ima_t) p(ima.domain());
00149   for_all (p)
00150     std::cout << "ima (" << p << ") = " << ima(p) << std::endl;
00151   std::cout << std::endl << std::endl;
00152 
00153   /*---------------------------------------------------.
00154   | Morphological operations on complex-based images.  |
00155   `---------------------------------------------------*/
00156 
00157   test_morpho(ima, complex_lower_window_p<D, G>(),
00158               "lower-dimension faces");
00159   test_morpho(ima, complex_higher_window_p<D, G>(),
00160               "higher-dimension faces");
00161   test_morpho(ima, complex_lower_higher_window_p<D, G>(),
00162               "lower- and higer-dimension faces");
00163 
00164   test_morpho(ima, complex_lower_dim_connected_n_face_window_p<D, G>(),
00165               "lower-dimension connected n-faces");
00166   test_morpho(ima, complex_higher_dim_connected_n_face_window_p<D, G>(),
00167               "higher-dimension connected n-faces");
00168 
00169   /* FIXME: Exercise elementary erosion/dilation (with neighborhoods)
00170      when available.  */
00171 }
00172 
00173 
00174 template <typename I, typename W>
00175 void
00176 test_morpho(const mln::Image<I>& ima_, const mln::Window<W> win,
00177             const std::string& comment)
00178 {
00179   const I& ima = exact(ima_);
00180   mln_assertion(ima.is_valid());
00181   mln_piter(I) p(ima.domain());
00182 
00183   mln_concrete(I) ima_dil = mln::morpho::dilation(ima, win);
00184   // Manual iteration over the domain of IMA_DIL.
00185   std::cout << "Dilation using " << comment << ":" << std::endl;
00186   for_all (p)
00187     std::cout << "  ima_dil (" << p << ") = " << ima_dil(p) << std::endl;
00188   std::cout << std::endl;
00189 
00190   std::cout << "Erosion using " << comment  << ":" << std::endl;
00191   mln_concrete(I) ima_ero = mln::morpho::erosion(ima, win);
00192   // Manual iteration over the domain of IMA_ERO.
00193   for_all (p)
00194     std::cout << "  ima_ero (" << p << ") = " << ima_ero(p) << std::endl;
00195   std::cout << std::endl << std::endl;
00196 }

Generated on Tue Oct 4 2011 15:23:37 for Milena (Olena) by  doxygen 1.7.1