cc.hh

00001 // Copyright (C) 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_LEVEL_CC_HH
00029 # define OLENA_LEVEL_CC_HH
00030 
00031 /*
00032   Connected components.
00033 */
00034 
00035 # include <oln/arith/ops.hh>
00036 # include <oln/core/image.hh>
00037 # include <oln/level/fill.hh>
00038 
00039 # include <set>
00040 
00041 namespace oln {
00042 
00045   namespace level {
00046 
00047     // optional behavior for this algorithm.
00048     struct update_label;
00049 
00096     template <class DestType, class I, class E>
00097     typename mute<I, DestType>::ret
00098     frontp_connected_component(const abstract::binary_image<I>& input,
00099                                const abstract::neighborhood<E>& se,
00100                                unsigned& nb_label)
00101     {
00102       typename mute<I, DestType>::ret output(input.size());
00103       level::fill(output, 0);
00104 
00105       typedef std::set<oln_point_type(I),
00106                        oln::internal::default_less<oln_point_type(I) > >
00107               points_set;
00108 
00109       I is_processed(input.size());
00110       level::fill(is_processed, false);
00111       DestType cur_label = 1;
00112       oln_iter_type(I) p(input);
00113       for_all(p) if ((input[p] == true)&& (is_processed[p] == false))
00114         {
00115           //propagation front
00116           points_set component;
00117           component.insert(p.cur());
00118           points_set points_to_process;
00119           points_to_process.insert(p.cur());
00120           while (!points_to_process.empty())
00121             {
00122               //set label and net neighbors
00123               points_set next;
00124               for (typename points_set::const_iterator i =
00125                      points_to_process.begin();
00126                    i != points_to_process.end();
00127                    ++i)
00128                 {
00129                   component.insert(*i);
00130                   oln_neighb_type(E) p_prime(se, *i);
00131                   for_all (p_prime) if(input.hold(p_prime) &&
00132                                        (input[p_prime] == true))
00133                     next.insert(p_prime.cur());
00134                 }
00135               points_to_process.clear();
00136               set_difference(next.begin(), next.end(),
00137                              component.begin(), component.end(),
00138                              inserter(points_to_process,
00139                                       points_to_process.begin()),
00140                              oln::internal::default_less<oln_point_type(I) >());
00141             }
00142           for (typename points_set::const_iterator i = component.begin();
00143                i != component.end();
00144                ++i)
00145             {
00146               output[*i] = cur_label;
00147               is_processed[*i] = true;
00148             }
00149           cur_label++;
00150         }
00151       nb_label = cur_label;
00152       return output;
00153     }
00154 
00155     template <class DestType, class I, class E>
00156     typename mute<I, DestType>::ret
00157     frontp_connected_component(const abstract::image<I>& input,
00158                                const abstract::neighborhood<E>& se)
00159     {
00160       unsigned dummy;
00161       return frontp_connected_component(input, se, dummy);
00162     }
00163 
00164     template <class I>
00165     typename mute<I, ntg::bin>::ret
00166     extract_i_cc(const abstract::image<I>& input,
00167                  oln_value_type(I) i)
00168     {
00169 
00170       typename mute<I, ntg::bin>::ret output(input.size());
00171       level::fill(output, false);
00172       oln_iter_type(I) p(input);
00173       for_all(p)
00174         if (input[p] == i)
00175           output[p] = true;
00176       return output;
00177     }
00178 
00179     template <class I>
00180     oln_value_type(I) get_n_cc(const abstract::image<I>& input)
00181     {
00182       return fold(arith::default_f_max<oln_value_type(I)>(), input);
00183     }
00184 
00185   } // end of namespace level
00186 
00187 } // end of namespace oln
00188 
00189 #endif // ! OLENA_LEVEL_CC_HH

Generated on Tue Feb 20 20:18:36 2007 for Olena by  doxygen 1.5.1